Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExp slash escapes not parsed correctly #68

Open
cartant opened this issue Jun 3, 2018 · 5 comments
Open

RegExp slash escapes not parsed correctly #68

cartant opened this issue Jun 3, 2018 · 5 comments

Comments

@cartant
Copy link

cartant commented Jun 3, 2018

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.

@michaelficarra
Copy link
Member

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.

@cartant
Copy link
Author

cartant commented Jun 3, 2018

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.

@michaelficarra
Copy link
Member

Sure, send one over.

@toefraz
Copy link

toefraz commented Aug 23, 2018

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

[value=/foo\\u002Fbar/]

@colinta
Copy link

colinta commented Feb 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants