Skip to content

Commit

Permalink
Fix DCE tests with labels in separate namespace (#225)
Browse files Browse the repository at this point in the history
* Fix DCE tests with labels in separate namespace

* Convert to do..while loop
  • Loading branch information
boopathi authored and kangax committed Nov 2, 2016
1 parent 62441c0 commit 1e47c64
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/babel-plugin-minify-dead-code-elimination/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
};

0 comments on commit 1e47c64

Please # to comment.