-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Anonymous functions sometimes given conflicting names, breaking code. #829
Comments
Somehow, when doing I don't think the minifier normally tries to preserve function names, since functions get renamed when minifying. Although there does seem to be some sort of weird (failed) attempt at preserving const obj = {
alert() {
alert(`Hi, I'm calling "${alert.name}" from "${obj.alert.name}".`);
}
};
obj.alert(); // Hi, I'm calling "alert" from "alert". becomes "use strict";
var obj = {
alert: (function(a) {
function b() {
return a.apply(this, arguments);
}
return (
(b.toString = function() {
return a.toString();
}),
b
);
})(function() {
alert(
"Hi, I'm calling \"" + alert.name + '" from "' + obj.alert.name + '".'
);
})
};
obj.alert(); // Hi, I'm calling "alert" from "b". when minified. |
Names are sometimes automatically assigned - for example - var foo = function () {}
foo.name // foo |
@boopathi but they can’t be used inside the function’s scope to call it recursively. |
This is a bug for sure. Minimal repro - function foo() {
var con = console;
return {
a() {
con.log("foo");
}
};
} This happens when used along with the function name transformer (in preset-env). |
The This was added in babel/babel#3298 The problem is the binding identifier is NOT registered (skipped) during |
The t.NOT_LOCAL_BINDING symbol used in plugin-function-name (https://github.com/babel/babel/blob/bd98041321c60737ddcacd1ad7e056ef6de31879/packages/babel-helper-function-name/src/index.js#L206) is causing this issue. This was added in babel/babel#3298 The problem is the binding identifier is NOT registered (skipped) during crawl and the mangler finds the first safe identifier which ultimately collides with this name during runtime. So, we register the binding and don't respect the t.NOT_LOCAL_BINDING. Fix #829
* fix(mangle): handle inferred names for functions The t.NOT_LOCAL_BINDING symbol used in plugin-function-name (https://github.com/babel/babel/blob/bd98041321c60737ddcacd1ad7e056ef6de31879/packages/babel-helper-function-name/src/index.js#L206) is causing this issue. This was added in babel/babel#3298 The problem is the binding identifier is NOT registered (skipped) during crawl and the mangler finds the first safe identifier which ultimately collides with this name during runtime. So, we register the binding and don't respect the t.NOT_LOCAL_BINDING. Fix #829 * fix lint errors
Input Code
index.js:
package.json:
Actual Output
Prints 'OUCH, should never get here! Should be "Hi.".'.
Expected Output
Prints 'Hi.'.
Details
Note the incorrect
function a()
instead offunction()
, leading to thecon
variable being clobbered.Same thing if using babel-preset-es2015 with babel-preset-minify.
Can't make a direct link to reproduce online – not sure why. But following this link, clicking on 'Plugins', on 'Only official plugins', typing 'mangle' and selecting minify-mangle-names reproduces it:
https://babeljs.io/repl/#?babili=false&browsers=&build=&builtIns=false&code_lz=JAcgrgzgpgBBAuAnAlgY3iA3AKGACjwEoYBeAPhgG9dhUB7AOwRnodKoBs6BzALhgAepCgEMOURPDwDCAXxzBajZnQBGAK3bVFwEUSo1FrAHRdueEAAlkxkIQWLZNeTTXrjI0z3YJEwgzomZhYA8gCqAMKWADRwABZ0YBwAJjAMUABuEjDcUPAwcRJQAIQwAMoJSamqsABEIDAA1HBITTAgtbb2zg5uHkQKsoQD2EA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&lineWrap=false&presets=es2015&prettier=false&targets=&version=6.26.0&envVersion=
Not sure whether this is a minify bug or a babel bug. Can't reproduce if running 'env' on the output of 'minify' or when running 'minify' on the output of 'env', only if doing both in the same step.
The text was updated successfully, but these errors were encountered: