trullock / NUglify

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

Array destructuring with defaults produces non-working and very odd code #349

Open jfrank14 opened 1 year ago

jfrank14 commented 1 year ago

My source code is this:

//name and email delimited by a tab
var line = 'myname\tmyname@mydomain.net';
//split by tabs
var fields = line.split(/\t/);
//destructure into named variables
var [name = "", email = ""] = fields;

but NUglify is rewriting this as:

var s = this, t, o, c, u, e;
....
if (o = t.split(/\t/),
[] = o,

Variables c and u are then intended to be name and email, but are always undefined.

Also, that if syntax followed by a comma looks really suspect too.

However, if I change it to remove the default values, it produces working and reasonable code:

var s = this, t, o, c, u, e;
...
if (o = t.split(/\t/),
[c,u] = o,
trullock commented 1 year ago

array destructuring is a relatively new syntax, NUglify probably doesnt support it at all, hence the mad output. PRs welcome

jfrank14 commented 1 year ago

Is that because uglify-js itself doesn't support it, or is this specific to NUglify?

Curiously, it seems to work fine to destructure arrays as long as no default values are specified.

I'm a bit swamped with work, but will try to find a bit of time to see if I could fix this.

trullock commented 1 year ago

https://github.com/trullock/NUglify/blob/master/src/NUglify.Tests/JavaScript/ES2015.cs#L100

This test already exists, look when it was added and what was touched as a starting point.

Otherwise add breakpoints inside JSParser, maybe in ParseArrayLiteral