Skip to content

Commit c3241a0

Browse files
asgerfCopilot
andcommitted
JS: Fix query regressions from parenthesized expression removal
- WhitespaceContradictsPrecedence: suppress false positives where parenthesized inner expressions explicitly clarify precedence - ExprHasNoEffect: continue flagging parenthesized anonymous function expressions as useless (they are valid code, not syntax errors) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c38bd1c commit c3241a0

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

javascript/ql/lib/Expressions/ExprHasNoEffect.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ predicate hasNoEffect(Expr e) {
172172
// from a syntax error we already flag
173173
not exists(FunctionExpr fe, ExprStmt es | fe = e |
174174
fe = es.getExpr() and
175-
not exists(fe.getName())
175+
not exists(fe.getName()) and
176+
not fe.isParenthesized()
176177
) and
177178
// exclude block-level flow type annotations. For example: `(name: empty)`.
178179
not (

javascript/ql/src/Expressions/WhitespaceContradictsPrecedence.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ predicate interestingNesting(BinaryExpr inner, BinaryExpr outer) {
8181
inner = outer.getAChildExpr() and
8282
not inner instanceof AssocNestedExpr and
8383
not inner instanceof HarmlessNestedExpr and
84+
not inner.isParenthesized() and
8485
not benignWhitespace(outer)
8586
}
8687

javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
| tst.js:61:2:61:20 | o.trivialNonGetter1 | This expression has no effect. |
1414
| tst.js:77:24:77:24 | o | This expression has no effect. |
1515
| uselessfn.js:1:2:1:26 | functio ... d.");\\n} | This expression has no effect. |
16+
| uselessfn.js:6:3:6:25 | functio ... d.");\\n} | This expression has no effect. |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
(function f() { // $ Alert
22
console.log("I'm never called.");
33
})
4+
5+
// anonymous function in parens - valid code but useless
6+
;(function() { // $ Alert
7+
console.log("Also never called.");
8+
})

javascript/ql/test/query-tests/Expressions/WhitespaceContradictsPrecedence/tst.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ x + x >> 1
5252

5353
// OK - asm.js-like
5454
x = x - 1|0;
55+
56+
// OK - parenthesized inner expression clarifies precedence
57+
function ok_paren1(x) {
58+
return (x + x)>>1;
59+
}
60+
61+
function ok_paren2(o, p) {
62+
return (p in o)&&o[p];
63+
}
64+
65+
function ok_paren3(x) {
66+
return (x + x) >> 1;
67+
}

0 commit comments

Comments
 (0)