Skip to content

Commit 627583f

Browse files
author
Max Schaefer
committed
JavaScript: Refactor UselessConditional for performance.
1 parent 8b8b352 commit 627583f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

javascript/ql/src/Statements/UselessConditional.ql

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ import semmle.javascript.RestrictedLocations
1717
import semmle.javascript.dataflow.Refinements
1818
import semmle.javascript.DefensiveProgramming
1919

20+
/**
21+
* Gets the unique definition of `v`.
22+
*
23+
* If `v` has no definitions or more than one, or if its single definition
24+
* is a destructuring assignment, this predicate is undefined.
25+
*/
26+
VarDef getSingleDef(Variable v) {
27+
strictcount(VarDef vd | vd.getAVariable() = v) = 1 and
28+
result.getTarget() = v.getAReference()
29+
}
30+
2031
/**
2132
* Holds if variable `v` looks like a symbolic constant, that is, it is assigned
2233
* exactly once, either in a `const` declaration or with a constant initializer.
2334
*
2435
* We do not consider conditionals to be useless if they check a symbolic constant.
2536
*/
2637
predicate isSymbolicConstant(Variable v) {
27-
// defined exactly once
28-
count(VarDef vd | vd.getAVariable() = v) = 1 and
29-
// the definition is either a `const` declaration or it assigns a constant to it
30-
exists(VarDef vd | vd.getAVariable() = v and count(vd.getAVariable()) = 1 |
31-
vd.(VariableDeclarator).getDeclStmt() instanceof ConstDeclStmt or
38+
exists(VarDef vd | vd = getSingleDef(v) |
39+
vd.(VariableDeclarator).getDeclStmt() instanceof ConstDeclStmt
40+
or
3241
isConstant(vd.getSource())
3342
)
3443
}

0 commit comments

Comments
 (0)