coderaiser/minify

Order of operations not preserved when using classes or switch statement

Closed this issue · 3 comments

Using classes:
Considering the following code which works fine on its own, minifier merges the variable declarations, and produces (a well deserved) error in runtime:

original code:

var a = "foo";
var b = 123.4;
class C {
    f() {
    }
    constructor() {
        this.d = a;
        this.e = b;
    }
}
var g = new C();
globalThis.h = g;
globalThis.i = g;

minified code, formatted:

var a='foo',
    b=123.4,
    g=new C();
class C{
  f(){}
  constructor(){
    this.d=a;
    this.e=b
  }
}
globalThis.h=g;
globalThis.i=g;

Notice that instantiation of 'c' jumped before the class declaration, thus throwing a runtime error.

Using switch statement:
The switch statement case is similar.

Fom this:

var a = "x";
switch (a) {
    case "a":
        var b = "foo";
        console.log(b);
        break;
    case "x":
        var c = "bar";
        console.log(c);
        break;
}

To this:

var a='x';
switch(a) {
  case 'a':
    var b='foo',
        c='bar';
    console.log(b);
    break;
  case 'x':
    console.log(c);
    break
}

tested with minify v11.0.1

It should be fixed in @putout/plugin-minify

Just fixed in @putout/minify@3.8.0 🎉. Please re-isntall Minify. Is it works for you?

Verified the fix, Thanks for the fast response!

Cheers mate!