NUglify icon indicating copy to clipboard operation
NUglify copied to clipboard

Object destructing incorrectly reassigns variable

Open scott-the-brewer opened this issue 3 years ago • 5 comments

this:

{
  const opts = { environment: 'dev' }
  const { environment = 'prod' } = opts;
}

becomes:

{const{n="prod"}={environment:"dev"}}

instead I expect it to be:

{const{environment="prod"}={environment:"dev"}}

This works correctly, when not in block scope:

const opts = { environment: 'dev' }
const { environment = 'prod' } = opts;

becomes:

const opts={environment:"dev"},{environment="prod"}=opts

scott-the-brewer avatar Jul 21 '21 13:07 scott-the-brewer

curious, good find. Will take a look asap

trullock avatar Jul 21 '21 13:07 trullock

This also fixes #201 which suffers this same bug

trullock avatar Jul 26 '21 08:07 trullock

Unfortunately we can't use the workaround mentioned in #201 as this code is in a vendor library.

scott-the-brewer avatar Jul 26 '21 08:07 scott-the-brewer

I noticed a similar issue when using a destructured object as a constructor parameter. If a property gets a default value, its name will be replaced. If it doesn't get a default value, the name doesn't get replaced, but gets a variable name appended to it (in the example below, "property1" gets turned into "property1:n").

Example:

class TestClass {
    constructor({
        property1,
        property2 = 'value2',
        property3 = 1,
        property4 = false
    }) {
        const test1 = property1 || 123;
        const test2 = `foo_${property2}`;
        const test3 = property3 * 10;
        const test4 = property4 ? 1 : 2;
    }
}

Becomes:

class TestClass{constructor({property1:n,t="value2",i=1,r=false}){const u=n||123,f=`foo_${t}`,e=i*10,o=r?1:2}};

HansHappyHorizon avatar Mar 30 '22 09:03 HansHappyHorizon

As HansJuice mentioned. This really messes up functions in my code where the result is that functions dont get their parameters set, since the names get minified.

AllNamesRTaken avatar Nov 16 '22 14:11 AllNamesRTaken