-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathno-empty-capturing-group.ts
42 lines (40 loc) · 1.25 KB
/
no-empty-capturing-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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { isZeroLength } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
export default createRule("no-empty-capturing-group", {
meta: {
docs: {
description: "disallow capturing group that captures empty.",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
unexpected: "Unexpected capture empty.",
},
type: "suggestion",
},
create(context) {
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onCapturingGroupEnter(cgNode) {
if (isZeroLength(cgNode, flags)) {
context.report({
node,
loc: getRegexpLocation(cgNode),
messageId: "unexpected",
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})