Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96b6c83

Browse files
committed
JS: Tests and fixes for PartialInvokeNode
1 parent 3dcb134 commit 96b6c83

5 files changed

Lines changed: 72 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,13 @@ module PartialInvokeNode {
10541054
* A partial call through the built-in `Function.prototype.bind`.
10551055
*/
10561056
private class BindPartialCall extends PartialInvokeNode::Range, DataFlow::MethodCallNode {
1057-
BindPartialCall() { getMethodName() = "bind" }
1057+
BindPartialCall() {
1058+
getMethodName() = "bind" and
1059+
1060+
// Avoid overlap with angular.bind and goog.bind
1061+
not this = AngularJS::angular().getAMethodCall() and
1062+
not getReceiver().accessesGlobal("goog")
1063+
}
10581064

10591065
override predicate isPartialArgument(DataFlow::Node callback, DataFlow::Node argument, int index) {
10601066
index >= 0 and
@@ -1086,7 +1092,7 @@ module PartialInvokeNode {
10861092
}
10871093

10881094
override DataFlow::SourceNode getBoundFunction(DataFlow::Node callback, int boundArgs) {
1089-
callback = getReceiver() and
1095+
callback = getArgument(0) and
10901096
boundArgs = getNumArgument() - 1 and
10911097
result = this
10921098
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
goog.module('test');
2+
3+
function test(x) {
4+
addEventListener("click", goog.bind(function(x, event) {}, this, x));
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
getBoundFunction
2+
| closure.js:4:29:4:69 | goog.bi ... his, x) | closure.js:4:39:4:59 | functio ... ent) {} | 1 | closure.js:4:29:4:69 | goog.bi ... his, x) |
3+
| tst.js:5:29:5:63 | functio ... his, x) | tst.js:5:29:5:49 | functio ... ent) {} | 1 | tst.js:5:29:5:63 | functio ... his, x) |
4+
| tst.js:6:29:6:68 | lodash. ... {}, x) | tst.js:6:44:6:64 | functio ... ent) {} | 1 | tst.js:6:29:6:68 | lodash. ... {}, x) |
5+
| tst.js:7:29:7:65 | R.parti ... }, [x]) | tst.js:7:39:7:59 | functio ... ent) {} | 1 | tst.js:7:29:7:65 | R.parti ... }, [x]) |
6+
| tst.js:8:29:8:72 | angular ... {}, x) | tst.js:8:48:8:68 | functio ... ent) {} | 1 | tst.js:8:29:8:72 | angular ... {}, x) |
7+
| tst.js:11:29:11:43 | f.bind(this, x) | tst.js:11:29:11:29 | f | 1 | tst.js:11:29:11:43 | f.bind(this, x) |
8+
isPartialArgument
9+
| closure.js:4:29:4:69 | goog.bi ... his, x) | closure.js:4:39:4:59 | functio ... ent) {} | closure.js:4:68:4:68 | x | 0 |
10+
| tst.js:5:29:5:63 | functio ... his, x) | tst.js:5:29:5:49 | functio ... ent) {} | tst.js:5:62:5:62 | x | 0 |
11+
| tst.js:6:29:6:68 | lodash. ... {}, x) | tst.js:6:44:6:64 | functio ... ent) {} | tst.js:6:67:6:67 | x | 0 |
12+
| tst.js:7:29:7:65 | R.parti ... }, [x]) | tst.js:7:39:7:59 | functio ... ent) {} | tst.js:7:63:7:63 | x | 0 |
13+
| tst.js:8:29:8:72 | angular ... {}, x) | tst.js:8:48:8:68 | functio ... ent) {} | tst.js:8:71:8:71 | x | 0 |
14+
| tst.js:11:29:11:43 | f.bind(this, x) | tst.js:11:29:11:29 | f | tst.js:11:42:11:42 | x | 0 |
15+
getBoundReceiver
16+
| closure.js:4:29:4:69 | goog.bi ... his, x) | closure.js:4:62:4:65 | this |
17+
| tst.js:5:29:5:63 | functio ... his, x) | tst.js:5:56:5:59 | this |
18+
| tst.js:8:29:8:72 | angular ... {}, x) | tst.js:8:42:8:45 | this |
19+
| tst.js:11:29:11:43 | f.bind(this, x) | tst.js:11:36:11:39 | this |
20+
clickEvent
21+
| closure.js:4:51:4:55 | event |
22+
| tst.js:5:41:5:45 | event |
23+
| tst.js:6:56:6:60 | event |
24+
| tst.js:7:51:7:55 | event |
25+
| tst.js:8:60:8:64 | event |
26+
| tst.js:10:17:10:21 | event |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import javascript
2+
3+
query
4+
DataFlow::Node getBoundFunction(DataFlow::PartialInvokeNode invoke, DataFlow::Node callback, int boundArgs) {
5+
result = invoke.getBoundFunction(callback, boundArgs)
6+
}
7+
8+
query
9+
predicate isPartialArgument(DataFlow::PartialInvokeNode invoke, DataFlow::Node callback, DataFlow::Node argument, int index) {
10+
invoke.isPartialArgument(callback, argument, index)
11+
}
12+
13+
query
14+
DataFlow::Node getBoundReceiver(DataFlow::PartialInvokeNode invoke) {
15+
result = invoke.getBoundReceiver()
16+
}
17+
18+
query
19+
DataFlow::Node clickEvent() {
20+
result = DataFlow::globalVarRef("addEventListener").getACall().getABoundCallbackParameter(1, 0)
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let lodash = require('lodash');
2+
let R = require('ramda');
3+
4+
function test(x) {
5+
addEventListener("click", function(x, event) {}.bind(this, x));
6+
addEventListener("click", lodash.partial(function(x, event) {}, x));
7+
addEventListener("click", R.partial(function(x, event) {}, [x]));
8+
addEventListener("click", angular.bind(this, function(x, event) {}, x));
9+
10+
function f(x, event) {}
11+
addEventListener("click", f.bind(this, x));
12+
}

0 commit comments

Comments
 (0)