Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mangle - Fix label shadowing #186

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe("mangle-names", () => {
});

// https://phabricator.babeljs.io/T6957
xit("labels should not shadow bindings", () => {
it("labels should not shadow bindings", () => {
const source = unpad(`
function foo() {
var meh;
Expand All @@ -201,6 +201,31 @@ describe("mangle-names", () => {
expect(transform(source)).toBe(expected);
});

// https://github.com/babel/babili/issues/185
it("labels should not shadow bindings 2", () => {
const source = unpad(`
function f(a) {
try {
a: {
console.log(a);
}
} catch ($a) {}
}
`);

const expected = unpad(`
function f(b) {
try {
a: {
console.log(b);
}
} catch (c) {}
}
`);

expect(transform(source)).toBe(expected);
});

it("should be order independent", () => {
const source = unpad(`
function foo() {
Expand Down
62 changes: 62 additions & 0 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,68 @@ module.exports = ({ types: t }) => {
mangle() {
const mangler = this;

this.program.traverse({
Scopable(path) {
const {scope} = path;

const bindings = scope.getAllBindings();
const names = Object.keys(bindings);

for (let i = 0; i < names.length; i++) {
const oldName = names[i];
const binding = bindings[oldName];

if (binding.path.isLabeledStatement()) {
const faulty = binding.referencePaths.filter((ref) => {
return !(ref.parentPath.isBreakStatement() || ref.parentPath.isContinueStatement());
});
faulty.forEach((f) => {
const index = binding.referencePaths.indexOf(f);
if (index > -1) {
binding.referencePaths.splice(index, 1);
binding.references--;
if (binding.references === 0) {
binding.referenced = false;
}
}
});

// probably really bad for labels
scope.removeBinding(oldName);

const newBinding = scope.getBinding(oldName);
if (newBinding) {
// we found a binding in outer scopes
faulty.forEach((f) => newBinding.reference(f));
} else {
// we might have a binding in the same scope
// slow

// register binding
path.traverse({
BindingIdentifier(bindingIdPath) {
if (bindingIdPath.parentPath.isLabeledStatement({ label: bindingIdPath.node })) {
return;
}
if (bindingIdPath.node.name === oldName) {
scope.registerDeclaration(bindingIdPath);
}
}
});

// update references
const registeredBinding = scope.getBinding(oldName);
if (!registeredBinding) {
// I'm not sure what this is - a global?
return;
}
faulty.forEach((f) => registeredBinding.reference(f));
}
}
}
}
});

this.program.traverse({
Scopable(path) {
const {scope} = path;
Expand Down