@nx/angular
Version:
18 lines (17 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentDataFromAST = getComponentDataFromAST;
function getComponentDataFromAST(tree, normalizedComponentPath) {
const COMPONENT_CONTENT_SELECTOR = 'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=Component]))';
const COMPONENT_NAME_SELECTOR = 'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=Component])) > Identifier';
const componentFileContents = tree.read(normalizedComponentPath, 'utf-8');
const { tsquery } = require('@phenomnomnominal/tsquery');
const componentAST = tsquery.ast(componentFileContents);
const componentNode = tsquery(componentAST, COMPONENT_CONTENT_SELECTOR, {
visitAllChildren: true,
})[0];
const componentContents = componentFileContents.slice(componentNode.getStart(), componentNode.getEnd());
const componentNameNode = tsquery(tsquery.ast(componentContents), COMPONENT_NAME_SELECTOR, { visitAllChildren: true })[0];
const componentName = componentNameNode.getText();
return { componentFileContents, componentAST, componentName };
}