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

identifier mangler on default parameters #41

Closed
Sorte1 opened this issue Dec 31, 2023 · 10 comments · Fixed by #42
Closed

identifier mangler on default parameters #41

Sorte1 opened this issue Dec 31, 2023 · 10 comments · Fixed by #42
Labels
bug Something isn't working mangle

Comments

@Sorte1
Copy link
Contributor

Sorte1 commented Dec 31, 2023

Describe the bug

When passing an identifier as a default parameter valuee, i doest work

the error seems to come from "babel-plugin-minify-mangle-names/lib/scope-tracker.js"
line 47

image

Expected Behaviour

function a(a, b = 0, c = a, d = 30) {
  return a;
}

Code

function func(adad, arg2 = 0, arg3 = adad, arg4 = 30) {
  return arg1;
}

or 

```js 
let adad = 1
function func(arg1, arg2 = 0, arg3 = adad, arg4 = 30) {
  return arg1;
}


### Logs

```Text
TypeError: Cannot read properties of undefined (reading 'add')
    at ScopeTracker.addReference (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
    at ReferencedIdentifier (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/index.js:202:26)
    at newFn (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/@babel/traverse/lib/visitors.js:195:17)
    at bfsTraverse (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:38:43)
    at Mangler.collect (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/index.js:235:7)
    at Mangler.run (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/index.js:60:12)
    at Object.exit (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/babel-plugin-minify-mangle-names/lib/index.js:546:19)
    at NodePath._call (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/@babel/traverse/lib/path/context.js:46:20)
    at NodePath.call (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/@babel/traverse/lib/path/context.js:36:17)
    at NodePath.visit (/home/sorte/.npm/_npx/6da011cd7208f74f/node_modules/@babel/traverse/lib/path/context.js:90:8)

Node.js v18.16.0
@Sorte1 Sorte1 added the bug Something isn't working label Dec 31, 2023
@Sorte1
Copy link
Contributor Author

Sorte1 commented Dec 31, 2023

a possible but not ideal fix would just be moving the variable assignment down

let adad = 1
function func(arg1, arg2 = 0, arg3 , arg4 = 30) {
  arg3 = adad
  return arg1;
}

@j4k0xb
Copy link
Owner

j4k0xb commented Dec 31, 2023

Yeah that's a known issue: babel/minify#1023
It doesn't seem to be maintained anymore and isn't really the right tool (as it optimizes for 1 letter names and gzip compression rather than readability)

Your suggested fix could work with some changes so I'll try applying it for now, but the name mangling will eventually be redesigned from scratch: #21

let adad = 1
// dummy default param to retain arguments.length and func.length == 1 behaviour
function func(arg1, arg2 = 0, arg3 = undefined , arg4 = 30) {
  if (arg3 === undefined) arg3 = adad; // only set when the arg isn't passed
  return arg1;
}

@j4k0xb
Copy link
Owner

j4k0xb commented Dec 31, 2023

Could you check if everything works now? https://deploy-preview-42--webcrack.netlify.app/

@Sorte1
Copy link
Contributor Author

Sorte1 commented Dec 31, 2023

this file gives me the same error

raw-game.zip

image

@j4k0xb
Copy link
Owner

j4k0xb commented Dec 31, 2023

works fine for me
image

@Sorte1
Copy link
Contributor Author

Sorte1 commented Dec 31, 2023

I just tried it again, and now it works...

@Sorte1 Sorte1 closed this as completed Dec 31, 2023
@j4k0xb
Copy link
Owner

j4k0xb commented Dec 31, 2023

oops had a bug in it, !== should be ===

@Sorte1
Copy link
Contributor Author

Sorte1 commented Dec 31, 2023

when i looked at it, i was like thats not gonna work, but then i got distracted and forgot to write the comment

@j4k0xb j4k0xb added the unminify label Jan 1, 2024
@Sorte1
Copy link
Contributor Author

Sorte1 commented Jan 2, 2024

when will this fix be released?

@j4k0xb
Copy link
Owner

j4k0xb commented Jan 2, 2024

I released it now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mangle
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants