js_regex icon indicating copy to clipboard operation
js_regex copied to clipboard

ES6 mode

Open fledman opened this issue 5 years ago • 6 comments
trafficstars

The readme mentions:

Possible future improvements might include an "ES6 mode" using the u flag, which would allow for more concise representations of astral plane properties and sets.

I was wondering if this improvement is still planned, or if the gem is considered complete at this point.

fledman avatar Jun 16 '20 16:06 fledman

greetings!

may i ask what your use case is?

as far as i can tell, supporting the u flag would bring mostly cosmetic benefits and make the conversion result more readable. size shouldn't be an issue in most cases.

it's been a while since i wrote this section, though. now that even firefox is catching up, an ES2018 mode might be nice, adding not only the u flag but also support for lookbehinds, the dot-all flag s, maybe property escapes (if codepoints match) and a few more things.

jaynetics avatar Jun 16 '20 19:06 jaynetics

My use case is basically human readability.

I am using this gem to ensure consistency between client and server side validations.

It would be much easier to audit/verify/understand the generated regexes given such a mode.

fledman avatar Jun 16 '20 23:06 fledman

it would also be interesting if this gem had a reverse mode, to convert a javascript formatted regex back into a ruby formatted one

fledman avatar Jun 18 '20 16:06 fledman

yeah that would be neat, but it would require a parser for JavaScript regular expressions to do it cleanly. i don't think there is one in Ruby, and it isn't exactly straightforward to write one either due to differences between versions.

concerning readability, I might get around to implementing modern ES capabilities soonish. but that won't make everything more readable. e.g. properties will still be translated to character sets if JS uses different codepoints for them because correctness, i.e. consistent behavior between Ruby and JS, is more important than readability to me.

jaynetics avatar Jun 18 '20 18:06 jaynetics

of course, I wouldn't want to sacrifice correctness

fledman avatar Jun 18 '20 18:06 fledman

I came across this issue as a result of this comment: https://github.com/ammar/regexp_parser/issues/80#issuecomment-784543054

Not having a ES2018 mode would be a regression if ruby2js were to make use of this function.

Ruby2JS's focus is readability. One of the use cases for Ruby2JS is a one way migration: taking code originally written in Ruby and converting it to JavaScript with the intent of future maintenance being against the JavaScript version.

rubys avatar Feb 24 '21 01:02 rubys

i've finally gotten around to supporting modern JS!

there is now an optional target: argument.

JsRegex.new(regexp, target: 'ES2015') makes use of the u flag and suffices for most of the benefits in terms of readability.

target: 'ES2018' adds support for some more features and concise representations, but isn't fully supported by Safari. (please see the CHANGELOG entry for further details.)

while this has improved readability, it might not be the final word on it. especially the translation of . is still a bit ugly, but also tricky to improve. i might also have missed some opportunities for generating more readable output.

feel free to open additional issues if you have ideas about this or notice anything that looks like it can be improved.

jaynetics avatar Sep 25 '22 20:09 jaynetics