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

fix(mangle): handle inferred names for functions #842

Merged
merged 2 commits into from May 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Expand Up @@ -139,6 +139,29 @@ module.exports = babel => {
});
},

/**
* This is required because after function name transformation
* plugin (part of es2015), the function name is NOT added to the
* scope's bindings. So to fix this issue, we simply add a hack to
* handle that case - fix it to the scope tree.
*
* Related:
* - https://github.com/babel/minify/issues/829
*/
BindingIdentifier(path) {
const { scope } = path;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused.


if (
// the parent has this id as the name
(path.parentPath.isFunctionExpression({ id: path.node }) ||
path.parentPath.isClassExpression({ id: path.node })) &&
// and the id isn't yet added to the scope
!hop.call(path.parentPath.scope.bindings, path.node.name)
) {
path.parentPath.scope.registerBinding("local", path.parentPath);
}
},

/**
* This is necessary because, in Babel, the scope.references
* does NOT contain the references in that scope. Only the program
Expand Down
55 changes: 55 additions & 0 deletions packages/babel-preset-minify/__tests__/minify-env-tests.js
Expand Up @@ -196,4 +196,59 @@ describe("preset along with env", () => {
console.log(foo);
`
);

thePlugin(
"should fix issue#829 mangling after function name",
`
function foo() {
let con = console;

return {
a(bar) {
con.log(bar);
}
};
}
`,
`
function foo() {
var b = console;
return {
a: function d(c) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t this be function a so that its .name is correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled by keepFnName option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t we strip the name then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. Correct. Deadcode should have taken care of that (have to debug why it didn't in this case), and we come back to plugin ordering problem - #422

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's the same issue as this one - we have to add the same thing in DCE (register this binding before dce runs).

b.log(c);
}
};
}
`
);

thePlugin(
"should fix issue#829 mangling after function name 2",
`
function bar() {
var b = console;
return {
a: class {
constructor(bar) {
b.log(bar);
}
}
};
}
`,
`
function _classCallCheck(a, b) { if (!(a instanceof b)) throw new TypeError(\"Cannot call a class as a function\"); }

function bar() {
var c = console;
return {
a: function b(a) {
"use strict";

_classCallCheck(this, b), c.log(a);
}
};
}
`
);
});