Skip to content

Commit

Permalink
Deopt evaluation of undefined with a local binding. Fix #5204 (#5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi authored and existentialism committed May 19, 2017
1 parent c89a22e commit d92c745
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/evaluation.js
Expand Up @@ -176,11 +176,11 @@ export function evaluate(): { confident: boolean; value: any } {
return binding.value;
} else {
if (node.name === "undefined") {
return undefined;
return binding ? deopt(binding.path) : undefined;
} else if (node.name === "Infinity") {
return Infinity;
return binding ? deopt(binding.path) : Infinity;
} else if (node.name === "NaN") {
return NaN;
return binding ? deopt(binding.path) : NaN;
}

const resolved = path.resolve();
Expand Down
18 changes: 18 additions & 0 deletions packages/babel-traverse/test/evaluation.js
Expand Up @@ -101,4 +101,22 @@ describe("evaluation", function () {
false
);
});

it("should evaluate undefined, NaN and Infinity", () => {
assert.strictEqual(getPath("undefined").get("body.0.expression").evaluate().confident, true);
assert.strictEqual(getPath("NaN").get("body.0.expression").evaluate().confident, true);
assert.strictEqual(getPath("Infinity").get("body.0.expression").evaluate().confident, true);
});

it("should deopt redefined primitives - undefined, NaN and Infinity", () => {
const eval_undef = getPath("let undefined; undefined;").get("body.1.expression").evaluate();
const eval_nan = getPath("let NaN; NaN;").get("body.1.expression").evaluate();
const eval_inf = getPath("let Infinity; Infinity;").get("body.1.expression").evaluate();
assert.strictEqual(eval_undef.confident, false);
assert.strictEqual(eval_nan.confident, false);
assert.strictEqual(eval_inf.confident, false);

assert.strictEqual(eval_undef.deopt.type, "VariableDeclarator");
assert.strictEqual(eval_undef.deopt.parentPath.node.kind, "let");
});
});

0 comments on commit d92c745

Please sign in to comment.