diff --git a/packages/babel-helper-evaluate-path/src/index.js b/packages/babel-helper-evaluate-path/src/index.js index baed0e119..43c49844c 100644 --- a/packages/babel-helper-evaluate-path/src/index.js +++ b/packages/babel-helper-evaluate-path/src/index.js @@ -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) { @@ -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"); diff --git a/packages/babel-preset-minify/__tests__/preset-tests.js b/packages/babel-preset-minify/__tests__/preset-tests.js index 72cb3b447..dd3a6060d 100644 --- a/packages/babel-preset-minify/__tests__/preset-tests.js +++ b/packages/babel-preset-minify/__tests__/preset-tests.js @@ -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!'); + ` + ); });