1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.hasNgModuleImport = void 0;
|
---|
11 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
12 | const ts = require("typescript");
|
---|
13 | /**
|
---|
14 | * Whether the Angular module in the given path imports the specified module class name.
|
---|
15 | */
|
---|
16 | function hasNgModuleImport(tree, modulePath, className) {
|
---|
17 | const moduleFileContent = tree.read(modulePath);
|
---|
18 | if (!moduleFileContent) {
|
---|
19 | throw new schematics_1.SchematicsException(`Could not read Angular module file: ${modulePath}`);
|
---|
20 | }
|
---|
21 | const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(), ts.ScriptTarget.Latest, true);
|
---|
22 | const ngModuleMetadata = findNgModuleMetadata(parsedFile);
|
---|
23 | if (!ngModuleMetadata) {
|
---|
24 | throw new schematics_1.SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`);
|
---|
25 | }
|
---|
26 | for (let property of ngModuleMetadata.properties) {
|
---|
27 | if (!ts.isPropertyAssignment(property) || property.name.getText() !== 'imports' ||
|
---|
28 | !ts.isArrayLiteralExpression(property.initializer)) {
|
---|
29 | continue;
|
---|
30 | }
|
---|
31 | if (property.initializer.elements.some(element => element.getText() === className)) {
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return false;
|
---|
36 | }
|
---|
37 | exports.hasNgModuleImport = hasNgModuleImport;
|
---|
38 | /**
|
---|
39 | * Resolves the last identifier that is part of the given expression. This helps resolving
|
---|
40 | * identifiers of nested property access expressions (e.g. myNamespace.core.NgModule).
|
---|
41 | */
|
---|
42 | function resolveIdentifierOfExpression(expression) {
|
---|
43 | if (ts.isIdentifier(expression)) {
|
---|
44 | return expression;
|
---|
45 | }
|
---|
46 | else if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.name)) {
|
---|
47 | return expression.name;
|
---|
48 | }
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 | /**
|
---|
52 | * Finds a NgModule declaration within the specified TypeScript node and returns the
|
---|
53 | * corresponding metadata for it. This function searches breadth first because
|
---|
54 | * NgModule's are usually not nested within other expressions or declarations.
|
---|
55 | */
|
---|
56 | function findNgModuleMetadata(rootNode) {
|
---|
57 | // Add immediate child nodes of the root node to the queue.
|
---|
58 | const nodeQueue = [...rootNode.getChildren()];
|
---|
59 | while (nodeQueue.length) {
|
---|
60 | const node = nodeQueue.shift();
|
---|
61 | if (ts.isDecorator(node) && ts.isCallExpression(node.expression) &&
|
---|
62 | isNgModuleCallExpression(node.expression)) {
|
---|
63 | return node.expression.arguments[0];
|
---|
64 | }
|
---|
65 | else {
|
---|
66 | nodeQueue.push(...node.getChildren());
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | /** Whether the specified call expression is referring to a NgModule definition. */
|
---|
72 | function isNgModuleCallExpression(callExpression) {
|
---|
73 | if (!callExpression.arguments.length ||
|
---|
74 | !ts.isObjectLiteralExpression(callExpression.arguments[0])) {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | // The `NgModule` call expression name is never referring to a `PrivateIdentifier`.
|
---|
78 | const decoratorIdentifier = resolveIdentifierOfExpression(callExpression.expression);
|
---|
79 | return decoratorIdentifier ? decoratorIdentifier.text === 'NgModule' : false;
|
---|
80 | }
|
---|
81 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmctbW9kdWxlLWltcG9ydHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3NjaGVtYXRpY3MvdXRpbHMvYXN0L25nLW1vZHVsZS1pbXBvcnRzLnRzIl0sIm5hb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