Skip to content

Commit 4d186e0

Browse files
author
Max Schaefer
committed
JavaScript: Teach Unused{Variable,Parameter} to ignore variables with leading underscore.
1 parent a4b3b1e commit 4d186e0

File tree

7 files changed

+26
-5
lines changed

7 files changed

+26
-5
lines changed

change-notes/1.20/analysis-javascript.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| **Query** | **Expected impact** | **Change** |
2121
|--------------------------------------------|------------------------------|------------------------------------------------------------------------------|
2222
| Client-side cross-site scripting | More results | This rule now recognizes WinJS functions that are vulnerable to HTML injection. |
23-
| Unused variable, import, function or class | Fewer false-positive results | This rule now flags fewer variables that are implictly used by JSX elements. |
23+
| Unused parameter | Fewer false-positive results | This rule no longer flags parameters with leading underscore. |
24+
| Unused variable, import, function or class | Fewer false-positive results | This rule now flags fewer variables that are implictly used by JSX elements, and no longer flags variables with leading underscore. |
2425

2526
## Changes to QL libraries

javascript/ql/src/Declarations/UnusedParameter.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import javascript
12-
import UnusedParameter // local library
12+
import UnusedParameter
1313

1414
from Parameter p
1515
where isAnAccidentallyUnusedParameter(p)

javascript/ql/src/Declarations/UnusedParameter.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ predicate isUnused(Function f, Parameter p, Variable pv, int i) {
4646
// functions without a body cannot use their parameters
4747
f.hasBody() and
4848
// field parameters are used to initialize a field
49-
not p instanceof FieldParameter
49+
not p instanceof FieldParameter and
50+
// common convention: parameters with leading underscore are intentionally unused
51+
pv.getName().charAt(0) != "_"
5052
}
5153

5254
/**

javascript/ql/src/Declarations/UnusedVariable.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class UnusedLocal extends LocalVariable {
2222
not exists(FunctionExpr fe | this = fe.getVariable()) and
2323
not exists(ClassExpr ce | this = ce.getVariable()) and
2424
not exists(ExportDeclaration ed | ed.exportsAs(this, _)) and
25-
not exists(LocalVarTypeAccess type | type.getVariable() = this)
25+
not exists(LocalVarTypeAccess type | type.getVariable() = this) and
26+
// common convention: variables with leading underscore are intentionally unused
27+
getName().charAt(0) != "_"
2628
}
2729
}
2830

javascript/ql/test/query-tests/Declarations/UnusedParameter/tst.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ var g = f3;
3333
// OK
3434
define(function (require, exports, module) {
3535
module.x = 23;
36-
});
36+
});
37+
38+
// OK: starts with underscore
39+
function f(_p) {
40+
}

javascript/ql/test/query-tests/Declarations/UnusedVariable/UnusedVariable.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
| multi-imports.js:2:1:2:42 | import ... om 'x'; | Unused imports alphabetically, ordered. |
1010
| require-react-in-other-scope.js:2:9:2:13 | React | Unused variable React. |
1111
| typeoftype.ts:9:7:9:7 | y | Unused variable y. |
12+
| underscore.js:6:7:6:7 | e | Unused variable e. |
13+
| underscore.js:7:7:7:7 | f | Unused variable f. |
1214
| unusedShadowed.ts:1:1:1:26 | import ... where'; | Unused import T. |
1315
| unusedShadowed.ts:2:1:2:31 | import ... where'; | Unused import object. |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function f(a) {
2+
const [a, // OK: used
3+
_, // OK: starts with underscore
4+
_c, // OK: starts with underscore
5+
d, // OK: used
6+
e, // NOT OK
7+
f] // NOT OK
8+
= a;
9+
return a + d;
10+
}

0 commit comments

Comments
 (0)