babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

minify-mangle-names fail when default parameters are assigned to a variable #1023

Open binary-person opened 2 years ago

binary-person commented 2 years ago

Describe the bug

mangling names don't work when there is a default parameter with a value referencing another variable like this:

var abc = 123;
function lmno(abcdefg = abc) {}

This causes issues when trying to minify with mangling as described in this still open issue opened two years ago: #974

To Reproduce

Minimal code to reproduce the bug

const babel = require('@babel/core');

console.log(babel.transformSync(`
var uhoh = 'oops';
function test(first, second = '123', third = uhoh) {}

(function() {
    var uhoh1 = 'oops1';
    function test1(first1, second1 = '123', third1 = uhoh1) {}
})()
`, {
    plugins: ['minify-mangle-names'],
}).code);

Expected Output

var uhoh = 'oops';

function test(a, b = '123', c = uhoh) {}

(function () {
  var d = 'oops1';

  function b(a, b = '123', c = d) {}
})();

Stack Trace

/home/coder/project/fix-babel/node_modules/@babel/core/lib/transformation/index.js:45
    throw e;
    ^

TypeError: unknown: Cannot read properties of undefined (reading 'add')
    at ScopeTracker.addReference (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
    at ReferencedIdentifier (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26)
    at newFn (/home/coder/project/fix-babel/node_modules/@babel/traverse/lib/visitors.js:218:17)
    at bfsTraverse (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43)
    at Mangler.collect (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7)
    at Mangler.run (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12)
    at PluginPass.exit (/home/coder/project/fix-babel/node_modules/babel-plugin-minify-mangle-names/lib/index.js:558:19)
    at newFn (/home/coder/project/fix-babel/node_modules/@babel/traverse/lib/visitors.js:177:21)
    at NodePath._call (/home/coder/project/fix-babel/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/home/coder/project/fix-babel/node_modules/@babel/traverse/lib/path/context.js:40:17) {
  code: 'BABEL_TRANSFORM_ERROR'
}

Configuration

package.json:

    "@babel/core": "^7.17.7",
    "babel-plugin-minify-mangle-names": "^0.5.0"

babel-minify-config: There is none

babelrc: There is none