Skip to content

Commit 7091a9f

Browse files
committed
JS: Special-case alert message for type annotations
1 parent c8ab69a commit 7091a9f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

javascript/ql/src/LanguageFeatures/NonLinearPattern.ql

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,25 @@ class RootDestructuringPattern extends BindingPattern {
3737
hasConflictingBindings(name) and
3838
result = min(VarDecl decl | decl = getABindingVarRef() and decl.getName() = name | decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn())
3939
}
40+
41+
/** Holds if variables in this pattern may resemble type annotations. */
42+
predicate resemblesTypeAnnotation() {
43+
hasConflictingBindings(_) and // Restrict size of predicate.
44+
this instanceof Parameter and
45+
this instanceof ObjectPattern and
46+
not exists(getTypeAnnotation()) and
47+
getFile().getFileType().isTypeScript()
48+
}
4049
}
4150

42-
from RootDestructuringPattern p, string n, VarDecl v, VarDecl w
51+
from RootDestructuringPattern p, string n, VarDecl v, VarDecl w, string message
4352
where
4453
v = p.getFirstClobberedVarDecl(n) and
4554
w = p.getABindingVarRef() and
4655
w.getName() = n and
47-
v != w
48-
select w, "Repeated binding of pattern variable '" + n + "' previously bound $@.", v, "here"
56+
v != w and
57+
if p.resemblesTypeAnnotation() then
58+
message = "The pattern variable '" + n + "' appears to be a type, but is a variable previously bound $@."
59+
else
60+
message = "Repeated binding of pattern variable '" + n + "' previously bound $@."
61+
select w, message, v, "here"
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
| ts-test.ts:3:13:3:13 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:3:10:3:10 | x | here |
22
| ts-test.ts:8:16:8:16 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:8:10:8:10 | x | here |
33
| ts-test.ts:11:10:11:10 | x | Repeated binding of pattern variable 'x' previously bound $@. | ts-test.ts:11:7:11:7 | x | here |
4-
| 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 |
4+
| ts-test.ts:21:8:21:13 | string | The pattern variable 'string' appears to be a type, but is a variable 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:40:27:40:32 | string | Repeated binding of pattern variable 'string' previously bound $@. | ts-test.ts:40:16:40:21 | string | here |
78
| tst.js:3:13:3:13 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:3:10:3:10 | x | here |
89
| tst.js:8:16:8:16 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:8:10:8:10 | x | here |
910
| tst.js:11:10:11:10 | x | Repeated binding of pattern variable 'x' previously bound $@. | tst.js:11:7:11:7 | x | here |

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ function blah(arg) {
3636
}
3737
} = arg;
3838
}
39+
40+
function h({x: string, y: string}: any) { // NOT OK
41+
}

0 commit comments

Comments
 (0)