Skip to content

Commit 301dab0

Browse files
author
Max Schaefer
committed
JavaScript: Improve AMD support in type inference.
Now leverages the recently introduced logic for resolving AMD imports based on unique matching paths.
1 parent 5101a5b commit 301dab0

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

javascript/ql/src/semmle/javascript/dataflow/internal/InterModuleTypeInference.qll

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,18 @@ private class AnalyzedAmdImport extends AnalyzedPropertyRead, DataFlow::Node {
248248
Module required;
249249

250250
AnalyzedAmdImport() {
251-
exists(AmdModule amd, PathExpr dep, Parameter p |
252-
amd.getDefine().dependencyParameter(dep, p) and
253-
this = DataFlow::parameterNode(p) and
254-
required.getFile() = amd.resolve(dep)
251+
exists(AmdModule amd, PathExpr dep |
252+
exists(Parameter p |
253+
amd.getDefine().dependencyParameter(dep, p) and
254+
this = DataFlow::parameterNode(p)
255+
)
256+
or
257+
exists(CallExpr requireCall |
258+
requireCall = amd.getDefine().getARequireCall() and
259+
dep = requireCall.getAnArgument() and
260+
this = requireCall.flow()
261+
) |
262+
required = dep.(Import).getImportedModule()
255263
)
256264
}
257265

javascript/ql/test/library-tests/Flow/AbstractValues.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
| amd3.js:1:1:5:0 | module object of module amd3 |
2020
| amd3.js:1:24:4:1 | anonymous function |
2121
| amd3.js:1:24:4:1 | instance of anonymous function |
22+
| amd4.js:1:1:4:0 | exports object of module amd4 |
23+
| amd4.js:1:1:4:0 | module object of module amd4 |
24+
| amd4.js:1:28:3:1 | anonymous function |
25+
| amd4.js:1:28:3:1 | instance of anonymous function |
26+
| amd5.js:1:1:4:0 | exports object of module amd5 |
27+
| amd5.js:1:1:4:0 | module object of module amd5 |
28+
| amd5.js:1:21:3:1 | anonymous function |
29+
| amd5.js:1:21:3:1 | instance of anonymous function |
2230
| amd.js:1:1:7:0 | exports object of module amd |
2331
| amd.js:1:1:7:0 | module object of module amd |
2432
| amd.js:1:31:6:1 | anonymous function |

javascript/ql/test/library-tests/Flow/abseval.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
| amd3.js:3:7:3:8 | _c | amd3.js:3:12:3:12 | c | c.js:1:1:7:0 | exports object of module c |
1313
| amd3.js:3:7:3:8 | _c | amd3.js:3:12:3:12 | c | c.js:1:18:1:19 | object literal |
1414
| amd3.js:3:7:3:8 | _c | amd3.js:3:12:3:12 | c | file://:0:0:0:0 | indefinite value (call) |
15+
| amd4.js:2:7:2:8 | _u | amd4.js:2:12:2:12 | u | file://:0:0:0:0 | indefinite value (call) |
16+
| amd4.js:2:7:2:8 | _u | amd4.js:2:12:2:12 | u | file://:0:0:0:0 | non-empty, non-numeric string |
17+
| amd4.js:2:7:2:8 | _u | amd4.js:2:12:2:12 | u | reexport/lib/src/utils/util.js:1:1:3:0 | exports object of module util |
18+
| amd5.js:2:7:2:8 | _u | amd5.js:2:12:2:32 | req('sr ... /util') | file://:0:0:0:0 | indefinite value (call) |
19+
| amd5.js:2:7:2:8 | _u | amd5.js:2:12:2:32 | req('sr ... /util') | file://:0:0:0:0 | non-empty, non-numeric string |
20+
| amd5.js:2:7:2:8 | _u | amd5.js:2:12:2:32 | req('sr ... /util') | reexport/lib/src/utils/util.js:1:1:3:0 | exports object of module util |
1521
| amd.js:2:7:2:7 | m | amd.js:2:11:2:13 | mod | amd.js:1:1:7:0 | module object of module amd |
1622
| amd.js:2:7:2:7 | m | amd.js:2:11:2:13 | mod | file://:0:0:0:0 | indefinite value (call) |
1723
| amd.js:3:7:3:7 | e | amd.js:3:11:3:13 | exp | amd.js:1:1:7:0 | exports object of module amd |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define(['src/utils/util'], function(u) {
2+
var _u = u;
3+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define(['require'], function(req) {
2+
var _u = req('src/utils/util');
3+
});

javascript/ql/test/library-tests/Flow/getAPrototype.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
| a.js:15:1:17:1 | instance of function bump | a.js:15:1:17:1 | instance of function bump |
44
| amd2.js:1:8:3:1 | instance of anonymous function | amd2.js:1:8:3:1 | instance of anonymous function |
55
| amd3.js:1:24:4:1 | instance of anonymous function | amd3.js:1:24:4:1 | instance of anonymous function |
6+
| amd4.js:1:28:3:1 | instance of anonymous function | amd4.js:1:28:3:1 | instance of anonymous function |
7+
| amd5.js:1:21:3:1 | instance of anonymous function | amd5.js:1:21:3:1 | instance of anonymous function |
68
| amd.js:1:31:6:1 | instance of anonymous function | amd.js:1:31:6:1 | instance of anonymous function |
79
| arguments.js:1:2:3:1 | instance of anonymous function | arguments.js:1:2:3:1 | instance of anonymous function |
810
| arguments.js:5:2:8:1 | instance of anonymous function | arguments.js:5:2:8:1 | instance of anonymous function |

javascript/ql/test/library-tests/Flow/types.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
| a.js:14:12:14:24 | notAlwaysZero | a.js:14:28:14:28 | 0 | number |
66
| amd3.js:2:7:2:8 | _a | amd3.js:2:12:2:12 | a | boolean, class, date, function, null, number, object, regular expression,string or undefined |
77
| amd3.js:3:7:3:8 | _c | amd3.js:3:12:3:12 | c | boolean, class, date, function, null, number, object, regular expression,string or undefined |
8+
| amd4.js:2:7:2:8 | _u | amd4.js:2:12:2:12 | u | boolean, class, date, function, null, number, object, regular expression,string or undefined |
9+
| amd5.js:2:7:2:8 | _u | amd5.js:2:12:2:32 | req('sr ... /util') | boolean, class, date, function, null, number, object, regular expression,string or undefined |
810
| amd.js:2:7:2:7 | m | amd.js:2:11:2:13 | mod | boolean, class, date, function, null, number, object, regular expression,string or undefined |
911
| amd.js:3:7:3:7 | e | amd.js:3:11:3:13 | exp | boolean, class, date, function, null, number, object, regular expression,string or undefined |
1012
| arguments.js:2:7:2:7 | y | arguments.js:2:11:2:11 | x | number |

0 commit comments

Comments
 (0)