Skip to content

Commit f00863f

Browse files
authored
Merge pull request #383 from esben-semmle/js/unused-eval-variable
Approved by xiemaisi
2 parents 28f3b68 + eb7add6 commit f00863f

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

change-notes/1.19/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
| Remote property injection | Fewer results | The precision of this rule has been revised to "medium". Results are no longer shown on LGTM by default. |
3636
| Missing CSRF middleware | Fewer false-positive results | This rule now recognizes additional CSRF protection middlewares. |
3737
| Server-side URL redirect | More results | This rule now recognizes redirection calls in more cases. |
38+
| Unused variable, import, function or class | Fewer false-positive results | This rule now flags fewer variables that may be used by `eval` calls. |
3839
| Unused variable, import, function or class | Fewer results | This rule now flags import statements with multiple unused imports once. |
3940
| User-controlled bypass of security check | Fewer results | This rule no longer flags conditions that guard early returns. The precision of this rule has been revised to "medium". Results are no longer shown on LGTM by default. |
4041
| Whitespace contradicts operator precedence | Fewer false-positive results | This rule no longer flags operators with asymmetric whitespace. |

javascript/ql/src/Declarations/UnusedVariable.ql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ predicate whitelisted(UnusedLocal v) {
163163
isEnumMember(vd) or
164164
// ignore ambient declarations - too noisy
165165
vd.isAmbient()
166+
) or
167+
exists (DirectEval eval |
168+
// eval nearby
169+
eval.getEnclosingFunction() = v.getADeclaration().getEnclosingFunction() and
170+
// ... but not on the RHS
171+
not v.getAnAssignedExpr() = eval
166172
)
167173
}
168174

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
| Babelrc/importPragma.jsx:2:1:2:27 | import ... react'; | Unused import q. |
22
| decorated.ts:1:1:1:126 | import ... where'; | Unused import actionHandler. |
33
| decorated.ts:4:10:4:12 | fun | Unused function fun. |
4+
| eval.js:10:9:10:24 | not_used_by_eval | Unused variable not_used_by_eval. |
5+
| eval.js:19:9:19:24 | not_used_by_eval | Unused variable not_used_by_eval. |
46
| externs.js:6:5:6:13 | iAmUnused | Unused variable iAmUnused. |
57
| importWithoutPragma.jsx:1:1:1:27 | import ... react'; | Unused import h. |
68
| multi-imports.js:1:1:1:29 | import ... om 'x'; | Unused imports a, b, d. |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function(){
2+
var used_by_eval = f();
3+
eval(src);
4+
});
5+
(function(){
6+
eval(src);
7+
var used_by_eval = f();
8+
});
9+
(function(){
10+
var not_used_by_eval = f();
11+
(function(){
12+
eval(src);
13+
})
14+
});
15+
(function(){
16+
(function(){
17+
eval(src);
18+
})
19+
var not_used_by_eval = f();
20+
});

0 commit comments

Comments
 (0)