imports.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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. /** Gets import information about the specified identifier by using the type checker. */
  12. function getImportOfIdentifier(typeChecker, node) {
  13. const symbol = typeChecker.getSymbolAtLocation(node);
  14. if (!symbol || !symbol.declarations.length) {
  15. return null;
  16. }
  17. const decl = symbol.declarations[0];
  18. if (!ts.isImportSpecifier(decl)) {
  19. return null;
  20. }
  21. // Since "decl" is an import specifier, we can walk up three times to get a reference
  22. // to the import declaration node (NamedImports -> ImportClause -> ImportDeclaration).
  23. const importDecl = decl.parent.parent.parent;
  24. if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
  25. return null;
  26. }
  27. return {
  28. // Handles aliased imports: e.g. "import {Component as myComp} from ...";
  29. name: decl.propertyName ? decl.propertyName.text : decl.name.text,
  30. importModule: importDecl.moduleSpecifier.text,
  31. node: importDecl
  32. };
  33. }
  34. exports.getImportOfIdentifier = getImportOfIdentifier;
  35. //# sourceMappingURL=imports.js.map