Skip to content

Commit 1d4dd2b

Browse files
authored
Merge pull request github#3057 from esbena/js/infer-this-as-exports
Approved by asgerf
2 parents 7e093a8 + 2fac743 commit 1d4dd2b

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ private class AnalyzedThisInBoundFunction extends AnalyzedThisExpr {
4545
}
4646
}
4747

48+
/**
49+
* Flow analysis for `this` expressions in node modules.
50+
*
51+
* These expressions are assumed to refer to the `module.exports` object.
52+
*/
53+
private class AnalyzedThisAsModuleExports extends DataFlow::AnalyzedNode, DataFlow::ThisNode {
54+
NodeModule m;
55+
56+
AnalyzedThisAsModuleExports() { m = getBindingContainer() }
57+
58+
override AbstractValue getALocalValue() { result = TAbstractExportsObject(m) }
59+
}
60+
4861
/**
4962
* Flow analysis for `this` expressions inside a function that is instantiated.
5063
*

javascript/ql/test/library-tests/ThisExpr/ThisExpr_analyzed.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
| module-exports.js:3:1:3:4 | this | file://:0:0:0:0 | indefinite value (call) |
2+
| module-exports.js:3:1:3:4 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
3+
| module-exports.js:4:28:4:31 | this | file://:0:0:0:0 | indefinite value (call) |
4+
| module-exports.js:4:28:4:31 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
5+
| module-exports.js:4:28:4:31 | this | module-exports.js:4:15:4:34 | instance of anonymous function |
6+
| module-exports.js:5:35:5:38 | this | file://:0:0:0:0 | indefinite value (call) |
7+
| module-exports.js:5:35:5:38 | this | file://:0:0:0:0 | indefinite value (heap) |
8+
| module-exports.js:5:35:5:38 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
9+
| module-exports.js:5:35:5:38 | this | module-exports.js:5:22:5:41 | instance of anonymous function |
10+
| module-exports.js:6:21:6:24 | this | file://:0:0:0:0 | indefinite value (call) |
11+
| module-exports.js:6:21:6:24 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
12+
| module-exports.js:7:28:7:31 | this | file://:0:0:0:0 | indefinite value (call) |
13+
| module-exports.js:7:28:7:31 | this | module-exports.js:1:1:8:0 | exports object of module module-exports |
114
| tst.js:4:9:4:12 | this | file://:0:0:0:0 | indefinite value (call) |
215
| tst.js:4:9:4:12 | this | tst.js:1:1:11:1 | instance of class C_normal |
316
| tst.js:8:15:8:18 | this | file://:0:0:0:0 | indefinite value (call) |

javascript/ql/test/library-tests/ThisExpr/getBasicBlock.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
missingBasicBlock
22
basicBlock
3+
| module-exports.js:1:1:1:0 | this | module-exports.js:1:1:1:0 | entry node of <toplevel> |
4+
| module-exports.js:4:15:4:14 | this | module-exports.js:4:15:4:14 | entry node of function() { this; } |
5+
| module-exports.js:5:22:5:21 | this | module-exports.js:5:22:5:21 | entry node of function() { this; } |
36
| tst.js:1:1:1:0 | this | tst.js:1:1:1:0 | entry node of <toplevel> |
47
| tst.js:1:16:1:15 | this | tst.js:1:16:1:15 | entry node of () {} |
58
| tst.js:3:7:3:6 | this | tst.js:3:7:3:6 | entry node of () {\\n ... ;\\n } |

javascript/ql/test/library-tests/ThisExpr/getBinder.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| module-exports.js:4:28:4:31 | this | module-exports.js:4:15:4:34 | function() { this; } |
2+
| module-exports.js:5:35:5:38 | this | module-exports.js:5:22:5:41 | function() { this; } |
13
| tst.js:4:9:4:12 | this | tst.js:3:7:5:5 | () {\\n ... ;\\n } |
24
| tst.js:8:15:8:18 | this | tst.js:7:7:9:5 | () {\\n ... ;\\n } |
35
| tst.js:17:13:17:16 | this | tst.js:16:10:18:9 | functio ... } |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var fs = require('fs');
2+
3+
this;
4+
exports.foo = function() { this; };
5+
module.exports.bar = function() { this; };
6+
exports.baz = () => this;
7+
module.exports.qux = () => this;

0 commit comments

Comments
 (0)