-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-named-capture-group.ts
46 lines (43 loc) · 1.43 KB
/
prefer-named-capture-group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { mention } from "../utils/mention"
export default createRule("prefer-named-capture-group", {
meta: {
docs: {
description: "enforce using named capture groups",
category: "Stylistic Issues",
recommended: false,
},
schema: [],
messages: {
required:
"Capture group {{group}} should be converted to a named or non-capturing group.",
},
type: "suggestion",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation } = regexpContext
return {
onCapturingGroupEnter(cgNode) {
if (cgNode.name === null) {
context.report({
node,
loc: getRegexpLocation(cgNode),
messageId: "required",
data: {
group: mention(cgNode),
},
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})