@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
88 lines (87 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouterAnalyzer = void 0;
const ts_morph_1 = require("ts-morph");
const AbstractAnnotationAnalyzer_1 = require("./interface/AbstractAnnotationAnalyzer");
const DecoratorParser_1 = require("./utils/DecoratorParser");
const constants_1 = require("../constants");
const framework_1 = require("../../framework");
const PluginError_1 = require("../error/PluginError");
class RouterAnalyzer extends AbstractAnnotationAnalyzer_1.AbstractAnnotationAnalyzer {
constructor(constantResolver) {
super(constantResolver);
this.name = 'RouterAnalyzer';
}
analyze(sourceFile, filePath, context) {
framework_1.Logger.debug('', `Start to analyze source file: ${filePath}`);
sourceFile.getChildrenOfKind(ts_morph_1.SyntaxKind.MissingDeclaration).forEach((node) => {
const componentName = this.extractComponentNameFromNode(node);
node.getChildrenOfKind(ts_morph_1.SyntaxKind.Decorator).forEach((decorator) => {
if (decorator.getName() === constants_1.AnnotationConstants.ROUTER_ANNOTATION) {
const result = this.addToResultSet(decorator, componentName, filePath, sourceFile);
context.addAnalyzeResults(result);
}
});
});
sourceFile.getExportAssignments().forEach((exportAssignment) => {
const componentName = exportAssignment.getNextSiblingIfKind(ts_morph_1.SyntaxKind.ExpressionStatement)?.getText() || '';
exportAssignment.getDescendantsOfKind(ts_morph_1.SyntaxKind.Decorator).forEach((decorator) => {
if (decorator.getName() === constants_1.AnnotationConstants.ROUTER_ANNOTATION) {
const result = this.addToResultSet(decorator, componentName, filePath, sourceFile);
context.addAnalyzeResults(result);
context.addTemplateData(result.name, { isDefaultExport: 'true' });
}
});
});
this.checkNavDestinationUsage(sourceFile, filePath);
}
addToResultSet(decorator, componentName, filePath, sourceFile) {
const decoratorResult = DecoratorParser_1.DecoratorParser.parseDecorator(decorator, this.constantResolver, sourceFile, filePath);
decoratorResult.name = componentName;
decoratorResult.sourceFilePath = filePath;
return decoratorResult;
}
checkNavDestinationUsage(sourceFile, filePath) {
const statements = sourceFile.getStatements();
const sortedStatements = statements.sort((a, b) => a.getStart() - b.getStart());
let HMRouterExists = false;
let useNavDst = false;
sortedStatements.forEach((statement) => {
if (statement.getKind() === ts_morph_1.SyntaxKind.MissingDeclaration &&
statement.getText().includes(constants_1.AnnotationConstants.ROUTER_ANNOTATION)) {
HMRouterExists = true;
useNavDst = false;
const text = statement.getText();
if (/(useNavDst\s*:\s*true)/.test(text)) {
framework_1.Logger.debug('', `Found useNavDst: true in @HMRouter annotation, skip NavDestination check, filePath: ${filePath}`);
useNavDst = true;
}
}
if (statement.getKind() === ts_morph_1.SyntaxKind.Block && HMRouterExists) {
HMRouterExists = false;
if (useNavDst) {
return;
}
const reg = new RegExp(/NavDestination\s*\(\s*\)/);
const text = statement.getText();
const cleanedCodeBlock = text
.replace(/(["'`]).*?\1/g, '')
.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, '');
if (reg.test(cleanedCodeBlock)) {
throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.WRONG_DECORATION, '', filePath);
}
}
});
}
extractComponentNameFromNode(node) {
const nextSibling = node.getNextSiblingIfKind(ts_morph_1.SyntaxKind.ExpressionStatement);
if (nextSibling && nextSibling.getText() === constants_1.PluginConstants.STRUCT_KEYWORD) {
const componentName = nextSibling.getNextSiblingIfKind(ts_morph_1.SyntaxKind.ExpressionStatement);
if (componentName) {
return componentName.getText();
}
}
return '';
}
}
exports.RouterAnalyzer = RouterAnalyzer;