Skip to content

Commit 8cac42f

Browse files
Handle matchAll().take() and .drop() capture usage (#1042)
* Handle matchAll().take() and .drop() capture usage * Update matchall-take-drop-captures.md --------- Co-authored-by: Yosuke Ota <[email protected]>
1 parent 79059b5 commit 8cac42f

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": minor
3+
---
4+
5+
Handle matchAll().take()/.drop() capture usage

‎lib/utils/extract-capturing-group-references.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ type ExtractCapturingGroupReferencesContext = {
137137
}
138138

139139
type ArrayMethodName =
140-
Exclude<keyof unknown[], "length" | symbol | number> | "toArray"
140+
| Exclude<keyof unknown[], "length" | symbol | number>
141+
| "toArray"
142+
| "take"
143+
| "drop"
141144
const WELL_KNOWN_ARRAY_METHODS: {
142145
[key in ArrayMethodName]: {
143146
// If specified, the method receives a function that iterates the element.
@@ -194,6 +197,8 @@ const WELL_KNOWN_ARRAY_METHODS: {
194197
with: { result: "array" },
195198
// Iterator helpers
196199
toArray: { result: "array" },
200+
take: { result: "iterator" },
201+
drop: { result: "iterator" },
197202
}
198203

199204
/**

‎tests/lib/rules/__snapshots__/no-unused-capturing-group.ts.eslintsnap‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,25 @@ Output:
748748
---
749749

750750

751+
Test: no-unused-capturing-group >> invalid
752+
Options:
753+
- fixable: true
754+
755+
Code:
756+
1 | const bs = 'abc_abc'.matchAll(/a(b)/g).take(2).map(m => m[0])
757+
| ^~~ [1]
758+
759+
Output:
760+
1 | const bs = 'abc_abc'.matchAll(/a(?:b)/g).take(2).map(m => m[0])
761+
762+
[1] Capturing group number 1 is defined but never used.
763+
Suggestions:
764+
- Making this a non-capturing group.
765+
Output:
766+
1 | const bs = 'abc_abc'.matchAll(/a(?:b)/g).take(2).map(m => m[0])
767+
---
768+
769+
751770
Test: no-unused-capturing-group >> invalid
752771
Options:
753772
- fixable: true

‎tests/lib/rules/no-unused-capturing-group.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ tester.run("no-unused-capturing-group", rule, {
139139
const bs = 'abc_abc'.matchAll(/a(b)/g).toArray().map(m => m[1])
140140
`,
141141
`
142+
for (const m of 'abc_abc'.matchAll(/a(b)(c)/g).take(2)) console.log(m[1], m[2])
143+
`,
144+
`
145+
'abc_abc'.matchAll(/a(b)(c)/g).drop(1).toArray().forEach(m => console.log(m[1], m[2]))
146+
`,
147+
`
142148
const bs = [...'abc_abc'.matchAll(/a(b)/g)].map(m => m[1])
143149
`,
144150
`
@@ -309,6 +315,10 @@ tester.run("no-unused-capturing-group", rule, {
309315
code: "const bs = 'abc_abc'.matchAll(/a(b)/g).toArray().map(m => m[0])",
310316
options: [{ fixable: true }],
311317
},
318+
{
319+
code: "const bs = 'abc_abc'.matchAll(/a(b)/g).take(2).map(m => m[0])",
320+
options: [{ fixable: true }],
321+
},
312322
{
313323
code: `
314324
;[...'abc_abc'.matchAll(/a(b)(c)/g)].forEach(m => console.log(m[1]))

0 commit comments

Comments
 (0)