ng-module-imports.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. const ts = require("typescript");
  11. /**
  12. * Whether the Angular module in the given path imports the specifed module class name.
  13. */
  14. function hasNgModuleImport(tree, modulePath, className) {
  15. const moduleFileContent = tree.read(modulePath);
  16. if (!moduleFileContent) {
  17. throw new Error(`Could not read Angular module file: ${modulePath}`);
  18. }
  19. const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(), ts.ScriptTarget.Latest, true);
  20. let ngModuleMetadata = null;
  21. const findModuleDecorator = (node) => {
  22. if (ts.isDecorator(node) && ts.isCallExpression(node.expression) &&
  23. isNgModuleCallExpression(node.expression)) {
  24. ngModuleMetadata = node.expression.arguments[0];
  25. return;
  26. }
  27. ts.forEachChild(node, findModuleDecorator);
  28. };
  29. ts.forEachChild(parsedFile, findModuleDecorator);
  30. if (!ngModuleMetadata) {
  31. throw new Error(`Could not find NgModule declaration inside: "${modulePath}"`);
  32. }
  33. /* tslint:disable-next-line: no-non-null-assertion */
  34. for (const property of ngModuleMetadata.properties) {
  35. if (!ts.isPropertyAssignment(property) || property.name.getText() !== 'imports' ||
  36. !ts.isArrayLiteralExpression(property.initializer)) {
  37. continue;
  38. }
  39. /* tslint:disable-next-line: no-any */
  40. if (property.initializer.elements.some((element) => element.getText() === className)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. exports.hasNgModuleImport = hasNgModuleImport;
  47. /**
  48. * Resolves the last identifier that is part of the given expression. This helps resolving
  49. * identifiers of nested property access expressions (e.g. myNamespace.core.NgModule).
  50. */
  51. function resolveIdentifierOfExpression(expression) {
  52. if (ts.isIdentifier(expression)) {
  53. return expression;
  54. }
  55. else if (ts.isPropertyAccessExpression(expression)) {
  56. return resolveIdentifierOfExpression(expression.expression);
  57. }
  58. return null;
  59. }
  60. /** Whether the specified call expression is referring to a NgModule definition. */
  61. function isNgModuleCallExpression(callExpression) {
  62. if (!callExpression.arguments.length ||
  63. !ts.isObjectLiteralExpression(callExpression.arguments[0])) {
  64. return false;
  65. }
  66. const decoratorIdentifier = resolveIdentifierOfExpression(callExpression.expression);
  67. return decoratorIdentifier ? decoratorIdentifier.text === 'NgModule' : false;
  68. }
  69. //# sourceMappingURL=ng-module-imports.js.map