Skip to content

Commit

Permalink
[ConstPlugin] fix bug introduced by evaluation of && and ||
Browse files Browse the repository at this point in the history
  • Loading branch information
ljqx committed Oct 25, 2018
1 parent dd5e502 commit 13d1dab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ConstPlugin.js
Expand Up @@ -279,7 +279,15 @@ class ConstPlugin {
(expression.operator === "||" && !bool);

if (param.isBoolean() || keepRight) {
const dep = new ConstDependency(`${bool}`, param.range);
// for case like
//
// return'development'===process.env.NODE_ENV&&'foo'
//
// we need a space before the bool to prevent result like
//
// returnfalse&&'foo'
//
const dep = new ConstDependency(` ${bool}`, param.range);
dep.loc = expression.loc;
parser.state.current.addDependency(dep);
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/cases/parsing/evaluate/index.js
Expand Up @@ -11,12 +11,15 @@ it("should evaluate logical expression", function() {
var value4 = typeof require !== "function" && require("fail");
var value5 = "hello" && (() => "value5")();
var value6 = "" || (() => "value6")();
var value7 = (function () { return'value7'===typeof 'value7'&&'value7'})();

expect(value1).toBe("hello");
expect(value2).toBe(true);
expect(value3).toBe("");
expect(value4).toBe(false);
expect(value5).toBe("value5");
expect(value6).toBe("value6");
expect(value7).toBe(false);
});

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

0 comments on commit 13d1dab

Please sign in to comment.