mirror of
https://github.com/github/codeql.git
synced 2025-12-24 12:46:34 +01:00
TS: Resolve imports using TypeScript symbols
This commit is contained in:
@@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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> |
|
||||
@@ -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()
|
||||
@@ -0,0 +1,3 @@
|
||||
export function foo() {
|
||||
return 123;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { foo } from "@/foo";
|
||||
import foolib = require("@/foo");
|
||||
|
||||
foo();
|
||||
|
||||
foolib.foo();
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"include": ["."],
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/lib/*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user