Skip to content

chore(schematics): better NgModule search logic #14449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/cdk/schematics/utils/ast/ng-module-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,7 @@ export function hasNgModuleImport(tree: Tree, modulePath: string, className: str

const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(),
ts.ScriptTarget.Latest, true);
let ngModuleMetadata: ts.ObjectLiteralExpression | null = null;

const findModuleDecorator = (node: ts.Node) => {
if (ts.isDecorator(node) && ts.isCallExpression(node.expression) &&
isNgModuleCallExpression(node.expression)) {
ngModuleMetadata = node.expression.arguments[0] as ts.ObjectLiteralExpression;
return;
}

ts.forEachChild(node, findModuleDecorator);
};

ts.forEachChild(parsedFile, findModuleDecorator);
const ngModuleMetadata = findNgModuleMetadata(parsedFile);

if (!ngModuleMetadata) {
throw new SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`);
Expand Down Expand Up @@ -66,6 +54,29 @@ function resolveIdentifierOfExpression(expression: ts.Expression): ts.Identifier
return null;
}

/**
* Finds a NgModule declaration within the specified TypeScript node and returns the
* corresponding metadata for it. This function searches breadth first because
* NgModule's are usually not nested within other expressions or declarations.
*/
function findNgModuleMetadata(rootNode: ts.Node): ts.ObjectLiteralExpression | null {
// Add immediate child nodes of the root node to the queue.
const nodeQueue: ts.Node[] = [...rootNode.getChildren()];

while (nodeQueue.length) {
const node = nodeQueue.shift()!;

if (ts.isDecorator(node) && ts.isCallExpression(node.expression) &&
isNgModuleCallExpression(node.expression)) {
return node.expression.arguments[0] as ts.ObjectLiteralExpression;
} else {
nodeQueue.push(...node.getChildren());
}
}

return null;
}

/** Whether the specified call expression is referring to a NgModule definition. */
function isNgModuleCallExpression(callExpression: ts.CallExpression): boolean {
if (!callExpression.arguments.length ||
Expand Down