Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't convert RegExp:Unicode property escapes #2215

Closed
YOOYY opened this issue Apr 28, 2022 · 6 comments
Closed

Can't convert RegExp:Unicode property escapes #2215

YOOYY opened this issue Apr 28, 2022 · 6 comments

Comments

@YOOYY
Copy link

YOOYY commented Apr 28, 2022

There is a new feature in es2018, RegExp: Unicode property escapes; I tried converting it to es2015 but nothing changed in the code.

for example:

const regexGreekSymbol = /\p{Script_Extensions=Greek}/u;
console.log(regexGreekSymbol.test('π'));

output:

var regexGreekSymbol = /\p{Script_Extensions=Greek}/u;
console.log(regexGreekSymbol.test("\u03C0"));

this is my configuration:

{
  entryPoints: ['./demo.js'],
  outfile: 'outfile.js',
  target: ['es2015']
}

version: 0.14.38

@hyrious
Copy link

hyrious commented Apr 28, 2022

As far as I know there're some other new regex flags added recently. I wonder how could we polyfill/convert them correctly to lower environments. At least is there a reference implementation?

@evanw
Copy link
Owner

evanw commented Apr 28, 2022

TypeScript's lowering transform doesn't change the RegExp literal either, even when targeting ES5 (link). See also microsoft/TypeScript#36132 and microsoft/TypeScript#32214. I'm guessing the most straightforward thing for esbuild to do here would be to convert regular expressions into new RegExp() to convert the syntax error into a run-time error. This would let you bring your own polyfill.

@YOOYY
Copy link
Author

YOOYY commented Apr 29, 2022

As far as I know there're some other new regex flags added recently. I wonder how could we polyfill/convert them correctly to lower environments. At least is there a reference implementation?

Babel seems to support converting RegExp.
I had this problem when using vite, so I had to add vite's babel plugin, I also saw esbuild's babel plugin on npm

@evanw
Copy link
Owner

evanw commented Apr 29, 2022

Babel seems to support converting RegExp.

Not always. It looks like Babel just converts /./y into new RegExp(".", "y") for example. I assume that will crash in older browsers. Here's the relevant Babel thread: babel/babel#904. They are deliberately leaving this up to the polyfill to handle.

@jridgewell
Copy link
Contributor

jridgewell commented Apr 29, 2022

Babel can't implement sticky behavior without implementing a full matcher, so it's left to runtime. However, it's possible to support unicode, unicode properties escapes, unicode sets, named capture groups, and dotall through regexpu. For named capture groups, it requires a runtime wrapper, but everything else is just a syntax transform.

@evanw evanw closed this as completed in 14d9de5 May 27, 2022
@evanw
Copy link
Owner

evanw commented May 27, 2022

Thanks very much for the context about what Babel does handle. However, despite that I decided to fix this by converting unsupported regular expression literals to new RegExp(). One reason is that lowering regular expressions looks like a significant increase in complexity and this conversion is a much simpler way to solve this problem. Another reason is that a pure syntax conversion isn't totally correct, and you may still want/need a polyfill anyway (e.g. if you need the flags property to represent the original source code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants