moo icon indicating copy to clipboard operation
moo copied to clipboard

Having "-" as literall match thingies causes invalid escape sequences to be generated when using the unicode

Open TheGrandmother opened this issue 4 years ago • 4 comments

If i have the following token defined: ARITHMETIC: ['+' , '-'], And all my regular expressions are using the u flag moo will output an an invalid regex escape sequence as \- is apparently uncool when rolling with the u flag. Changing the token definition to: ARITHMETIC: ['+' , /-/u], works but is not pretty.

moo version 0.5.1 Node version 13.14.0

TheGrandmother avatar Jul 23 '20 14:07 TheGrandmother

Ooh, that's surprising!

tjvr avatar Jul 31 '20 10:07 tjvr

@tjvr simplest fix is to escape - as \x2d (which works everywhere) instead of \- (which only works inside [] in Unicode mode, even under Annex B).

It's incomprehensible why TC39 decided to make the RegExp grammar this inconsistently nitpicky (e.g., \{ works fine inside a Unicode character class even though the backslash does nothing).

nathan avatar Aug 10 '20 20:08 nathan

If someone wants to raise a PR which updates reEscape (or whatever the function’s called), that would be great.

tjvr avatar Aug 13 '20 11:08 tjvr

I've worked around this by simply changing '-' to /-/u, putting this out here in case it's helpful.

fabiosantoscode avatar Feb 28 '22 13:02 fabiosantoscode