NUglify
NUglify copied to clipboard
Object destructing incorrectly reassigns variable
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
curious, good find. Will take a look asap
This also fixes #201 which suffers this same bug
Unfortunately we can't use the workaround mentioned in #201 as this code is in a vendor library.
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}};
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.