Skip to content

Commit 4fef131

Browse files
committed
evaluate null
fixes webpack#633
1 parent cc93080 commit 4fef131

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

lib/BasicEvaluatedExpression.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ function BasicEvaluatedExpression() {
77
}
88
module.exports = BasicEvaluatedExpression;
99

10+
BasicEvaluatedExpression.prototype.isNull = function() {
11+
return !!this.null;
12+
};
1013
BasicEvaluatedExpression.prototype.isString = function() {
1114
return Object.prototype.hasOwnProperty.call(this, "string");
1215
};
@@ -36,6 +39,7 @@ BasicEvaluatedExpression.prototype.isWrapped = function() {
3639
};
3740
BasicEvaluatedExpression.prototype.asBool = function() {
3841
if(this.isBoolean()) return this.bool;
42+
else if(this.isNull()) return false;
3943
else if(this.isString()) return !!this.string;
4044
else if(this.isNumber()) return !!this.number;
4145
else if(this.isRegExp()) return true;
@@ -48,6 +52,7 @@ BasicEvaluatedExpression.prototype.set = function(value) {
4852
if(typeof value === "string") return this.setString(value);
4953
if(typeof value === "number") return this.setNumber(value);
5054
if(typeof value === "boolean") return this.setBoolean(value);
55+
if(value === null) return this.setNull();
5156
if(value instanceof RegExp) return this.setRegExp(value);
5257
if(Array.isArray(value)) return this.setArray(value);
5358
return this;
@@ -59,6 +64,10 @@ BasicEvaluatedExpression.prototype.setString = function(str) {
5964
this.string = str;
6065
return this;
6166
};
67+
BasicEvaluatedExpression.prototype.setNull = function(str) {
68+
this.null = true;
69+
return this;
70+
};
6271
BasicEvaluatedExpression.prototype.setNumber = function(num) {
6372
if(num === null)
6473
delete this.number;

lib/Parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Parser.prototype.initializeEvaluating = function() {
3333
case "boolean":
3434
return new BasicEvaluatedExpression().setBoolean(expr.value).setRange(expr.range);
3535
}
36+
if(expr.value === null)
37+
return new BasicEvaluatedExpression().setNull().setRange(expr.range);
3638
if(expr.value instanceof RegExp)
3739
return new BasicEvaluatedExpression().setRegExp(expr.value).setRange(expr.range);
3840
});

test/cases/parsing/evaluate/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ it("should define DEBUG", function() {
55
var y = DEBUG ? require("fail") : require("./a");
66
});
77

8+
it("should evaluate null", function() {
9+
var y = null ? require("fail") : require("./a");
10+
if(null)
11+
require("fail");
12+
});
13+
814
it("should short-circut evaluating", function() {
915
var expr;
1016
var a = DEBUG && expr ? require("fail") : require("./a");

0 commit comments

Comments
 (0)