estools/esquery

RegExp slash escapes not parsed correctly

cartant opened this issue ยท 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.

Yeah, the regexp parsing leaves a bit to be desired:

regex = "/" d:[^/]+ "/" { return { type: 'regexp', value: new RegExp(d.join('')) }; }

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

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.

Sure, send one over.

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

[value=/foo\\u002Fbar/]

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

pke commented

Will this ever be fixed?