butternut icon indicating copy to clipboard operation
butternut copied to clipboard

Bug: Values in for loop heads are kept without variable

Open loilo opened this issue 8 years ago • 1 comments

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 avatar May 17 '17 23:05 loilo

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++);

loilo avatar May 19 '17 07:05 loilo