question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unicode RegExp escape sequences handled backwards (lone braces and brackets)

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pygycommented, Feb 13, 2020

Edit: sorry, I spoke too fast. The problem indeed lies with regexu.

0reactions
pygycommented, Feb 13, 2020

@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 of regjsparser

Read more comments on GitHub >

github_iconTop Results From Across the Web

Character Escapes in .NET Regular Expressions
A character that otherwise would be interpreted as an unescaped language construct should be interpreted literally. For example, a brace ( { ) ......
Read more >
Perl Regular Expression Backslash Sequences and Escapes
\o{} Octal escape sequence. \p{}, \pP Match any character with the given Unicode property. \P{}, \PP Match any character without the given property....
Read more >
Everything you need to know about Regular Expressions
A regular expression pattern is constructed from distinct building blocks. It may contain literals, character classes, boundary matchers, ...
Read more >
Regular Expression (Regex) Tutorial
[^...]: NOT ONE of the character, e.g., [^0-9] matches any non-digit. Only these four characters require escape sequence inside the bracket list: ^...
Read more >
pcre2pattern specification
escape sequence is recognized only when PCRE2 is operating in UTF mode. Perl also uses \N{name} to specify characters by Unicode name; PCRE2 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found