Skip to content

Commit 13d1dab

Browse files
committed
[ConstPlugin] fix bug introduced by evaluation of && and ||
1 parent dd5e502 commit 13d1dab

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/ConstPlugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,15 @@ class ConstPlugin {
279279
(expression.operator === "||" && !bool);
280280

281281
if (param.isBoolean() || keepRight) {
282-
const dep = new ConstDependency(`${bool}`, param.range);
282+
// for case like
283+
//
284+
// return'development'===process.env.NODE_ENV&&'foo'
285+
//
286+
// we need a space before the bool to prevent result like
287+
//
288+
// returnfalse&&'foo'
289+
//
290+
const dep = new ConstDependency(` ${bool}`, param.range);
283291
dep.loc = expression.loc;
284292
parser.state.current.addDependency(dep);
285293
} else {

test/cases/parsing/evaluate/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ it("should evaluate logical expression", function() {
1111
var value4 = typeof require !== "function" && require("fail");
1212
var value5 = "hello" && (() => "value5")();
1313
var value6 = "" || (() => "value6")();
14+
var value7 = (function () { return'value7'===typeof 'value7'&&'value7'})();
15+
1416
expect(value1).toBe("hello");
1517
expect(value2).toBe(true);
1618
expect(value3).toBe("");
1719
expect(value4).toBe(false);
1820
expect(value5).toBe("value5");
1921
expect(value6).toBe("value6");
22+
expect(value7).toBe(false);
2023
});
2124

2225
if("shouldn't evaluate expression", function() {

0 commit comments

Comments
 (0)