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 7ce3c91e3..0fbffb2c7 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; + } };