Skip to content

Commit 8ea9fd4

Browse files
author
Esben Sparre Andreasen
committed
JS: address review comments
1 parent 8b71b25 commit 8ea9fd4

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

javascript/ql/src/Expressions/HeterogeneousComparison.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ from ASTNode cmp,
199199
int leftTypeCount, int rightTypeCount ,
200200
string leftTypeDescription, string rightTypeDescription
201201
where isHeterogeneousComparison(cmp, left, right, leftTypes, rightTypes) and
202-
not exists (cmp.(Expr).flow().(DefensiveExpression).getTheTestResult()) and
202+
not exists (cmp.(Expr).flow().(DefensiveExpressionTest).getTheTestResult()) and
203203
not whitelist(left.asExpr()) and
204204
not whitelist(right.asExpr()) and
205205
leftExprDescription = capitalize(getDescription(left.asExpr(), "this expression")) and

javascript/ql/src/Expressions/UselessDefensiveProgramming.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import javascript
1515
import semmle.javascript.DefensiveProgramming
1616

17-
from DefensiveExpression e, boolean cv
17+
from DefensiveExpressionTest e, boolean cv
1818
where e.getTheTestResult() = cv and
1919
// whitelist
2020
not (

javascript/ql/src/Statements/UselessConditional.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ predicate isConstantDefensive(Expr e) {
8282
// traverse negations
8383
defensive.(LogNotExpr).getOperand+() = e
8484
|
85-
exists(defensive.flow().(DefensiveExpression).getTheTestResult())
85+
exists(defensive.flow().(DefensiveExpressionTest).getTheTestResult())
8686
)
8787
}
8888

javascript/ql/src/semmle/javascript/DefensiveProgramming.qll

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ private import semmle.javascript.dataflow.InferredTypes
88
/**
99
* A test in a defensive programming pattern.
1010
*/
11-
abstract class DefensiveExpression extends DataFlow::ValueNode {
11+
abstract class DefensiveExpressionTest extends DataFlow::ValueNode {
1212
/** Gets the unique Boolean value that this test evaluates to, if any. */
1313
abstract boolean getTheTestResult();
1414
}
1515

1616
/**
17-
* INTERNAL: Do not use directly; use `DefensiveExpression` instead.
17+
* INTERNAL: Do not use directly; use `DefensiveExpressionTest` instead.
1818
*/
1919
module Internal {
2020
/**
@@ -27,7 +27,7 @@ module Internal {
2727
* - the second `x` in `x = (x || e)`
2828
* - the second `x` in `var x = x || e`
2929
*/
30-
class DefensiveInit extends DefensiveExpression {
30+
class DefensiveInit extends DefensiveExpressionTest {
3131
DefensiveInit() {
3232
exists(VarAccess va, LogOrExpr o, VarRef va2 |
3333
va = astNode and
@@ -76,16 +76,14 @@ module Internal {
7676
/**
7777
* A dis- or conjunction that tests if an expression is `null` or `undefined` in either branch.
7878
*/
79-
private class CompositeUndefinedNullTestPart extends DefensiveExpression {
79+
private class CompositeUndefinedNullTestPart extends DefensiveExpressionTest {
8080

8181
UndefinedNullTest test;
8282

8383
boolean polarity;
8484

8585
CompositeUndefinedNullTestPart(){
86-
exists (BinaryExpr composite, Variable v, Expr op, Expr opOther, UndefinedNullTest testOther |
87-
composite instanceof LogAndExpr or
88-
composite instanceof LogOrExpr |
86+
exists (LogicalBinaryExpr composite, Variable v, Expr op, Expr opOther, UndefinedNullTest testOther |
8987
composite.hasOperands(op, opOther) and
9088
this = op.flow() and
9189
test = stripNotsAndParens(op, polarity) and
@@ -106,7 +104,7 @@ module Internal {
106104
/**
107105
* A test for `undefined` or `null` in an if-statement.
108106
*/
109-
private class SanityCheckingUndefinedNullGuard extends DefensiveExpression {
107+
private class SanityCheckingUndefinedNullGuard extends DefensiveExpressionTest {
110108

111109
UndefinedNullTest test;
112110

@@ -165,16 +163,20 @@ module Internal {
165163
result = getPolarity() and
166164
(
167165
if this instanceof StrictEqualityTest then
166+
// case: `operand === null` or `operand === undefined`
168167
operand.analyze().getTheType() = op2type
169168
else
169+
// case: `operand == null` or `operand == undefined`
170170
not isNotNullOrUndefined(operand.analyze().getAType())
171171
)
172172
or
173173
result = getPolarity().booleanNot() and
174174
(
175175
if this instanceof StrictEqualityTest then
176+
// case: `operand !== null` or `operand !== undefined`
176177
not operand.analyze().getAType() = op2type
177178
else
179+
// case: `operand != null` or `operand != undefined`
178180
not isNullOrUndefined(operand.analyze().getAType())
179181
)
180182
}
@@ -232,9 +234,8 @@ module Internal {
232234
* Gets the first expression that is guarded by `guard`.
233235
*/
234236
private Expr getAGuardedExpr(Expr guard) {
235-
exists(BinaryExpr op |
237+
exists(LogicalBinaryExpr op |
236238
op.getLeftOperand() = guard and
237-
(op instanceof LogAndExpr or op instanceof LogOrExpr) and
238239
op.getRightOperand() = result
239240
)
240241
or
@@ -262,7 +263,7 @@ module Internal {
262263
/**
263264
* A defensive expression that tests for `undefined` and `null` using a truthiness test.
264265
*/
265-
private class UndefinedNullTruthinessGuard extends DefensiveExpression {
266+
private class UndefinedNullTruthinessGuard extends DefensiveExpressionTest {
266267

267268
VarRef guardVar;
268269

@@ -293,7 +294,7 @@ module Internal {
293294
/**
294295
* A defensive expression that tests for `undefined` and `null`.
295296
*/
296-
private class UndefinedNullTypeGuard extends DefensiveExpression {
297+
private class UndefinedNullTypeGuard extends DefensiveExpressionTest {
297298

298299
UndefinedNullTest test;
299300

@@ -362,7 +363,7 @@ module Internal {
362363
/**
363364
* A defensive expression that tests if an expression has type `function`.
364365
*/
365-
private class FunctionTypeGuard extends DefensiveExpression {
366+
private class FunctionTypeGuard extends DefensiveExpressionTest {
366367

367368
TypeofTest test;
368369

0 commit comments

Comments
 (0)