MCPcopy Create free account
hub / github.com/angular/components / hasNgModuleImport

Function hasNgModuleImport

src/cdk/schematics/utils/ast/ng-module-imports.ts:15–49  ·  view source on GitHub ↗
(tree: Tree, modulePath: string, className: string)

Source from the content-addressed store, hash-verified

13 * Whether the Angular module in the given path imports the specified module class name.
14 */
15export function hasNgModuleImport(tree: Tree, modulePath: string, className: string): boolean {
16 const moduleFileContent = tree.read(modulePath);
17
18 if (!moduleFileContent) {
19 throw new SchematicsException(`Could not read Angular module file: ${modulePath}`);
20 }
21
22 const parsedFile = ts.createSourceFile(
23 modulePath,
24 moduleFileContent.toString(),
25 ts.ScriptTarget.Latest,
26 true,
27 );
28 const ngModuleMetadata = findNgModuleMetadata(parsedFile);
29
30 if (!ngModuleMetadata) {
31 throw new SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`);
32 }
33
34 for (let property of ngModuleMetadata!.properties) {
35 if (
36 !ts.isPropertyAssignment(property) ||
37 property.name.getText() !== 'imports' ||
38 !ts.isArrayLiteralExpression(property.initializer)
39 ) {
40 continue;
41 }
42
43 if (property.initializer.elements.some(element => element.getText() === className)) {
44 return true;
45 }
46 }
47
48 return false;
49}
50
51/**
52 * Resolves the last identifier that is part of the given expression. This helps resolving

Callers 1

Calls 4

findNgModuleMetadataFunction · 0.85
readMethod · 0.80
toStringMethod · 0.80
getTextMethod · 0.45

Tested by

no test coverage detected