UNPKG

typia

Version:

Superfast runtime validators with only one line

102 lines (99 loc) 6.05 kB
import ts from 'typescript'; import { _randomFormatUuid } from '../internal/_randomFormatUuid.mjs'; var ExpressionFactory; (function (ExpressionFactory) { ExpressionFactory.number = (value) => value < 0 ? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(Math.abs(value))) : ts.factory.createNumericLiteral(value); ExpressionFactory.bigint = (value) => ts.factory.createCallExpression(ts.factory.createIdentifier("BigInt"), undefined, [ts.factory.createIdentifier(value.toString())]); ExpressionFactory.isRequired = (input) => ts.factory.createStrictInequality(ts.factory.createIdentifier("undefined"), input); ExpressionFactory.isArray = (input) => ts.factory.createCallExpression(ts.factory.createIdentifier("Array.isArray"), undefined, [input]); ExpressionFactory.isObject = (props) => { const conditions = [ ts.factory.createStrictEquality(ts.factory.createStringLiteral("object"), ts.factory.createTypeOfExpression(props.input)), ]; if (props.checkNull === true) conditions.push(ts.factory.createStrictInequality(ts.factory.createNull(), props.input)); if (props.checkArray === true) conditions.push(ts.factory.createStrictEquality(ts.factory.createFalse(), ts.factory.createCallExpression(ts.factory.createIdentifier("Array.isArray"), undefined, [props.input]))); return conditions.length === 1 ? conditions[0] : conditions.reduce((x, y) => ts.factory.createLogicalAnd(x, y)); }; ExpressionFactory.isInstanceOf = (type, input) => { return ts.factory.createBinaryExpression(input, ts.factory.createToken(ts.SyntaxKind.InstanceOfKeyword), ts.factory.createIdentifier(type)); }; ExpressionFactory.coalesce = (x, y) => ts.factory.createBinaryExpression(x, ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken), y); ExpressionFactory.currying = (props) => { if (props.arguments.length === 0) return ts.factory.createCallExpression(props.function, undefined, undefined); let prev = ts.factory.createCallExpression(props.function, undefined, [props.arguments[0]]); for (const param of props.arguments.slice(1)) prev = ts.factory.createCallExpression(prev, undefined, [param]); return prev; }; ExpressionFactory.selfCall = (body, type) => ts.isCallExpression(body) ? body : ts.factory.createCallExpression(ts.factory.createParenthesizedExpression(ts.factory.createArrowFunction(undefined, undefined, [], type, undefined, body)), undefined, undefined); ExpressionFactory.getEscapedText = (props) => props.printer.printNode(ts.EmitHint.Expression, props.input, props.input.getSourceFile()); ExpressionFactory.transpile = (props) => { const file = ts.createSourceFile(`${_randomFormatUuid()}.ts`, props.script, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS); const statement = file.statements[0]; if (statement === undefined) throw new ReferenceError("Error on ExpressionFactory.transpile(): no statement exists."); else if (!ts.isExpressionStatement(statement)) throw new TypeError("Error on ExpressionFactory.transpile(): statement is not an expression statement."); return (input) => { const visitor = (node) => { if (ts.isIdentifier(node) && node.text === "$input") return input; else if (props.importer !== undefined && ts.isCallExpression(node)) if (node.expression.getText() === "$importInternal" && node.arguments.length === 1 && ts.isStringLiteralLike(node.arguments[0])) { const name = node.arguments[0].text; return props.importer.internal(name); } else if (node.expression.getText() === "$importInstance" && node.arguments.length === 2 && ts.isStringLiteralLike(node.arguments[0]) && ts.isStringLiteralLike(node.arguments[1])) { const name = node.arguments[0].text; const file = node.arguments[1].text; return props.importer.instance({ file, name, alias: null, }); } else if (node.expression.getText() === "$importNamespace" && node.arguments.length === 2 && ts.isStringLiteralLike(node.arguments[0]) && ts.isStringLiteralLike(node.arguments[1])) { const name = node.arguments[0].text; const file = node.arguments[1].text; return props.importer.namespace({ file, name, }); } else if (node.expression.getText() === "$importDefault" && node.arguments.length === 3 && ts.isStringLiteralLike(node.arguments[0]) && ts.isStringLiteralLike(node.arguments[1])) { const name = node.arguments[0].text; const file = node.arguments[1].text; return props.importer.default({ file, name, type: false, }); } return ts.visitEachChild(ts.factory.cloneNode(node), visitor, props.transformer); }; return visitor(ts.factory.cloneNode(statement.expression)); }; }; })(ExpressionFactory || (ExpressionFactory = {})); export { ExpressionFactory }; //# sourceMappingURL=ExpressionFactory.mjs.map