Skip to content

Commit 2035925

Browse files
committed
JS: Handle name resolution through dynamic imports
1 parent 8f27aa3 commit 2035925

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

javascript/ql/lib/semmle/javascript/internal/NameResolution.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ module NameResolution {
4646
this instanceof Module
4747
or
4848
this instanceof NamespaceDefinition // `module {}` or `enum {}` statement
49+
or
50+
// A module wrapped in a promise. We model this as a module exporting the actual module in a property called `$$promise-content`.
51+
this instanceof DynamicImportExpr
4952
}
5053
}
5154

@@ -232,6 +235,19 @@ module NameResolution {
232235
name = expr.getName() and
233236
node2 = expr
234237
)
238+
or
239+
exists(AwaitExpr await |
240+
node1 = await.getOperand() and
241+
name = "$$promise-content" and
242+
node2 = await
243+
)
244+
or
245+
exists(MethodCallExpr call |
246+
call.getMethodName() = "then" and
247+
node1 = call.getReceiver() and
248+
name = "$$promise-content" and
249+
node2 = call.getArgument(0).(Function).getParameter(0)
250+
)
235251
}
236252

237253
private signature module TypeResolutionInputSig {
@@ -334,6 +350,12 @@ module NameResolution {
334350
)
335351
or
336352
storeToVariable(result, name, mod.(Closure::ClosureModule).getExportsVariable())
353+
or
354+
exists(DynamicImportExpr imprt |
355+
mod = imprt and
356+
name = "$$promise-content" and
357+
result = imprt.getImportedPathExpr()
358+
)
337359
}
338360

339361
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
async function t1() {
22
const e = await import('./dynamicImportLib');
3-
e.getRequest(); // $ MISSING: hasUnderlyingType='express'.Request
3+
e.getRequest(); // $ hasUnderlyingType='express'.Request
44
}

javascript/ql/test/library-tests/UnderlyingTypes/test.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| contextualTypes.ts:27:16:27:18 | req | 'express'.Request |
1616
| contextualTypes.ts:34:20:34:22 | req | 'express'.Request |
1717
| contextualTypes.ts:41:16:41:18 | req | 'express'.Request |
18+
| dynamicImportUse.ts:3:5:3:18 | e.getRequest() | 'express'.Request |
1819
| expressBulkExport.use.ts:3:13:3:15 | req | 'express'.Request |
1920
| expressBulkExport.use.ts:6:13:6:15 | res | 'express'.Response |
2021
| expressExportAssign.use.ts:3:13:3:15 | req | 'express'.Request |

0 commit comments

Comments
 (0)