-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNonLinearPattern.ql
More file actions
48 lines (43 loc) · 1.55 KB
/
NonLinearPattern.ql
File metadata and controls
48 lines (43 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @name Non-linear pattern
* @description If the same pattern variable appears twice in an array or object pattern,
* the second binding will silently overwrite the first binding, which is probably
* unintentional.
* @kind problem
* @problem.severity error
* @id js/non-linear-pattern
* @tags reliability
* correctness
* language-features
* @precision very-high
*/
import javascript
class RootDestructuringPattern extends BindingPattern {
RootDestructuringPattern() {
this instanceof DestructuringPattern and
not this = any(PropertyPattern p).getValuePattern() and
not this = any(ArrayPattern p).getAnElement()
}
/** Holds if this pattern has multiple bindings for `name`. */
predicate hasConflictingBindings(string name) {
exists(VarRef v, VarRef w |
v = getABindingVarRef() and
w = getABindingVarRef() and
name = v.getName() and
name = w.getName() and
v != w
)
}
/** Gets the first occurrence of the conflicting binding `name`. */
VarDecl getFirstClobberedVarDecl(string name) {
hasConflictingBindings(name) and
result = min(VarDecl decl | decl = getABindingVarRef() and decl.getName() = name | decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn())
}
}
from RootDestructuringPattern p, string n, VarDecl v, VarDecl w
where
v = p.getFirstClobberedVarDecl(n) and
w = p.getABindingVarRef() and
w.getName() = n and
v != w
select w, "Repeated binding of pattern variable '" + n + "' previously bound $@.", v, "here"