Skip to content

Commit 9eeff3b

Browse files
fix: update esquery (#20423)
* fix: update esquery * Update selectors documentation and add a test case * Update docs/src/extend/selectors.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update docs/src/extend/selectors.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> --------- Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent 1c4b33f commit 9eeff3b

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎docs/src/extend/selectors.md‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The following selectors are supported:
3333
- wildcard (matches all nodes): `*`
3434
- attribute existence: `[attr]`
3535
- attribute value: `[attr="foo"]` or `[attr=123]`
36-
- attribute regex: `[attr=/foo.*/]` <sub>(with some [known issues](#known-issues))</sub>
36+
- attribute regex: `[attr=/foo.*/]`
3737
- attribute conditions: `[attr!="foo"]`, `[attr>2]`, `[attr<3]`, `[attr>=2]`, or `[attr<=3]`
3838
- nested attribute: `[attr.level2="foo"]`
3939
- field: `FunctionDeclaration > Identifier.id`
@@ -147,21 +147,25 @@ Or you can enforce that calls to `setTimeout` always have two arguments:
147147

148148
Using selectors in the `no-restricted-syntax` rule can give you a lot of control over problematic patterns in your codebase, without needing to write custom rules to detect each pattern.
149149

150-
### Known issues
150+
### Using regular expressions
151151

152-
Due to a [bug](https://github.com/estools/esquery/issues/68) in [esquery](https://github.com/estools/esquery), regular expressions that contain a forward-slash character `/` aren't properly parsed, so `[value=/some\/path/]` will be a syntax error. As a [workaround](https://github.com/estools/esquery/issues/68), you can replace the `/` character with its unicode counterpart, like so: `[value=/some\u002Fpath/]`.
152+
You can use regular expressions in attribute selectors. For example, the following selector matches all `Identifier` nodes with a `name` value that starts with "foo":
153153

154-
For example, the following configuration disallows importing from `some/path`:
154+
```text
155+
Identifier[name=/^foo/]
156+
```
157+
158+
You can also use forward slashes in your regular expressions. For example, the following configuration disallows importing from `some/path`:
155159

156160
```json
157161
{
158162
"rules": {
159163
"no-restricted-syntax": [
160164
"error",
161-
"ImportDeclaration[source.value=/^some\\u002Fpath$/]"
165+
"ImportDeclaration[source.value=/^some\\/path$/]"
162166
]
163167
}
164168
}
165169
```
166170

167-
Note that the `\` character needs to be escaped (`\\`) in JSON and string literals.
171+
Note that the `/` character inside the regular expression needs to be escaped (`\/`) so it is not interpreted as the end of the regular expression. Additionally, because the selector is inside a JSON string, the backslash character itself needs to be escaped (`\\`).

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"eslint-scope": "^9.1.0",
124124
"eslint-visitor-keys": "^5.0.0",
125125
"espree": "^11.1.0",
126-
"esquery": "^1.5.0",
126+
"esquery": "^1.7.0",
127127
"esutils": "^2.0.2",
128128
"fast-deep-equal": "^3.1.3",
129129
"file-entry-cache": "^8.0.0",

‎tests/lib/rules/no-restricted-syntax.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,5 +361,18 @@ ruleTester.run("no-restricted-syntax", rule, {
361361
},
362362
],
363363
},
364+
{
365+
code: "import values from 'some/path';",
366+
options: ["ImportDeclaration[source.value=/^some\\/path$/]"],
367+
errors: [
368+
{
369+
messageId: "restrictedSyntax",
370+
data: {
371+
message:
372+
"Using 'ImportDeclaration[source.value=/^some\\/path$/]' is not allowed.",
373+
},
374+
},
375+
],
376+
},
364377
],
365378
});

0 commit comments

Comments
 (0)