tedious / JShrink

Javascript Minifier built in PHP
http://www.tedivm.com
BSD 3-Clause "New" or "Revised" License
749 stars 152 forks source link

Minify fails when shrinking ES6 modules with adjacent private arrow-functions. #131

Closed kwilliams1987 closed 1 year ago

kwilliams1987 commented 1 year ago

Currently the Minifier is producing invalid code under certain conditions inside ES6 modules.

Tested with v1.6.5.

Example:

export default class Example {
    #prop;

    set prop(value) {
        this.#prop = value;

        this.#event(value);
    }
    get prop() { return this.#prop; }

    constructor(arg) {
        this.#prop = arg;

        if (arg) {
            this.#method();
        }
    }

    #event = f => {
        if (typeof(f) === 'function') {
            f();
        }
    }

    #method = async _ => {
        return fetch(param);
    }
}

Output:

export default class Example{#prop;set prop(value){this.#prop=value;this.#event(value);}
get prop(){return this.#prop;}
constructor(arg){this.#prop=arg;if(arg){this.#method();}}#event=f=>{if(typeof(f)==='function'){f();}}#method=async _=>{return fetch(param);}}

Error:

Uncaught SyntaxError: Unexpected identifier '#method' (at example.min.js:3:102)

Seems specifically tied to having two private arrow-functions next to each other.

tedivm commented 1 year ago

Thanks for the report! I've added this to our test suite and have pushed up a fix. If you upgrade to v1.6.6 you should be good!

kwilliams1987 commented 1 year ago

Damn that was a fast turnaround!

Thanks for the quick response.

tedivm commented 1 year ago

Fortunately this was an easier fix! Thanks again for reporting!