Reputation: 13275
Getting a parser error with Google Closure Compiler for JavaScript private class fields, even though according to MDN it is widely supported. Extending their example:
class ClassWithPrivateField {
#privateField;
constructor() {
this.#privateField = 42;
}
getPrivate() {
return this.#privateField;
}
}
console.log("private field", new ClassWithPrivateField().getPrivate());
Saved the above to private.js and ran it in a Node container:
$ docker run \
--workdir /home/node/app \
--rm -t -v $PWD:/home/node/app \
node:18 bash -c \
"node private.js"
private field 42
The console output shows correctly on the last line above. Now if we run it through google-closure-compiler
:
$ docker run \
--workdir /home/node/app \
--rm -t -v $PWD:/home/node/app \
node:18 bash -c \
"npm i -g google-closure-compiler && google-closure-compiler --js /home/node/app/private.js"
This is the final output:
/home/node/app/private.js:2:2: ERROR - [JSC_PARSE_ERROR] Parse error. '}' expected
2| #privateField;
^
1 error(s), 0 warning(s)
Looking this up I got the impression from a Github post that this feature is not yet supported, is this correct? Is there a solution or workaround, if I want to use private fields? Even ignoring this error to allow the compilation to continue would be acceptable. Thanks.
Upvotes: 1
Views: 477
Reputation: 5468
The Closure Compiler does not yet support private fields. The work around is to not use private fields or to use something like Babel to translate them.
Upvotes: 2