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

Object desctructuring to remove object properties does not work after minification #390

Open
koljada opened this issue Apr 18, 2024 · 7 comments

Comments

@koljada
Copy link

koljada commented Apr 18, 2024

Version
1.21.4

Describe the bug
I'm using object desctructuring to remove object properties. For example:
const { avatar, timeline, backup, ...copy } = { ...user };
After this line copy object does not have avatar, timeline and backup properties.
But after minification not used variables avatar, timeline and backup are removed. And as a result my copy object is identical to original user object. Which is a big problem in my scenario. I tried to set RemoveUnneededCode settings to false but it didn't work.

To Reproduce

string src = """
	const func = () => {
		const user = {
			avatar: Date(),
			timeline: Date(),
			backup: Date(),
			id: 123,
			name: 'Roman',
		};
		const { avatar, timeline, backup, ...copy } = user;
		return copy;
	};
	console.log(func());
	""";
var result = Uglify.Js(src);

Minified output or stack trace
const func=()=>{const n={avatar:Date(),timeline:Date(),backup:Date(),id:123,name:"Roman"},{...copy}=n;return copy};console.log(func())

Excepted output code
const func=()=>{const n={avatar:Date(),timeline:Date(),backup:Date(),id:123,name:"Roman"},{avatar, timeline, backup,...copy}=n;return copy};console.log(func())

@trullock
Copy link
Owner

This is a new one.

Its because it doesn't think theyre used as it doesn't properly understand the object destructuring with the spread operator, i.e. the preceeding arguments/destructured variables change what gets ... spread into copy.

The change needs to be in one of the Analysers, can't recall off the top of my head, pull requests welcome

@failwyn
Copy link

failwyn commented May 8, 2024

I found an issue with destructuring objects with default values, haven’t had a chance to enter it yet, but if anyone can give ideas of where to look, I’ll try to take a pass at all if them. I’ve set aside tomorrow (5/9) to try and fix them.

@trullock
Copy link
Owner

trullock commented May 9, 2024

That would be great, thanks

Without going deep into it i cant tell you exactly where, but from memory its to do with how it parses object literals. I cant remember exactly what the problem was, but the parser gets confused between two different notations/types of objects and it needed more than a quick bugfix to deal with it, that part of the parser needed reconsidering/rewriting

@trullock
Copy link
Owner

trullock commented May 9, 2024

Probably in ParseObjectLiteralProperty

@failwyn
Copy link

failwyn commented May 10, 2024

This one is a bit more than I have time for this week, I'll try to come back to it when I have another free day.

@Matey-Tsilov
Copy link

Hello @failwyn and @trullock Is the bug mentioned by @koljada fixed now? If yes from which version on?

@trullock
Copy link
Owner

trullock commented Jul 1, 2024

I haven't fixed it. PRs welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants