Skip to content

Commit eebcfc5

Browse files
Improved error message and docs for regexp/no-useless-non-capturing-group (#668)
* Improved error message and docs for `regexp/no-useless-non-capturing-group` * Create flat-snakes-raise.md * Updated tests
1 parent 3ad2a1a commit eebcfc5

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

.changeset/flat-snakes-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": patch
3+
---
4+
5+
Improved error message and docs for `regexp/no-useless-non-capturing-group`

docs/rules/no-useless-non-capturing-group.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ since: "v0.4.0"
1717
1818
## :book: Rule Details
1919

20-
This rule reports unnecessary non-capturing group
20+
This rule reports unnecessary non-capturing groups. Unnecessary groups are just clutter that make regexes harder to read, so they should be removed.
2121

2222
<eslint-code-block fix>
2323

@@ -34,6 +34,7 @@ var foo = /(?:abcd)/.test(str)
3434
var foo = /(?:[a-d])/.test(str)
3535
var foo = /(?:[a-d])|e/.test(str)
3636
var foo = /(?:a|(?:b|c)|d)/.test(str)
37+
var foo = /a(?:b)+/.test(str)
3738
```
3839

3940
</eslint-code-block>
@@ -49,7 +50,7 @@ var foo = /(?:a|(?:b|c)|d)/.test(str)
4950
```
5051

5152
- `"allowTop"`:
52-
Whether a top-level non-capturing group is allowed. Defaults to `"partial"`.
53+
Whether a top-level non-capturing group is allowed (e.g. `/(?:foo|bar)/`). Defaults to `"partial"`.
5354

5455
Sometimes it's useful to wrap a whole pattern into a non-capturing group (e.g. when the pattern is used as a building block to construct more complex patterns). Use this option to allow top-level non-capturing groups.
5556
- `"partial"`:

lib/rules/no-useless-non-capturing-group.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export default createRule("no-useless-non-capturing-group", {
4949
},
5050
],
5151
messages: {
52-
unexpected: "Unexpected quantifier Non-capturing group.",
52+
unexpected:
53+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
5354
},
5455
type: "suggestion", // "problem",
5556
},

tests/lib/rules/no-useless-non-capturing-group.ts

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ tester.run("no-useless-non-capturing-group", rule as any, {
8383
output: `/abcd/.test(str)`,
8484
errors: [
8585
{
86-
message: "Unexpected quantifier Non-capturing group.",
86+
message:
87+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
8788
line: 1,
8889
column: 2,
8990
endLine: 1,
@@ -96,7 +97,8 @@ tester.run("no-useless-non-capturing-group", rule as any, {
9697
output: `/abcd/v.test(str)`,
9798
errors: [
9899
{
99-
message: "Unexpected quantifier Non-capturing group.",
100+
message:
101+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
100102
line: 1,
101103
column: 2,
102104
endLine: 1,
@@ -109,7 +111,8 @@ tester.run("no-useless-non-capturing-group", rule as any, {
109111
output: `/[abcd]/.test(str)`,
110112
errors: [
111113
{
112-
message: "Unexpected quantifier Non-capturing group.",
114+
message:
115+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
113116
line: 1,
114117
column: 2,
115118
endLine: 1,
@@ -120,19 +123,24 @@ tester.run("no-useless-non-capturing-group", rule as any, {
120123
{
121124
code: `/(?:ab|cd)/.test(str)`,
122125
output: `/ab|cd/.test(str)`,
123-
errors: ["Unexpected quantifier Non-capturing group."],
126+
errors: [
127+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
128+
],
124129
},
125130
{
126131
code: `/a(?:ab|(?:.|a|b))/`,
127132
output: `/a(?:ab|.|a|b)/`,
128-
errors: ["Unexpected quantifier Non-capturing group."],
133+
errors: [
134+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
135+
],
129136
},
130137
{
131138
code: `/(?:[abcd]+?)/.test(str)`,
132139
output: `/[abcd]+?/.test(str)`,
133140
errors: [
134141
{
135-
message: "Unexpected quantifier Non-capturing group.",
142+
message:
143+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
136144
line: 1,
137145
column: 2,
138146
},
@@ -143,12 +151,14 @@ tester.run("no-useless-non-capturing-group", rule as any, {
143151
output: String.raw`/0/.test(str); /\1(?:0)/.test(str); /1/.test(str); /\1(?:1)/.test(str)`,
144152
errors: [
145153
{
146-
message: "Unexpected quantifier Non-capturing group.",
154+
message:
155+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
147156
line: 1,
148157
column: 2,
149158
},
150159
{
151-
message: "Unexpected quantifier Non-capturing group.",
160+
message:
161+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
152162
line: 1,
153163
column: 42,
154164
},
@@ -157,7 +167,9 @@ tester.run("no-useless-non-capturing-group", rule as any, {
157167
{
158168
code: String.raw`/(?:a\n)/.test(str)`,
159169
output: String.raw`/a\n/.test(str)`,
160-
errors: ["Unexpected quantifier Non-capturing group."],
170+
errors: [
171+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
172+
],
161173
},
162174
{
163175
code: String.raw`
@@ -166,61 +178,83 @@ tester.run("no-useless-non-capturing-group", rule as any, {
166178
output: String.raw`
167179
const s = "a\\n"
168180
;(new RegExp(s)).test(str)`,
169-
errors: ["Unexpected quantifier Non-capturing group."],
181+
errors: [
182+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
183+
],
170184
},
171185
{
172186
code: String.raw`
173187
const s = "(?:a"+"\\n)"
174188
;(new RegExp(s)).test(str)`,
175189
output: null,
176-
errors: ["Unexpected quantifier Non-capturing group."],
190+
errors: [
191+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
192+
],
177193
},
178194

179195
{
180196
code: `/(?:a)/.test(str)`,
181197
output: `/a/.test(str)`,
182-
errors: ["Unexpected quantifier Non-capturing group."],
198+
errors: [
199+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
200+
],
183201
},
184202
{
185203
code: String(/(?:a)+/),
186204
output: String(/a+/),
187-
errors: ["Unexpected quantifier Non-capturing group."],
205+
errors: [
206+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
207+
],
188208
},
189209
{
190210
code: String.raw`/(?:\w)/.test(str)`,
191211
output: String.raw`/\w/.test(str)`,
192-
errors: ["Unexpected quantifier Non-capturing group."],
212+
errors: [
213+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
214+
],
193215
},
194216
{
195217
code: String(/(?:[abc])*/),
196218
output: String(/[abc]*/),
197-
errors: ["Unexpected quantifier Non-capturing group."],
219+
errors: [
220+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
221+
],
198222
},
199223
{
200224
code: String(/foo(?:[abc]*)bar/),
201225
output: String(/foo[abc]*bar/),
202-
errors: ["Unexpected quantifier Non-capturing group."],
226+
errors: [
227+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
228+
],
203229
},
204230
{
205231
code: String(/foo(?:bar)/),
206232
output: String(/foobar/),
207-
errors: ["Unexpected quantifier Non-capturing group."],
233+
errors: [
234+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
235+
],
208236
},
209237
{
210238
code: `/(?:a|b)/.test(str)`,
211239
output: `/a|b/.test(str)`,
212-
errors: ["Unexpected quantifier Non-capturing group."],
240+
errors: [
241+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
242+
],
213243
},
214244
{
215245
code: String(/a|(?:b|c)/),
216246
output: String(/a|b|c/),
217-
errors: ["Unexpected quantifier Non-capturing group."],
247+
errors: [
248+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
249+
],
218250
},
219251
{
220252
code: String(/a|(?:b|c)/),
221253
output: String(/a|b|c/),
222254
options: [{ allowTop: "always" }],
223-
errors: ["Unexpected quantifier Non-capturing group."],
255+
errors: [
256+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
257+
],
224258
},
225259
{
226260
code: `
@@ -238,7 +272,9 @@ tester.run("no-useless-non-capturing-group", rule as any, {
238272
// { allowTop: "partial" }
239273
`,
240274
options: [{ allowTop: "partial" }],
241-
errors: ["Unexpected quantifier Non-capturing group."],
275+
errors: [
276+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
277+
],
242278
},
243279
{
244280
code: `
@@ -250,7 +286,9 @@ tester.run("no-useless-non-capturing-group", rule as any, {
250286
`,
251287
output: null,
252288
options: [{ allowTop: "never" }],
253-
errors: ["Unexpected quantifier Non-capturing group."],
289+
errors: [
290+
"Unexpected unnecessary non-capturing group. This group can be removed without changing the behaviour of the regex.",
291+
],
254292
},
255293
],
256294
})

0 commit comments

Comments
 (0)