Skip to content

Commit c8ab69a

Browse files
committed
JS: Avoid duplicate alerts
1 parent f380898 commit c8ab69a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

javascript/ql/src/LanguageFeatures/NonLinearPattern.ql

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,35 @@
1414

1515
import javascript
1616

17-
from BindingPattern p, string n, VarDecl v, VarDecl w
17+
class RootDestructuringPattern extends BindingPattern {
18+
RootDestructuringPattern() {
19+
this instanceof DestructuringPattern and
20+
not this = any(PropertyPattern p).getValuePattern() and
21+
not this = any(ArrayPattern p).getAnElement()
22+
}
23+
24+
/** Holds if this pattern has multiple bindings for `name`. */
25+
predicate hasConflictingBindings(string name) {
26+
exists(VarRef v, VarRef w |
27+
v = getABindingVarRef() and
28+
w = getABindingVarRef() and
29+
name = v.getName() and
30+
name = w.getName() and
31+
v != w
32+
)
33+
}
34+
35+
/** Gets the first occurrence of the conflicting binding `name`. */
36+
VarDecl getFirstClobberedVarDecl(string name) {
37+
hasConflictingBindings(name) and
38+
result = min(VarDecl decl | decl = getABindingVarRef() and decl.getName() = name | decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn())
39+
}
40+
}
41+
42+
from RootDestructuringPattern p, string n, VarDecl v, VarDecl w
1843
where
19-
v = p.getABindingVarRef() and
44+
v = p.getFirstClobberedVarDecl(n) and
2045
w = p.getABindingVarRef() and
21-
v.getName() = n and
2246
w.getName() = n and
23-
v != w and
24-
v.getLocation().startsBefore(w.getLocation())
47+
v != w
2548
select w, "Repeated binding of pattern variable '" + n + "' previously bound $@.", v, "here"

javascript/ql/test/query-tests/LanguageFeatures/NonLinearPattern/NonLinearPattern.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
| ts-test.ts:21:8:21:13 | string | Repeated binding of pattern variable 'string' previously bound $@. | ts-test.ts:20:8:20:13 | string | here |
55
| ts-test.ts:32:16:32:16 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:30:12:30:12 | x | here |
66
| ts-test.ts:34:20:34:20 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:30:12:30:12 | x | here |
7-
| ts-test.ts:34:20:34:20 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:32:16:32:16 | x | here |
87
| tst.js:3:13:3:13 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:3:10:3:10 | x | here |
98
| tst.js:8:16:8:16 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:8:10:8:10 | x | here |
109
| tst.js:11:10:11:10 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:11:7:11:7 | x | here |

0 commit comments

Comments
 (0)