esquery icon indicating copy to clipboard operation
esquery copied to clipboard

RegExp slash escapes not parsed correctly

Open cartant opened this issue 6 years ago • 6 comments

Using the following snippet in the ESQuery demo:

var f = "foo/bar";

None of these queries is parsed correctly and all fail with an error:

[value=/foo\/bar/]

Invalid regular expression: /foo/: \ at end of pattern

[value=/foo\\/bar/]

Expected " " or "]" but "b" found.

[value=/foo\\\\/bar/]

Expected " " or "]" but "b" found.

I'm using tsquery to write an import-location-specific TSLint rule for RxJS and the RegExp issue appears to be an upstream problem with this package.

cartant avatar Jun 03 '18 02:06 cartant

Yeah, the regexp parsing leaves a bit to be desired: https://github.com/estools/esquery/blob/1853a6fbaf40709dc2bf9f3c027163a573c3824f/grammar.pegjs#L83

We should enhance it to at least take into account escape sequences and character classes.

michaelficarra avatar Jun 03 '18 06:06 michaelficarra

My situation was easy to work around, so this is not something that's blocking me. I can have a look at it later, if you are interested in a PR to fix the problem.

cartant avatar Jun 03 '18 06:06 cartant

Sure, send one over.

michaelficarra avatar Jun 03 '18 13:06 michaelficarra

As a workaround you can use the unicode code for / in place of the actual character:

[value=/foo\\u002Fbar/]

toefraz avatar Aug 23 '18 22:08 toefraz

@toefraz I "optimized" your code. 🤣 [value=/foo\\x2Fbar/] (I am kidding, but also I am flexing my knowledge of obscure JS string parsing)

colinta avatar Feb 09 '24 15:02 colinta