@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
45 lines (44 loc) • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DecoratorParser = void 0;
const ts_morph_1 = require("ts-morph");
class DecoratorParser {
static parseDecorator(decorator, constantResolver, sourceFile, filePath) {
const decoratorResult = {
annotation: decorator.getName(),
name: '',
sourceFilePath: ''
};
const args = this.parseDecoratorArguments(decorator, constantResolver, sourceFile, filePath);
Object.assign(decoratorResult, args);
return decoratorResult;
}
static parseDecoratorArguments(decorator, constantResolver, sourceFile, filePath) {
const argResult = {
annotation: decorator.getName(),
name: '',
sourceFilePath: '',
};
decorator.getArguments().forEach((arg) => {
const objLiteral = arg.asKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression);
if (objLiteral) {
objLiteral.getProperties().forEach((prop) => {
this.hondleObjLiteral(prop, constantResolver, sourceFile, filePath, argResult);
});
}
});
return argResult;
}
static hondleObjLiteral(prop, constantResolver, sourceFile, filePath, argResult) {
if (prop.getKind() === ts_morph_1.SyntaxKind.PropertyAssignment) {
const propertyAssignment = prop;
const propertyName = propertyAssignment.getName();
const initializer = propertyAssignment.getInitializer();
if (initializer) {
const propertyValue = constantResolver.resolvePropertyValue(initializer, sourceFile, filePath);
Reflect.set(argResult, propertyName, propertyValue);
}
}
}
}
exports.DecoratorParser = DecoratorParser;