UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

98 lines (97 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TsAstUtil = exports.project = void 0; const ts_morph_1 = require("ts-morph"); const hvigor_1 = require("@ohos/hvigor"); const Logger_1 = require("../common/Logger"); exports.project = null; class TsAstUtil { static getSourceFile(filePath) { if (!hvigor_1.FileUtil.exist(filePath)) { Logger_1.Logger.warn(`File not found: ${filePath}`); const normalizedPath = filePath.replace(/\.+\/+/g, './'); if (normalizedPath !== filePath && hvigor_1.FileUtil.exist(normalizedPath)) { Logger_1.Logger.info(`Using normalized path instead: ${normalizedPath}`); filePath = normalizedPath; } else { throw new Error(`File not found: ${filePath}`); } } if (!exports.project) { exports.project = new ts_morph_1.Project({ compilerOptions: { target: ts_morph_1.ScriptTarget.ES2021 } }); } return exports.project.addSourceFileAtPath(filePath); } static clearProject() { exports.project = null; } static parseConstantValue(sourceFile, variableName, propertyName) { let result; if (propertyName) { let classInstance = sourceFile.getClasses().find((classes) => { return classes.getName() === variableName; }); if (!classInstance) { throw new Error(`Unknown class '${variableName}'`); } let property = classInstance.getProperties().find((properties) => { return properties.getName() === propertyName; }); if (!property) { throw new Error(`Unknown property '${propertyName}'`); } result = property.getInitializer(); } else { let constant = sourceFile.getVariableDeclarations().find(declaration => { return declaration.getName() === variableName; }); if (!constant) { throw new Error(`Unknown constant '${variableName}'`); } result = constant.getInitializer(); } if (result.getKind() !== ts_morph_1.SyntaxKind.StringLiteral) { Logger_1.Logger.error(Logger_1.PluginError.ERR_INVALID_STRING_VALUE, variableName); throw new Error('Invalid constants value, only string literal is supported, variableName:' + variableName); } return result.asKind(ts_morph_1.SyntaxKind.StringLiteral)?.getLiteralValue(); } static parseCrossModuleVariable(scanDir) { let sourceFiles = exports.project.addSourceFilesAtPaths(`${scanDir}/**/*.ets`); const exportMap = new Map(); for (let sourceFile of sourceFiles) { const exportedNames = this.getExportedVariables(sourceFile); if (exportedNames.length > 0) { exportMap.set(sourceFile.getFilePath(), exportedNames); } } return exportMap; } static getExportedVariables(sourceFile) { const exportSymbols = []; let exportKeywordNodes = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ExportKeyword); exportKeywordNodes.forEach((node) => { let parentNodeKind = node.getParent()?.getKind(); switch (parentNodeKind) { case ts_morph_1.SyntaxKind.VariableStatement: let variableStatement = node.getParent()?.asKind(ts_morph_1.SyntaxKind.VariableStatement); let variableNames = variableStatement.getDeclarationList().getDeclarations().map((declaration) => { return declaration.getName(); }); exportSymbols.push(...variableNames); break; case ts_morph_1.SyntaxKind.ClassDeclaration: let classDeclaration = node.getParent(); let className = classDeclaration?.asKind(ts_morph_1.SyntaxKind.ClassDeclaration)?.getName(); exportSymbols.push(className); break; } }); return exportSymbols; } } exports.TsAstUtil = TsAstUtil;