Skip to content

Commit 712a216

Browse files
author
Max Schaefer
committed
Add self-verifying type-tracking tests.
1 parent 941177e commit 712a216

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| client2.js:3:6:3:16 | // track: f | Failed to track f here. |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import javascript
2+
3+
/** Gets a node to which the source node annotated with `name` is tracked under state `t`. */
4+
DataFlow::SourceNode trackNamedNode(DataFlow::TypeTracker t, string name) {
5+
t.start() and
6+
exists(Comment c, string f, int l |
7+
f = c.getFile().getAbsolutePath() and
8+
l = c.getLocation().getStartLine() and
9+
result.hasLocationInfo(f, l, _, _, _) and
10+
name = c.getText().regexpFind("(?<=name: )\\S+", _, _)
11+
)
12+
or
13+
exists(DataFlow::TypeTracker t2 | result = trackNamedNode(t2, name).track(t2, t))
14+
}
15+
16+
/** Holds if `name` is tracked to expression `e` starting on line `l` of file `f`. */
17+
predicate actual(Expr e, File f, int l, string name) {
18+
trackNamedNode(DataFlow::TypeTracker::end(), name).flowsToExpr(e) and
19+
f = e.getFile() and
20+
l = e.getLocation().getStartLine()
21+
}
22+
23+
/**
24+
* Holds if there is an annotation comment expecting `name` to be tracked to an expression
25+
* on line `l` of file `f`.
26+
*/
27+
predicate expected(Comment c, File f, int l, string name) {
28+
f = c.getFile() and
29+
l = c.getLocation().getStartLine() and
30+
name = c.getText().regexpFind("(?<=track: )\\S+", _, _)
31+
}
32+
33+
from Locatable loc, File f, int l, string name, string msg
34+
where
35+
expected(loc, f, l, name) and
36+
not actual(_, f, l, name) and
37+
msg = "Failed to track " + name + " here."
38+
or
39+
actual(loc, f, l, name) and
40+
not expected(_, f, l, name) and
41+
expected(_, f, l, _) and
42+
msg = "Unexpectedly tracked " + name + " here."
43+
select loc, msg
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const g = require("./deprecated");
2+
3+
g(); // track: f
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const util = require("util");
2+
3+
function f() {} // name: f
4+
5+
module.exports = util.deprecate(f, "don't use this function");

0 commit comments

Comments
 (0)