TS: Resolve imports using TypeScript symbols

This commit is contained in:
Asger Feldthaus
2020-01-28 15:14:08 +00:00
parent abb95135c1
commit 9abf5f06e6
13 changed files with 98 additions and 11 deletions

View File

@@ -148,6 +148,16 @@ abstract class Import extends ASTNode {
)
}
/**
* Gets the imported module, as determined by the TypeScript compiler, if any.
*/
private Module resolveFromTypeScriptSymbol() {
exists(CanonicalName symbol |
ast_node_symbol(this, symbol) and
ast_node_symbol(result, symbol)
)
}
/**
* Gets the module this import refers to.
*
@@ -162,7 +172,8 @@ abstract class Import extends ASTNode {
else (
result = resolveAsProvidedModule() or
result = resolveImportedPath() or
result = resolveFromTypeRoot()
result = resolveFromTypeRoot() or
result = resolveFromTypeScriptSymbol()
)
}

View File

@@ -688,7 +688,7 @@ case @symbol.kind of
;
@type_with_symbol = @typereference | @typevariabletype | @typeoftype | @uniquesymboltype;
@ast_node_with_symbol = @typedefinition | @namespacedefinition | @toplevel | @typeaccess | @namespaceaccess | @vardecl | @function | @invokeexpr;
@ast_node_with_symbol = @typedefinition | @namespacedefinition | @toplevel | @typeaccess | @namespaceaccess | @vardecl | @function | @invokeexpr | @importdeclaration | @externalmodulereference;
ast_node_symbol(
unique int node: @ast_node_with_symbol ref,

View File

@@ -0,0 +1,11 @@
symbols
| src/lib/foo.ts:1:1:4:0 | <toplevel> | library-tests/TypeScript/PathMapping/src/lib/foo.ts |
| src/lib/foo.ts:1:8:3:1 | functio ... 123;\\n} | foo in library-tests/TypeScript/PathMapping/src/lib/foo.ts |
| test/test_foo.ts:1:1:1:28 | import ... @/foo"; | library-tests/TypeScript/PathMapping/src/lib/foo.ts |
| test/test_foo.ts:1:1:7:0 | <toplevel> | library-tests/TypeScript/PathMapping/test/test_foo.ts |
| test/test_foo.ts:2:17:2:32 | require("@/foo") | library-tests/TypeScript/PathMapping/src/lib/foo.ts |
| test/test_foo.ts:4:1:4:5 | foo() | foo in library-tests/TypeScript/PathMapping/src/lib/foo.ts |
| test/test_foo.ts:6:1:6:12 | foolib.foo() | foo in library-tests/TypeScript/PathMapping/src/lib/foo.ts |
#select
| test/test_foo.ts:1:1:1:28 | import ... @/foo"; | src/lib/foo.ts:1:1:4:0 | <toplevel> |
| test/test_foo.ts:2:17:2:32 | require("@/foo") | src/lib/foo.ts:1:1:4:0 | <toplevel> |

View File

@@ -0,0 +1,8 @@
import javascript
query predicate symbols(ASTNode astNode, CanonicalName symbol) {
ast_node_symbol(astNode, symbol)
}
from Import imprt
select imprt, imprt.getImportedModule()

View File

@@ -0,0 +1,3 @@
export function foo() {
return 123;
}

View File

@@ -0,0 +1,6 @@
import { foo } from "@/foo";
import foolib = require("@/foo");
foo();
foolib.foo();

View File

@@ -0,0 +1,9 @@
{
"include": ["."],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/lib/*"]
}
}
}