minify-mangle-names fail when default parameters are assigned to a variable
- Dominant language
- JavaScript
- Stars
- 4.4k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
mangling names don't work when there is a default parameter with a value referencing another variable like this:
```js
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
```js
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**
```js
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:
```json
"@babel/core": "^7.17.7",
"babel-plugin-minify-mangle-names": "^0.5.0"
```
babel-minify-config: There is none
babelrc: There is none
Contributor guide
Research direction
Start with babel-plugin-minify-mangle-names/lib/scope-tracker.js, especially ScopeTracker.addReference, then trace its caller in lib/index.js and the traversal in lib/bfs-traverse.js. Reproduce the provided Babel transform and verify that default parameters referencing variables no longer throw, while the output matches the expected mangled names.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- babel, javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100