Bug: is_identifier_string is broken for some characters under typical environments (e.g. "Terser on Node.js and run on browser") #1512
+8
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR; When running on Node.js (e.g. v18),
is_identifier_string
incorrectly decide that "a・b
is a valid identifier" (the string includes "U+30FB KATAKANA MIDDLE DOT") and Terser applies optimization based on this and it can generate broken JavaScript for typical environments.For example,
console.log({"a・b":123})
will beconsole.log({a・b:123})
, which will throwSyntaxError: Invalid or unexpected token
when parsed on recent Google Chrome (tested v123). Note that we can check this "syntax error" also in this TypeScript Playground.How does this happen?
is_identifier_string internally uses
ID_Continue
regex, which uses\p{ID_Continue}
when available (source). This means that depending on the runtime environment (e.g. Node.js / Deno / Bun) and the version, the actual character set will be different sinceID_Continue
is being updated by Unicode spec.Unicode says:
So if we use newer logic for deciding "is_identifier" for a minifier, it can generate JavaScript file which is not compatibile with older environment.
I think it's better to hard-code the whole logic of "is_identifier". Otherwise any runtime updates such as Node.js update can start generating yet another broken JavaScript from Terser.
The solution in this PR
I just removed the variant to use unicode properties in regex. Notice that this bug is not reproducible when running Terser in web browser since
\p{ID_Continue}
is executed by browser, which has older set of characters than Node.js.It would be nice if we can write test case to avoid reintroducing Unicode properties regex but so far I have no idea. I just added a test case for the currently known problematic case.
Concerns for this PR
There might be performance degradation. But I think it's more important not to generate broken script for typical use cases.
Also it's better to make sure the behavior is consistent across environments for this kind of library IMHO
Misc
console.log(/\p{ID_Continue}/u.test('\u30FB'))
can be used for a quick test case, which can betrue
orfalse
depending on supported unicode version