Skip to content

Commit db0015a

Browse files
committed
JS: Ignore ambience during extractor's variable resolution
Previously, ambient declarations were ignored during variable resolution in the extractor. However, this makes it difficult to implement name/type resolution in QL, since ambient declaration can affect type checking. Instead, the extractor's variable resolution no longer cares whether a given declaration is ambient. The original behaviour was not particularly important to preserve since 'declare' can only appear at the top-level or inside 'namespace' declarations.
1 parent d9f73db commit db0015a

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/ScopeManager.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,13 @@ public Void visit(Identifier nd, Void v) {
426426
// cases where we turn on the 'declKind' flags
427427
@Override
428428
public Void visit(FunctionDeclaration nd, Void v) {
429-
if (nd.hasDeclareKeyword() && !isInTypeScriptDeclarationFile()) return null;
430429
// strict mode functions are block-scoped, non-strict mode ones aren't
431430
if (blockscope == isStrict) visit(nd.getId(), DeclKind.var);
432431
return null;
433432
}
434433

435434
@Override
436435
public Void visit(ClassDeclaration nd, Void c) {
437-
if (nd.hasDeclareKeyword() && !isInTypeScriptDeclarationFile()) return null;
438436
if (blockscope) visit(nd.getClassDef().getId(), DeclKind.varAndType);
439437
return null;
440438
}
@@ -483,7 +481,6 @@ public Void visit(EnhancedForStatement nd, Void v) {
483481

484482
@Override
485483
public Void visit(VariableDeclaration nd, Void v) {
486-
if (nd.hasDeclareKeyword() && !isInTypeScriptDeclarationFile()) return null;
487484
// in block scoping mode, only process 'let'; in non-block scoping
488485
// mode, only process non-'let'
489486
if (blockscope == nd.isBlockScoped(ecmaVersion)) visit(nd.getDeclarations());
@@ -518,8 +515,7 @@ public Void visit(ClassBody nd, Void c) {
518515
@Override
519516
public Void visit(NamespaceDeclaration nd, Void c) {
520517
if (blockscope) return null;
521-
boolean isAmbientOutsideDtsFile = nd.hasDeclareKeyword() && !isInTypeScriptDeclarationFile();
522-
boolean hasVariable = nd.isInstantiated() && !isAmbientOutsideDtsFile;
518+
boolean hasVariable = nd.isInstantiated();
523519
visit(nd.getName(), hasVariable ? DeclKind.varAndNamespace : DeclKind.namespace);
524520
return null;
525521
}

javascript/ql/test/ApiGraphs/typed/NodeOfType.expected

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@ getANodeOfType
22
| body-parser | | index.ts:4:20:4:41 | require ... arser") |
33
| express | | index.ts:3:17:3:34 | require("express") |
44
| mongodb | | index.ts:1:8:1:19 | * as mongodb |
5+
| mongodb | AbstractCursor | index.ts:14:3:14:33 | getColl ... d: v }) |
6+
| mongodb | Collection | index.ts:14:3:14:17 | getCollection() |
7+
| mongodb | FindCursor | index.ts:14:3:14:33 | getColl ... d: v }) |
8+
| mongodb | TypedEventEmitter | index.ts:14:3:14:33 | getColl ... d: v }) |
59
| mongoose | | index.ts:17:8:17:20 | * as mongoose |
10+
| mongoose | AcceptsDiscriminator | index.ts:22:3:22:20 | getMongooseModel() |
11+
| mongoose | Document | index.ts:22:3:22:20 | getMongooseModel() |
12+
| mongoose | HydratedDocument | index.ts:22:3:22:20 | getMongooseModel() |
13+
| mongoose | Model | index.ts:22:3:22:20 | getMongooseModel() |
14+
| mongoose | Query | index.ts:22:3:22:20 | getMongooseModel() |
15+
| mongoose | Query | index.ts:22:3:22:36 | getMong ... d: v }) |
16+
| mongoose | Query | index.ts:23:3:23:20 | getMongooseQuery() |
17+
| mongoose | Query | index.ts:23:3:23:36 | getMong ... d: v }) |
18+
| mongoose | QueryWithHelpers | index.ts:22:3:22:36 | getMong ... d: v }) |
19+
| mongoose | QueryWithHelpers | index.ts:23:3:23:36 | getMong ... d: v }) |
20+
| mongoose | SessionOperation | index.ts:22:3:22:20 | getMongooseModel() |
21+
| mongoose | SessionOperation | index.ts:22:3:22:36 | getMong ... d: v }) |
22+
| mongoose | SessionOperation | index.ts:23:3:23:20 | getMongooseQuery() |
23+
| mongoose | SessionOperation | index.ts:23:3:23:36 | getMong ... d: v }) |
624
| puppeteer | | index.ts:26:8:26:21 | * as puppeteer |
725
| puppeteer | Browser | index.ts:30:22:30:33 | this.browser |
826
getANodeOfTypeRaw
927
| body-parser | | index.ts:4:20:4:41 | require ... arser") |
1028
| express | | index.ts:3:17:3:34 | require("express") |
1129
| mongodb | | index.ts:1:8:1:19 | * as mongodb |
30+
| mongodb | Collection | index.ts:14:3:14:17 | getCollection() |
1231
| mongoose | | index.ts:17:8:17:20 | * as mongoose |
32+
| mongoose | Model | index.ts:22:3:22:20 | getMongooseModel() |
33+
| mongoose | Query | index.ts:23:3:23:20 | getMongooseQuery() |
1334
| puppeteer | | index.ts:26:8:26:21 | * as puppeteer |
1435
| puppeteer | Browser | index.ts:30:22:30:33 | this.browser |
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
| index.ts:14:36:14:142 | // use= ... "find") | use moduleImport("mongodb").getMember("exports").getMember("Collection") has no outgoing edge labelled getInstance(); it has no outgoing edges at all. |
2-
| index.ts:22:39:22:157 | // def= ... eter(0) | use moduleImport("mongoose").getMember("exports").getMember("Model") has no outgoing edge labelled getInstance(); it has no outgoing edges at all. |
3-
| index.ts:23:39:23:157 | // def= ... eter(0) | use moduleImport("mongoose").getMember("exports").getMember("Query") has no outgoing edge labelled getInstance(); it has no outgoing edges at all. |

0 commit comments

Comments
 (0)