UNPKG

typia

Version:

Superfast runtime validators with only one line

106 lines (100 loc) 3.33 kB
import ts from "typescript"; import { IdentifierFactory } from "../../factories/IdentifierFactory"; import { StatementFactory } from "../../factories/StatementFactory"; import { TypeFactory } from "../../factories/TypeFactory"; import { IProject } from "../../transformers/IProject"; import { AssertProgrammer } from "../AssertProgrammer"; import { FeatureProgrammer } from "../FeatureProgrammer"; import { FunctionImporter } from "../helpers/FunctionImporter"; import { MiscPruneProgrammer } from "./MiscPruneProgrammer"; export namespace MiscAssertPruneProgrammer { export const decompose = (props: { project: IProject; importer: FunctionImporter; type: ts.Type; name: string | undefined; init: ts.Expression | undefined; }): FeatureProgrammer.IDecomposed => { const assert: FeatureProgrammer.IDecomposed = AssertProgrammer.decompose({ ...props, equals: false, guard: false, }); const prune: FeatureProgrammer.IDecomposed = MiscPruneProgrammer.decompose({ ...props, validated: true, }); return { functions: { ...assert.functions, ...prune.functions, }, statements: [ ...assert.statements, ...prune.statements, StatementFactory.constant("__assert", assert.arrow), StatementFactory.constant("__prune", prune.arrow), ], arrow: ts.factory.createArrowFunction( undefined, undefined, [ IdentifierFactory.parameter("input", TypeFactory.keyword("any")), AssertProgrammer.Guardian.parameter(props.init), ], ts.factory.createTypeReferenceNode( props.name ?? TypeFactory.getFullName(props.project.checker)(props.type), ), undefined, ts.factory.createBlock( [ ts.factory.createExpressionStatement( ts.factory.createBinaryExpression( ts.factory.createIdentifier("input"), ts.SyntaxKind.EqualsToken, ts.factory.createCallExpression( ts.factory.createIdentifier("__assert"), undefined, [ ts.factory.createIdentifier("input"), AssertProgrammer.Guardian.identifier(), ], ), ), ), ts.factory.createExpressionStatement( ts.factory.createCallExpression( ts.factory.createIdentifier("__prune"), undefined, [ts.factory.createIdentifier("input")], ), ), ts.factory.createReturnStatement( ts.factory.createIdentifier("input"), ), ], true, ), ), }; }; export const write = (project: IProject) => (modulo: ts.LeftHandSideExpression) => (type: ts.Type, name?: string, init?: ts.Expression): ts.CallExpression => { const importer: FunctionImporter = new FunctionImporter(modulo.getText()); const result: FeatureProgrammer.IDecomposed = decompose({ project, importer, type, name, init, }); return FeatureProgrammer.writeDecomposed({ modulo, importer, result, }); }; }