draft-dsl-match
Version:
pattern match DSL for typedraft
86 lines (85 loc) • 4.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@babel/types");
const typedraft_1 = require("typedraft");
const draft_dsl_match_1 = require("draft-dsl-match");
var ts_pattern_1 = require("ts-pattern");
exports.__ = ts_pattern_1.__;
exports.MatchDSL = ts_pattern_1.match;
exports.when = ts_pattern_1.when;
exports.not = ts_pattern_1.not;
exports.use = ts_pattern_1.select;
exports.String = draft_dsl_match_1.__.string;
exports.Number = draft_dsl_match_1.__.number;
exports.Boolean = draft_dsl_match_1.__.boolean;
class PatternMatch {
constructor(config) {
var _a;
this.m_Factory = (_a = config === null || config === void 0 ? void 0 : config.factory) !== null && _a !== void 0 ? _a : "MatchDSL";
}
InplaceTranscribe(literal, output_type) {
const template_elements = literal.node.quasis;
const template_expressions = literal.node.expressions;
const [to_match] = template_expressions;
let root = this.CallChainRoot(to_match, output_type);
template_elements.forEach((each, index) => {
if (each.value.raw.trim() === "->") {
const arg1 = template_expressions[index - 1];
const handler = template_expressions[index];
const { predicate, pattern, method } = draft_dsl_match_1.MatchDSL(arg1).with({
type: "CallExpression",
callee: {
name: draft_dsl_match_1.not("not")
}
}, () => ({
predicate: WrapAsArrowFunctionExpression(arg1),
method: types_1.identifier("when")
})).when(() => ["BinaryExpression", "LogicalExpression"].includes(arg1.type), () => ({
predicate: WrapAsArrowFunctionExpression(arg1),
method: types_1.identifier("when")
})).with({
type: "ArrowFunctionExpression"
}, () => ({
predicate: arg1,
method: types_1.identifier("when")
})).with(draft_dsl_match_1.__, () => ({
pattern: arg1,
method: types_1.identifier("with")
})).run();
const _handler = draft_dsl_match_1.MatchDSL(handler).with({
type: "ArrowFunctionExpression",
body: {
extra: {
parenthesized: true
}
}
}, () => {
const _handler = WrapAsArrowFunctionExpression(handler.body);
_handler.params = handler.params;
return _handler;
}).with({
type: "ArrowFunctionExpression"
}, () => handler).with(draft_dsl_match_1.__, () => WrapAsArrowFunctionExpression(handler)).run();
root = AppendToCallChain(root, method, [pattern !== null && pattern !== void 0 ? pattern : predicate, _handler]);
}
});
root = AppendToCallChain(root, types_1.identifier("run"), []);
return root;
}
CallChainRoot(to_match, output_type) {
const root = types_1.callExpression(types_1.identifier(this.m_Factory), [to_match]);
if (types_1.isTSAsExpression(to_match)) {
root.typeParameters = types_1.tsTypeParameterInstantiation([to_match.typeAnnotation]);
root.arguments = [to_match.expression];
if (output_type) {
const type = CreateTSTypeFromString(output_type);
root.typeParameters.params.push(type);
}
}
return root;
}
}
exports.PatternMatch = PatternMatch;
const WrapAsArrowFunctionExpression = (expression) => types_1.arrowFunctionExpression([], types_1.blockStatement([types_1.returnStatement(expression)]));
const CreateTSTypeFromString = (type) => typedraft_1.ToAst(`type _ = ${type}`).typeAnnotation;
const AppendToCallChain = (root, method, args) => types_1.callExpression(types_1.memberExpression(root, method), args);