Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cacb8fd

Browse files
author
Esben Sparre Andreasen
committed
JS: move DeadStoreOfLocal::isDefaultInit to separate module
1 parent 28f3b68 commit cacb8fd

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Provides classes and predicates for reasoning about dead stores.
3+
*/
4+
5+
import javascript
6+
7+
/**
8+
* Holds if `e` is an expression that may be used as a default initial value,
9+
* such as `0` or `-1`, or an empty object or array literal.
10+
*/
11+
predicate isDefaultInit(Expr e) {
12+
// primitive default values: zero, false, empty string, and (integer) -1
13+
e.(NumberLiteral).getValue().toFloat() = 0.0 or
14+
e.(NegExpr).getOperand().(NumberLiteral).getValue() = "1" or
15+
e.(ConstantString).getStringValue() = "" or
16+
e.(BooleanLiteral).getValue() = "false" or
17+
// initialising to an empty array or object literal, even if unnecessary,
18+
// can convey useful type information to the reader
19+
e.(ArrayExpr).getSize() = 0 or
20+
e.(ObjectExpr).getNumProperty() = 0 or
21+
SyntacticConstants::isNullOrUndefined(e)
22+
}

javascript/ql/src/Declarations/DeadStoreOfLocal.ql

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import javascript
14+
import DeadStore
1415

1516
/**
1617
* Holds if `vd` is a definition of variable `v` that is dead, that is,
@@ -25,22 +26,6 @@ predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
2526
not exists (SsaExplicitDefinition ssa | ssa.defines(vd, v))
2627
}
2728

28-
/**
29-
* Holds if `e` is an expression that may be used as a default initial value,
30-
* such as `0` or `-1`, or an empty object or array literal.
31-
*/
32-
predicate isDefaultInit(Expr e) {
33-
// primitive default values: zero, false, empty string, and (integer) -1
34-
e.(NumberLiteral).getValue().toFloat() = 0.0 or
35-
e.(NegExpr).getOperand().(NumberLiteral).getValue() = "1" or
36-
e.(ConstantString).getStringValue() = "" or
37-
e.(BooleanLiteral).getValue() = "false" or
38-
// initialising to an empty array or object literal, even if unnecessary,
39-
// can convey useful type information to the reader
40-
e.(ArrayExpr).getSize() = 0 or
41-
e.(ObjectExpr).getNumProperty() = 0
42-
}
43-
4429
from VarDef dead, PurelyLocalVariable v // captured variables may be read by closures, so don't flag them
4530
where deadStoreOfLocal(dead, v) and
4631
// the variable should be accessed somewhere; otherwise it will be flagged by UnusedVariable

0 commit comments

Comments
 (0)