closure-compiler
closure-compiler copied to clipboard
Unexpected strict mode reserved word "let"
tl,dr: I'm getting function let() in my advanced compilation output, which is breaking runtime on older browsers.
One of my project's target platform is an old browser not permitting the "let" keyword e.g. as a function name, and our build intermittently fails at runtime in this browser with "Unexpected strict mode reserved word".
The latest case was in this line of output:
...function let(e,R,Z,V){...
Here's how we're configuring the build (we use the Java API directly). I'm already trying to explicitly avoid let, having been unable to find a place in the Closure Compiler source where you already do this:
RandomNameGenerator rng = new RandomNameGenerator(new Random(buildRevision.hashCode()));
rng.reset(new HashSet<>(Arrays.asList("break", "case", "catch", "class", "const",
"continue", "debugger", "default", "delete", "do", "else", "export", "extends",
"finally", "for", "function", "if", "import", "in", "instanceof", "new", "return",
"super", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with",
"yield", "enum", "implements", "interface", "let", "package", "private",
"protected", "public", "static", "await", "abstract", "boolean", "byte", "char",
"double", "final", "float", "goto", "int", "long", "native", "short",
"synchronized", "throws", "transient", "volatile")), "", null);
options.setNameGenerator(rng);
I'm assuming that
NameGenerator.reset()
is being called somewhere, and my preferences are being overwritten/cleared.
Is there another way to prevent reserved words being used as names?
From https://groups.google.com/forum/#!topic/closure-compiler-discuss/QmKBuR2kJ6s
@blickly I believe you have some familiarity with RandomNameGenerator. Could you take a look at this one?
Sure, I know that the ParserConfig.properties file is a way that we blacklist certain names from renaming: https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/parsing/ParserConfig.properties so that might be a way to workaround the issue.
But I think that more generally, we should always avoid generating output names that are keywords
@blickly Thanks for the tip - to test, I hacked ParserRunner.reservedVars through reflection to simulate adding all ES6 keywords to compiler.reserved.vars, and it did indeed prevent the problem.
I'm curious why this hasn't happened before - presumably I'm not the only one using randomised names in large projects. Also I'm curious where you're preventing e.g. if and for from being used. Surely if would have occurred naturally much sooner, randomised or not. I would have thought there is a list somewhere that is missing let but I didn't find it.
Good questions. I wondered the same thing.
It looks like the code that makes sure to skip keywords as generated names (in both the Random and Default name generators) is using a utility from the old rhino codebase for determining keywords, which is pre-ES6: https://github.com/google/closure-compiler/blob/3617803beb41931ffc7bba3e3f9c1e35485af067/src/com/google/javascript/jscomp/RandomNameGenerator.java#L280 https://github.com/google/closure-compiler/blob/333e143f72f1ef536f927bb6b283c2adc19ec134/src/com/google/javascript/jscomp/DefaultNameGenerator.java#L304
I guess we're just lucky that most of the new ES6 keywords are short enough that no one hit this before
On Mon, Jul 10, 2017 at 5:22 AM, Steve Spencer [email protected] wrote:
@blickly https://github.com/blickly Thanks for the tip - to test, I hacked ParserRunner.reservedVars through reflection to simulate adding all ES6 keywords to compiler.reserved.vars, and it did indeed prevent the problem.
I'm curious why this hasn't happened before - presumably I'm not the only one using randomised names in large projects. Also I'm curious where you're preventing e.g. if and for from being used. Surely if would have occurred naturally much sooner, randomised or not. I would have thought there is a list somewhere that is missing let but I didn't find it.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/google/closure-compiler/issues/2560#issuecomment-314089574, or mute the thread https://github.com/notifications/unsubscribe-auth/AACOQKM69rYQ_1FDbqS1GQwQzkB-E6lhks5sMheNgaJpZM4OPosp .