- Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathNonLinearPattern.ql
64 lines (58 loc) · 2.08 KB
/
NonLinearPattern.ql
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @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
classRootDestructuringPatternextendsDestructuringPattern{
RootDestructuringPattern(){
notthis=any(PropertyPatternp).getValuePattern()and
notthis=any(ArrayPatternp).getAnElement()
}
/** Holds if this pattern has multiple bindings for `name`. */
predicatehasConflictingBindings(stringname){
exists(VarRefv,VarRefw|
v=this.getABindingVarRef()and
w=this.getABindingVarRef()and
name=v.getName()and
name=w.getName()and
v!=w
)
}
/** Gets the first occurrence of the conflicting binding `name`. */
VarDeclgetFirstClobberedVarDecl(stringname){
this.hasConflictingBindings(name)and
result=
min(VarDecldecl|
decl=this.getABindingVarRef()anddecl.getName()=name
|
declorderbydecl.getLocation().getStartLine(),decl.getLocation().getStartColumn()
)
}
/** Holds if variables in this pattern may resemble type annotations. */
predicateresemblesTypeAnnotation(){
this.hasConflictingBindings(_)and// Restrict size of predicate.
thisinstanceofParameterand
thisinstanceofObjectPatternand
notexists(this.getTypeAnnotation())and
this.getFile().getFileType().isTypeScript()
}
}
fromRootDestructuringPatternp,stringn,VarDeclv,VarDeclw,stringmessage
where
v=p.getFirstClobberedVarDecl(n)and
w=p.getABindingVarRef()and
w.getName()=nand
v!=wand
ifp.resemblesTypeAnnotation()
thenmessage="The pattern variable '"+n+"' appears to be a type, but is a variable $@."
elsemessage="Repeated binding of pattern variable '"+n+"' $@."
selectw,message,v,"previously bound"