{"version":3,"sources":["parser/macro.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,sBAAsB,EAAmC,MAAM,UAAU,CAAC;AAMnF,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,EAAE,eAAe,EAAE,EAAE,CAAC,eAAe,EAAE,WAAW,EAAE,EAAE,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC,OAAO,EAAE,kBAAkB,EAAE,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,sBAAsB,CAAC,GAAG,EAAE,CAAC,UAAU,CAuH7N","file":"macro.d.ts","sourcesContent":["import { PACKAGE_NAME } from \"@src/config\";\nimport ts from \"typescript\";\nimport { MacroAnnotationHandler, MacroAnnotationNode, MacroUtils } from 'tt-model';\nimport { runInNewContext } from 'vm';\nimport { errorFile } from \"@src/utils/error\";\n\n/**\n * Resolve Annotation Macro\n */\nexport function resolveAnnotationMacro(\n\tsrcFile: ts.SourceFile,\n\tcompilerOptions: ts.CompilerOptions,\n\ttypechecker: ts.TypeChecker,\n\tprogram: ts.Program,\n\tMarcoAnnotationMap: Map<ts.Node, MacroAnnotationHandler>\n): ts.SourceFile {\n\tconst factory = ts.factory;\n\tconst imports: ts.ImportDeclaration[] = []\n\tsrcFile = ts.transform(srcFile, [function (ctx: ts.TransformationContext) {\n\t\tfunction _visitor(node: ts.Node): ts.Node {\n\t\t\tswitch (node.kind) {\n\t\t\t\tcase ts.SyntaxKind.SourceFile:\n\t\t\t\t\treturn ts.visitEachChild(node, _visitor, ctx);\n\t\t\t\tcase ts.SyntaxKind.ClassDeclaration: {\n\t\t\t\t\tif (node.decorators != null) {\n\t\t\t\t\t\tlet deco = _resolveDecorators(node as ts.ClassDeclaration);\n\t\t\t\t\t\tif (deco.modified) {\n\t\t\t\t\t\t\tlet entityNode = deco.node;\n\t\t\t\t\t\t\tnode = factory.updateClassDeclaration(\n\t\t\t\t\t\t\t\tentityNode, deco.decorators, entityNode.modifiers, entityNode.name,\n\t\t\t\t\t\t\t\tentityNode.typeParameters, entityNode.heritageClauses, entityNode.members\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn ts.visitEachChild(node, _visitor, ctx);\n\t\t\t\t}\n\t\t\t\tcase ts.SyntaxKind.MethodDeclaration: {\n\t\t\t\t\tif (node.decorators != null) {\n\t\t\t\t\t\tlet deco = _resolveDecorators(node as ts.MethodDeclaration);\n\t\t\t\t\t\tif (deco.modified) {\n\t\t\t\t\t\t\tlet entityNode = deco.node;\n\t\t\t\t\t\t\tnode = factory.updateMethodDeclaration(\n\t\t\t\t\t\t\t\tentityNode, deco.decorators, entityNode.modifiers, entityNode.asteriskToken,\n\t\t\t\t\t\t\t\tentityNode.name, entityNode.questionToken, entityNode.typeParameters,\n\t\t\t\t\t\t\t\tentityNode.parameters, entityNode.type, entityNode.body\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase ts.SyntaxKind.PropertyDeclaration: {\n\t\t\t\t\tif (node.decorators != null) {\n\t\t\t\t\t\tlet deco = _resolveDecorators(node as ts.PropertyDeclaration);\n\t\t\t\t\t\tif (deco.modified) {\n\t\t\t\t\t\t\tlet entityNode = deco.node;\n\t\t\t\t\t\t\tnode = factory.updatePropertyDeclaration(\n\t\t\t\t\t\t\t\tentityNode, deco.decorators, entityNode.modifiers, entityNode.name,\n\t\t\t\t\t\t\t\tentityNode.questionToken, entityNode.type, entityNode.initializer\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn node;\n\t\t}\n\t\treturn _visitor;\n\t}], compilerOptions).transformed[0] as ts.SourceFile;\n\t// Add imports\n\tif (imports.length > 0) {\n\t\tsrcFile = factory.updateSourceFile(\n\t\t\tsrcFile, [...imports, ...srcFile.statements], srcFile.isDeclarationFile,\n\t\t\tsrcFile.referencedFiles, srcFile.typeReferenceDirectives, srcFile.hasNoDefaultLib,\n\t\t\tsrcFile.libReferenceDirectives\n\t\t);\n\t}\n\treturn srcFile;\n\n\t/** Resolve Decorators */\n\tfunction _resolveDecorators(node: ts.ClassDeclaration): ResolveDecoReturn<ts.ClassDeclaration>;\n\tfunction _resolveDecorators(node: ts.MethodDeclaration): ResolveDecoReturn<ts.MethodDeclaration>;\n\tfunction _resolveDecorators(node: ts.PropertyDeclaration): ResolveDecoReturn<ts.PropertyDeclaration>;\n\tfunction _resolveDecorators(\n\t\tnode: MacroAnnotationNode\n\t): ResolveDecoReturn<MacroAnnotationNode> {\n\t\tvar decorators: ts.NodeArray<ts.Decorator> | ts.Decorator[] | undefined = node.decorators;\n\t\tvar decoRmSet: Set<ts.Decorator> = new Set();\n\t\tif (decorators != null) {\n\t\t\tfor (let i = 0, len = decorators.length; i < len; ++i) {\n\t\t\t\tlet decorator = decorators[i];\n\t\t\t\ttry {\n\t\t\t\t\tlet decoratorCall = decorator.expression as ts.CallExpression;\n\t\t\t\t\tlet decoExpr: ts.Node | undefined;\n\t\t\t\t\tlet decoSymbol: ts.Symbol | undefined;\n\t\t\t\t\tlet varSymbol: ts.Symbol | undefined;\n\t\t\t\t\tlet varExpr: ts.Node | undefined;\n\t\t\t\t\tif (\n\t\t\t\t\t\t(decoExpr = decoratorCall.expression) &&\n\t\t\t\t\t\t(decoSymbol = typechecker.getSymbolAtLocation(decoExpr)) &&\n\t\t\t\t\t\t(varSymbol = typechecker.getAliasedSymbol(decoSymbol)) &&\n\t\t\t\t\t\t(varExpr = varSymbol.declarations?.[0]) &&\n\t\t\t\t\t\t(ts.isVariableDeclaration(varExpr)) &&\n\t\t\t\t\t\t(varExpr = varExpr.initializer) &&\n\t\t\t\t\t\t(ts.isCallExpression(varExpr))\n\t\t\t\t\t) {\n\t\t\t\t\t\tlet handler = MarcoAnnotationMap.get(varExpr.expression);\n\t\t\t\t\t\tif (handler == null) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t//* Check create by \"AnnotationMacro\"\n\t\t\t\t\t\t\t\t(varSymbol = typechecker.getSymbolAtLocation(varExpr.expression)) &&\n\t\t\t\t\t\t\t\t(decoExpr = varSymbol.declarations?.[0]) &&\n\t\t\t\t\t\t\t\tts.isImportSpecifier(decoExpr) &&\n\t\t\t\t\t\t\t\t((decoExpr.parent.parent.parent as ts.ImportDeclaration).moduleSpecifier.getText().slice(1, -1) === PACKAGE_NAME) &&\n\t\t\t\t\t\t\t\t(handler = _resolveHandler(varExpr, compilerOptions))\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tMarcoAnnotationMap.set(varExpr.expression, handler);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcontinue; // Continue for loop\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Get arguments expression\n\t\t\t\t\t\tlet args = decoratorCall.arguments.map(a => {\n\t\t\t\t\t\t\tif (ts.isEnumMember(a) || ts.isPropertyAccessExpression(a) || ts.isElementAccessExpression(a))\n\t\t\t\t\t\t\t\treturn typechecker.getConstantValue(a);\n\t\t\t\t\t\t\telse return undefined;\n\t\t\t\t\t\t});\n\t\t\t\t\t\t// Exec handler\n\t\t\t\t\t\tconst macroUtils = new MacroUtils(program, node, decoratorCall.arguments.map(a => a.getText()), args)\n\t\t\t\t\t\tnode = handler(node, macroUtils, ...args);\n\t\t\t\t\t\tif (macroUtils._imports.length > 0) imports.push(...macroUtils._imports);\n\t\t\t\t\t\tdecoRmSet.add(decorator);\n\t\t\t\t\t}\n\t\t\t\t} catch (err: any) {\n\t\t\t\t\tthrow new Error(`Error at: ${errorFile(srcFile, decorator)}\\n${err?.message ?? err}`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tdecorators = node.decorators;\n\t\t\t// Update decorators\n\t\t\tif (decorators != null && decoRmSet.size > 0) {\n\t\t\t\tlet decoArr: ts.Decorator[] = [];\n\t\t\t\tfor (let i = 0, len = decorators.length; i < len; ++i) {\n\t\t\t\t\tlet deco = decorators[i];\n\t\t\t\t\tif (!decoRmSet.has(deco)) decoArr.push(deco);\n\t\t\t\t}\n\t\t\t\tdecorators = decoArr;\n\t\t\t}\n\t\t}\n\t\treturn { node, decorators, modified: decoRmSet.size > 0 }\n\t}\n}\n\ninterface ResolveDecoReturn<T> {\n\tnode: T\n\tdecorators: ts.Decorator[] | ts.NodeArray<ts.Decorator> | undefined\n\t/** If node has modifications */\n\tmodified: boolean\n}\n\nfunction _resolveHandler(decoExpr: ts.CallExpression, compilerOptions: ts.CompilerOptions): MacroAnnotationHandler | undefined {\n\t// Prepare module text\n\tlet code = ts.transpile(`module.fx=${decoExpr.arguments[0].getText()}`, compilerOptions, undefined);\n\t//* Prepare context\n\tconst ctx: { module: { fx?: MacroAnnotationHandler }, console: any } = { module: {}, console };\n\t//* Exec\n\trunInNewContext(code, ctx, { timeout: 1000 });\n\treturn ctx.module.fx;\n}\n"]}