@@ -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 */
1919module 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