Skip to content

Commit

Permalink
handle when binding is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed May 3, 2018
1 parent 0ba8418 commit ce2fef2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/babel-helper-evaluate-path/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,21 @@ function evaluateIdentifier(path) {
const binding = path.scope.getBinding(node.name);

if (!binding) {
return deopt(path);
const { name } = node;
if (!name) {
return deopt(path);
}

switch (name) {
case "undefined":
return { confident: true, value: undefined };
case "NaN":
return { confident: true, value: NaN };
case "Infinity":
return { confident: true, value: Infinity };
default:
return deopt(path);
}
}

if (binding.constantViolations.length > 0) {
Expand Down Expand Up @@ -119,6 +133,9 @@ function evaluateBasedOnControlFlow(binding, refPath) {
}

let blockParent = binding.path.scope.getBlockParent().path;
if (!blockParent) {
return { shouldDeopt: true };
}

if (blockParent === fnParent) {
if (!fnParent.isProgram()) blockParent = blockParent.get("body");
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-preset-minify/__tests__/preset-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,20 @@ describe("preset", () => {
}
`
);

thePlugin(
"should fix issue#810 declaration inside different scope",
`
if (false) {
var bar = true;
}
if (bar) {
alert('bug!');
}
`,
`
var bar;
bar && alert('bug!');
`
);
});

0 comments on commit ce2fef2

Please sign in to comment.