Rich-Harris / butternut

The fast, future-friendly minifier
https://butternut.now.sh
MIT License
1.17k stars 17 forks source link

Non-empty loops being collapsed if first node is EmptyStatement #57

Closed jbt closed 7 years ago

jbt commented 7 years ago

e.g. while (a) { ; b(); } -> while(a);; (link)

I tried making some test-cases + fixing up the check for empty nodes by checking over every descendent node rather than just the first, but my do-while case is failing completely for magic-string reasons I don't fully understand.

Additionally, all cases where a curly-braced loop could be reduced to a single-statement body aren't working because I can't figure out how to trim leading/trailing EmptyStatements.

Incidentally it looks like there's a few more cases where leading empty statements in a block change the output (e.g. if (a) { b(); } vs if (a) { ; b(); }) but the loops are the only case where it's doing something it definitely shouldn't.

Rich-Harris commented 7 years ago

Thanks. It was operating under the incorrect assumption that if the first statement of the body was an EmptyStatement, the body itself was empty. It should really remove empty statements altogether — will get to that soon — but #59 generates correct if suboptimal code