trullock / NUglify

NUglify is a HTML, JavaScript and CSS minification Library for .NET (fork of AjaxMin + new features)
Other
390 stars 79 forks source link

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

Open koljada opened 2 months ago

koljada commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

Probably in ParseObjectLiteralProperty

failwyn commented 1 month ago

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 commented 1 week ago

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

trullock commented 1 week ago

I haven't fixed it. PRs welcome