-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-empty-group.ts
50 lines (47 loc) · 1.48 KB
/
no-empty-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
47
48
49
50
import type { Group, CapturingGroup } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
export default createRule("no-empty-group", {
meta: {
docs: {
description: "disallow empty group",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
unexpected: "Unexpected empty group.",
},
type: "suggestion",
},
create(context) {
function verifyGroup(
{ node, getRegexpLocation }: RegExpContext,
gNode: Group | CapturingGroup,
) {
if (gNode.alternatives.every((alt) => alt.elements.length === 0)) {
context.report({
node,
loc: getRegexpLocation(gNode),
messageId: "unexpected",
})
}
}
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
return {
onGroupEnter(gNode) {
verifyGroup(regexpContext, gNode)
},
onCapturingGroupEnter(cgNode) {
verifyGroup(regexpContext, cgNode)
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})