Rich-Harris / butternut

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

Bug: Values in for loop heads are kept without variable #129

Open loilo opened 7 years ago

loilo commented 7 years ago

From squashing minified ajv.

When Butternut encounters a secondary variable initialization in the first part of a for loop head, and that secondary variable is no further used, the variable will be removed while the value will be kept in place. This leads to syntax errors or even non-errors with semantically wrong code.

Example 1: Syntax error

Input:

for ( let counter_var = 1, another_var = []; any_evaluation(); counter_var++ ) {}

Output Butternut:

for(let a=1;[];any_evaluation();a++);

Output UglifyJS:

for(let f=1;any_evaluation();f++);

Example 2: Modified semantics

Input: Just use a number as the value instead of the empty array.

for ( let counter_var = 1, another_var = 2; any_evaluation(); counter_var++ ) {}

Output Butternut:

for(let a=12;any_evaluation();a++);

Output UglifyJS:

for(let f=1;any_evaluation();f++);

Side note: Both examples are also working with var instead of let, but only inside an IIFE.

loilo commented 7 years ago

Example 3: Modified semantics 2

If the second variable has no value attached, the variable name itself will be appended to the first variable's value.

Input:

for ( let counter_var = 1, another_var; any_evaluation(); counter_var++ ) {}

Output Butternut: (different variable names than in the issue description due to new char frequency dependant naming in 0.4.6)

for(let f=1r;any_evaluation();f++);

Output UglifyJS:

for(let f=1;any_evaluation();f++);