Skip to content

Commit 7f21f14

Browse files
authored
Merge pull request #678 from asger-semmle/function-receiver
Approved by xiemaisi
2 parents b11d5c5 + cb34934 commit 7f21f14

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

javascript/ql/src/semmle/javascript/dataflow/Nodes.qll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,25 @@ class FunctionNode extends DataFlow::ValueNode, DataFlow::DefaultSourceNode {
311311
Function getFunction() {
312312
result = astNode
313313
}
314+
315+
/**
316+
* Gets the function whose `this` binding a `this` expression in this function refers to,
317+
* which is the nearest enclosing non-arrow function.
318+
*/
319+
FunctionNode getThisBinder() {
320+
result.getFunction() = getFunction().getThisBinder()
321+
}
322+
323+
/**
324+
* Gets the dataflow node holding the value of the receiver passed to the given function.
325+
*
326+
* Has no result for arrow functions, as they ignore the receiver argument.
327+
*
328+
* To get the data flow node for `this` in an arrow function, consider using `getThisBinder().getReceiver()`.
329+
*/
330+
ThisNode getReceiver() {
331+
result.getBinder() = this
332+
}
314333
}
315334

316335
/** A data flow node corresponding to an object literal expression. */
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| tst.js:1:1:13:1 | functio ... \\n };\\n} | tst.js:1:1:1:0 | this |
2+
| tst.js:2:3:2:21 | function inner() {} | tst.js:2:3:2:2 | this |
3+
| tst.js:5:11:5:10 | () {} | tst.js:5:11:5:10 | this |
4+
| tst.js:6:11:6:15 | () {} | tst.js:6:11:6:10 | this |
5+
| tst.js:10:15:10:19 | () {} | tst.js:10:15:10:14 | this |
6+
| tst.js:11:15:11:20 | (x) {} | tst.js:11:15:11:14 | this |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from DataFlow::FunctionNode function
4+
select function, function.getReceiver()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function f() {
2+
function inner() {}
3+
let arrow = () => 5;
4+
5+
class C {
6+
method() {}
7+
};
8+
9+
let obj = {
10+
get getter() {},
11+
set setter(x) {}
12+
};
13+
}

0 commit comments

Comments
 (0)