From d0d1557b436cd2c659590d641b0e6c002c091533 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Thu, 27 Oct 2016 20:50:31 +0200 Subject: [PATCH] Fix DCE tests with labels in separate namespace (#225) * Fix DCE tests with labels in separate namespace * Convert to do..while loop --- .../src/index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-dead-code-elimination/src/index.js b/packages/babel-plugin-minify-dead-code-elimination/src/index.js index 8af81c598..1f803d8bf 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -895,7 +895,13 @@ module.exports = ({ types: t, traverse }) => { // here we handle the break labels // if they are outside switch, we bail out // if they are within the case, we keep them - const _isAncestor = isAncestor(path.scope.getBinding(label.node.name).path, path); + let labelPath; + if (path.scope.getLabel) { + labelPath = getLabel(label.node.name, path); + } else { + labelPath = path.scope.getBinding(label.node.name).path; + } + const _isAncestor = isAncestor(labelPath, path); return { bail: _isAncestor, @@ -957,4 +963,15 @@ module.exports = ({ types: t, traverse }) => { return path.isFunctionDeclaration() || path.isVariableDeclaration({ kind: "var" }); } + + function getLabel(name, _path) { + let label, path = _path; + do { + label = path.scope.getLabel(name); + if (label) { + return label; + } + } while (path = path.parentPath); + return null; + } };