Skip to content

Commit ec1722c

Browse files
author
Esben Sparre Andreasen
committed
JS: add utility SyntacticConstants::isNullOrUndefined
1 parent 7c7cd7c commit ec1722c

3 files changed

Lines changed: 22 additions & 21 deletions

File tree

javascript/ql/src/Declarations/DeadStoreOfLocal.ql

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,6 @@ predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
2525
not exists (SsaExplicitDefinition ssa | ssa.defines(vd, v))
2626
}
2727

28-
/**
29-
* Holds if `e` is an expression evaluating to `null` or `undefined`.
30-
*
31-
* This includes not only direct references to `null` and `undefined`, but
32-
* also `void` expressions and assignments of the form `x = rhs`, where `rhs`
33-
* is itself an expression evaluating to `null` or `undefined`.
34-
*/
35-
predicate isNullOrUndef(Expr e) {
36-
exists (Expr inner |
37-
inner = e.stripParens() |
38-
// `null` or `undefined`
39-
inner instanceof NullLiteral or
40-
inner.(VarAccess).getName() = "undefined" or
41-
inner instanceof VoidExpr or
42-
// recursive case to catch multi-assignments of the form `x = y = null`
43-
isNullOrUndef(inner.(AssignExpr).getRhs())
44-
)
45-
}
46-
4728
/**
4829
* Holds if `e` is an expression that may be used as a default initial value,
4930
* such as `0` or `-1`, or an empty object or array literal.
@@ -77,7 +58,7 @@ where deadStoreOfLocal(dead, v) and
7758
not fd = outer.getBody().(BlockStmt).getAStmt()
7859
) and
7960
// don't flag overwrites with `null` or `undefined`
80-
not isNullOrUndef(dead.getSource()) and
61+
not SyntacticConstants::isNullOrUndefined(dead.getSource()) and
8162
// don't flag default inits that are later overwritten
8263
not (isDefaultInit(dead.getSource()) and dead.isOverwritten(v)) and
8364
// don't flag assignments in externs

javascript/ql/src/semmle/javascript/Constants.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ module SyntacticConstants {
132132

133133
}
134134

135+
/**
136+
* Holds if `c` evaluates to `undefined`.
137+
*/
138+
predicate isUndefined(SyntacticConstant c) {
139+
c.getUnderlyingValue() instanceof UndefinedConstant
140+
}
141+
142+
/**
143+
* Holds if `c` evaluates to `null`.
144+
*/
145+
predicate isNull(SyntacticConstant c) {
146+
c.getUnderlyingValue() instanceof NullConstant
147+
}
148+
149+
/**
150+
* Holds if `c` evaluates to `null` or `undefined`.
151+
*/
152+
predicate isNullOrUndefined(SyntacticConstant c) {
153+
isUndefined(c) or isNull(c)
154+
}
155+
135156
}
136157

137158
/**

javascript/ql/test/library-tests/Constants/Constants.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
| tst.js:47:5:47:5 | 1 |
5959
| tst.js:48:1:48:7 | x.p = 1 |
6060
| tst.js:48:7:48:7 | 1 |
61-
| tst.js:49:1:49:6 | x += 1 |
6261
| tst.js:49:6:49:6 | 1 |
6362
| tst.ts:1:13:1:21 | <number>1 |
6463
| tst.ts:1:21:1:21 | 1 |

0 commit comments

Comments
 (0)