`quote_keys` quotes private class member names, producing invalid output
Bug report or Feature request?
Bug report
Version (complete output of terser -V or specific git commit)
terser 5.51.2. Also reproduced on master at 707c978 through minify().
Complete CLI command or minify() options used
terser input.js --format quote_keysterser input
class A {
#field = 1;
static #method() {}
get #getter() { return 1; }
set #setter( v ) {}
run() { return this.#field + this.#getter; }
}terser output or error
class A{#"field"=1;static#"method"(){}get#"getter"(){return 1}set#"setter"(v){}"run"(){return this.#field+this.#getter}}This isn't valid JavaScript. Node rejects it with SyntaxError: Invalid or unexpected token at #"field", and browsers reject it the same way. Affects every kind of private member declaration: fields, methods, getters and setters. Private member access such as this.#field are printed correctly.
The README describes quote_keys as quoting "all keys in literal objects", and a private name is not an object key, so seems like the option shouldn't apply to it at all. The only existing quote_keys test, in test/compress/object.js, covers object literals only.
Expected result
Private names are printed unquoted, like they are without quote_keys:
class A{#field=1;static#method(){}get#getter(){return 1}set#setter(v){}"run"(){return this.#field+this.#getter}}Cause
In lib/output.js, DEFPRINT(AST_ClassPrivateProperty) and _print_getter_setter(type, is_private, output) print # and then call print_property_name() for the name. With quote_keys enabled, print_property_name() quotes every key without exception:
function print_property_name(key, quote, output) {
if (output.option("quote_keys")) {
output.print_string(key);
return false;
}A private name is always an identifier and can never be quoted, so these two call sites could print the name directly instead of going through print_property_name(). I'm planning on making a PR with a test using that approach, if it sounds right.
Source: terser/terser