Unicode RegExp escape sequences handled backwards (lone braces and brackets)
See original GitHub issueBug Report
- I would like to work on a fix!
Current Behavior
The way lone braces and brackets are handled when the u
flag is set isn’t spec conformant. Babel accepts invalid regexps, and emits useless escape sequences.
Input Code
const invalid = [/}/u, /]/u]
Expected behavior/code
Both of these RegExps are syntax errors when the Unicode flag is set, those brackets must be escaped according the the spec (see them fail in browers live here).
Also, the resulting RegExp doesn’t have the Unicode flag set, so escaping these characters is not needed in the result, but Babel escapes them anyway. /\}/u
could be compiled to /}/
without harm.
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
Default config using the online REPL. I don’t have Babel installed yet.
Environment
- Babel version(s): v7.8.4
- Node/npm version: online REPL
- OS: any Mac Browser
Possible Solution
Fix the parser 😃
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Edit: sorry, I spoke too fast. The problem indeed lies with regexu.
@nicolo-ribaudo if this is merged as is upstream, it will break code that relies on Babel, and publishing a codemod to fix this automatically will be hard (since the fix will be picked up by
regexpu-core
and@babel/helper-create-regexp-features-plugin
through semver, preventing the parsing of the problematic source files).I suppose we need to create and publish a codemod first, and wait for the ecosystem to be in good shape.
… unless you have a way to override
@babel/helper-create-regexp-features-plugin
on a per install basis, in which case we can create the codemod based on the previous version ofregjsparser
…