|
8 | 8 |
|
9 | 9 | import * as ts from 'typescript';
|
10 | 10 |
|
11 |
| -export type Import = { |
12 |
| - name: string, |
13 |
| - importModule: string, |
14 |
| - node: ts.ImportDeclaration |
15 |
| -}; |
16 |
| - |
17 |
| -/** Gets import information about the specified identifier by using the type checker. */ |
18 |
| -export function getImportOfIdentifier(typeChecker: ts.TypeChecker, node: ts.Identifier): Import| |
| 11 | +/** Interface describing a resolved import. */ |
| 12 | +export interface Import { |
| 13 | + /** Name of the imported symbol. */ |
| 14 | + symbolName: string; |
| 15 | + /** Module name from which the symbol has been imported. */ |
| 16 | + moduleName: string; |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +/** Resolves the import of the specified identifier. */ |
| 21 | +export function getImportOfIdentifier(node: ts.Identifier, typeChecker: ts.TypeChecker): Import| |
19 | 22 | null {
|
20 |
| - const symbol = typeChecker.getSymbolAtLocation(node); |
| 23 | + // Free standing identifiers which resolve to an import will be handled |
| 24 | + // as direct imports. e.g. "@Component()" where "Component" is an identifier |
| 25 | + // referring to an import specifier. |
| 26 | + const directImport = getSpecificImportOfIdentifier(node, typeChecker); |
| 27 | + if (directImport !== null) { |
| 28 | + return directImport; |
| 29 | + } else if (ts.isQualifiedName(node.parent) && node.parent.right === node) { |
| 30 | + // Determines the import of a qualified name. e.g. "let t: core.Component". In that |
| 31 | + // case, the import of the most left identifier will be determined ("core"). |
| 32 | + const qualifierRoot = getQualifiedNameRoot(node.parent); |
| 33 | + if (qualifierRoot) { |
| 34 | + const moduleName = getImportOfNamespacedIdentifier(qualifierRoot, typeChecker); |
| 35 | + if (moduleName) { |
| 36 | + return {moduleName, symbolName: node.text}; |
| 37 | + } |
| 38 | + } |
| 39 | + } else if (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) { |
| 40 | + // Determines the import of a property expression. e.g. "@core.Component". In that |
| 41 | + // case, the import of the most left identifier will be determined ("core"). |
| 42 | + const rootIdentifier = getPropertyAccessRoot(node.parent); |
| 43 | + if (rootIdentifier) { |
| 44 | + const moduleName = getImportOfNamespacedIdentifier(rootIdentifier, typeChecker); |
| 45 | + if (moduleName) { |
| 46 | + return {moduleName, symbolName: node.text}; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + return null; |
| 51 | +} |
21 | 52 |
|
| 53 | +/** |
| 54 | + * Resolves the import of the specified identifier. Expects the identifier to resolve |
| 55 | + * to a fine-grained import declaration with import specifiers. |
| 56 | + */ |
| 57 | +function getSpecificImportOfIdentifier(node: ts.Identifier, typeChecker: ts.TypeChecker): Import| |
| 58 | + null { |
| 59 | + const symbol = typeChecker.getSymbolAtLocation(node); |
22 | 60 | if (!symbol || !symbol.declarations || !symbol.declarations.length) {
|
23 | 61 | return null;
|
24 | 62 | }
|
25 |
| - |
26 |
| - const decl = symbol.declarations[0]; |
27 |
| - |
28 |
| - if (!ts.isImportSpecifier(decl)) { |
| 63 | + const declaration = symbol.declarations[0]; |
| 64 | + if (!ts.isImportSpecifier(declaration)) { |
29 | 65 | return null;
|
30 | 66 | }
|
31 |
| - |
32 |
| - // Since "decl" is an import specifier, we can walk up three times to get a reference |
| 67 | + // Since the declaration is an import specifier, we can walk up three times to get a reference |
33 | 68 | // to the import declaration node (NamedImports -> ImportClause -> ImportDeclaration).
|
34 |
| - const importDecl = decl.parent.parent.parent; |
35 |
| - |
| 69 | + const importDecl = declaration.parent.parent.parent; |
36 | 70 | if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
|
37 | 71 | return null;
|
38 | 72 | }
|
39 |
| - |
40 | 73 | return {
|
41 |
| - // Handles aliased imports: e.g. "import {Component as myComp} from ..."; |
42 |
| - name: decl.propertyName ? decl.propertyName.text : decl.name.text, |
43 |
| - importModule: importDecl.moduleSpecifier.text, |
44 |
| - node: importDecl |
| 74 | + moduleName: importDecl.moduleSpecifier.text, |
| 75 | + symbolName: declaration.propertyName ? declaration.propertyName.text : declaration.name.text |
45 | 76 | };
|
46 | 77 | }
|
| 78 | + |
| 79 | +/** |
| 80 | + * Resolves the import of the specified identifier. Expects the identifier to |
| 81 | + * resolve to a namespaced import declaration. e.g. "import * as core from ...". |
| 82 | + */ |
| 83 | +function getImportOfNamespacedIdentifier(node: ts.Identifier, typeChecker: ts.TypeChecker): string| |
| 84 | + null { |
| 85 | + const symbol = typeChecker.getSymbolAtLocation(node); |
| 86 | + if (!symbol || !symbol.declarations || !symbol.declarations.length) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + const declaration = symbol.declarations[0]; |
| 90 | + if (!ts.isNamespaceImport(declaration)) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + // Since the declaration is a namespace import, we can walk up three times to get a reference |
| 94 | + // to the import declaration node (NamespaceImport -> ImportClause -> ImportDeclaration). |
| 95 | + const importDecl = declaration.parent.parent; |
| 96 | + if (!ts.isStringLiteral(importDecl.moduleSpecifier)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + return importDecl.moduleSpecifier.text; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/** |
| 105 | + * Gets the root identifier of a qualified type chain. For example: "core.GestureConfig" |
| 106 | + * will return the "core" identifier. Allowing us to find the import of "core". |
| 107 | + */ |
| 108 | +function getQualifiedNameRoot(name: ts.QualifiedName): ts.Identifier|null { |
| 109 | + while (ts.isQualifiedName(name.left)) { |
| 110 | + name = name.left; |
| 111 | + } |
| 112 | + return ts.isIdentifier(name.left) ? name.left : null; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Gets the root identifier of a property access chain. For example: "core.GestureConfig" |
| 117 | + * will return the "core" identifier. Allowing us to find the import of "core". |
| 118 | + */ |
| 119 | +function getPropertyAccessRoot(node: ts.PropertyAccessExpression): ts.Identifier|null { |
| 120 | + while (ts.isPropertyAccessExpression(node.expression)) { |
| 121 | + node = node.expression; |
| 122 | + } |
| 123 | + return ts.isIdentifier(node.expression) ? node.expression : null; |
| 124 | +} |
0 commit comments