js_regex
js_regex copied to clipboard
ES6 mode
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.
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.
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.
it would also be interesting if this gem had a reverse mode, to convert a javascript formatted regex back into a ruby formatted one
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.
of course, I wouldn't want to sacrifice correctness
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.
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.