Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deopt evaluation of undefined with a local binding. Fix #5204 #5206

Merged
merged 1 commit into from Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/evaluation.js
Expand Up @@ -181,11 +181,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 @@ -98,4 +98,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");
});
});