Skip to content

Conversation

@JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Oct 31, 2024

Fixes #68 in support of eslint/eslint#16555.

This solution is a kind of hacky workaround based on #68's comments. It "encodes" all backslash-escaped forward slashes ('\\' + '/') with a backslash and an equivalent unicode character ('\\' + '\\x2F').

I think it would be much better to work within the PEG grammar in grammar.pegjs. Unfortunately, there is no backtracking or other nontrivial regular expression techniques in PEG.js. Expressions like d:[^\/]+ ("capture any number of characters other than /, under the name d") don't have a way I could find to, say, skip past a character like \.

& { predicate } and ! { predicate } with matchers like "/" d:.+ { ... } "/" or just "/" d:.+ { ... } also seemed promising. But then I couldn't figure out how to get it to match non-greedily: i.e. the full /foo\/bar/ instead of just /foo\/.

I don't often work with PEGs and am not confident I didn't miss some obvious technique. I hope someone who's more experienced in these matters will point something out that's more PEG-oriented. 🙂

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as ready for review November 4, 2024 19:26
@nzakas
Copy link
Contributor

nzakas commented Dec 29, 2025

@michaelficarra can you take a look at this?

@michaelficarra
Copy link
Member

@nzakas @JoshuaKGoldberg I've updated the PR to use the PEG to just parse the various components of the regexp. For now, the only parsing contexts are character classes, hex escapes, single-character escapes, and of course the top-level parsing context. Please review and also try it out to see if it works in your project. I also updated the tests to be more strict.

@michaelficarra
Copy link
Member

Rebased.

@JoshuaKGoldberg
Copy link
Contributor Author

✅ confirmed, that works for me! I posted the test case in eslint/eslint#16555 (comment):

For posterity, this is the basic test case I'm using in tests/lib/rules/no-restricted-syntax.js to test estools/esquery#149:

{
code: import values from 'some/path';,
options: ["ImportDeclaration[source.value=/^some\/path$/]"],
errors: [
{
messageId: "restrictedSyntax",
data: {
message:
"Using 'ImportDeclaration[source.value=/^some\/path$/]' is not allowed.",
},
},
],
}

Thanks for picking this up!

@michaelficarra michaelficarra merged commit a78e829 into estools:master Dec 31, 2025
5 checks passed
@michaelficarra
Copy link
Member

@JoshuaKGoldberg published v1.7.0

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

Successfully merging this pull request may close these issues.

RegExp slash escapes not parsed correctly

3 participants