-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-useless-non-capturing-group.ts
149 lines (142 loc) · 5.2 KB
/
no-useless-non-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type { Group } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { canUnwrapped, createRule, defineRegexpVisitor } from "../utils"
import { UsageOfPattern } from "../utils/get-usage-of-pattern"
/**
* Returns whether the given group is the top-level group of its pattern.
*
* A pattern with a top-level groups is of the form `/(?:...)/flags`.
*/
function isTopLevel(group: Group): boolean {
const parent = group.parent
if (parent.type === "Alternative" && parent.elements.length === 1) {
const parentParent = parent.parent
if (
parentParent.type === "Pattern" &&
parentParent.alternatives.length === 1
) {
return true
}
}
return false
}
export default createRule("no-useless-non-capturing-group", {
meta: {
docs: {
description: "disallow unnecessary non-capturing group",
category: "Stylistic Issues",
recommended: true,
},
fixable: "code",
schema: [
{
type: "object",
properties: {
allowTop: {
anyOf: [
{
// backward compatibility
type: "boolean",
},
{ enum: ["always", "never", "partial"] },
],
},
},
additionalProperties: false,
},
],
messages: {
unexpected: "Unexpected quantifier Non-capturing group.",
},
type: "suggestion", // "problem",
},
create(context) {
const allowTop: "always" | "never" | "partial" =
context.options[0]?.allowTop === true
? "always"
: context.options[0]?.allowTop === false
? "never"
: context.options[0]?.allowTop ?? "partial"
/**
* Create visitor
*/
function createVisitor({
node,
getRegexpLocation,
fixReplaceNode,
getUsageOfPattern,
}: RegExpContext): RegExpVisitor.Handlers {
let isIgnored: (gNode: Group) => boolean
if (allowTop === "always") {
isIgnored = isTopLevel
} else if (allowTop === "partial") {
if (getUsageOfPattern() !== UsageOfPattern.whole) {
isIgnored = isTopLevel
} else {
isIgnored = () => false
}
} else {
// allowTop === "never"
isIgnored = () => false
}
return {
onGroupEnter(gNode) {
if (isIgnored(gNode)) {
return
}
if (gNode.alternatives.length === 1) {
const alt = gNode.alternatives[0]
if (alt.elements.length === 0) {
// Ignore empty groups. You can check with another rule.
// e.g. /(?:)/
return
}
const parent = gNode.parent
if (
parent.type === "Quantifier" &&
(alt.elements.length > 1 ||
alt.elements[0].type === "Quantifier")
) {
// e.g. /(?:ab)?/
return
}
if (!canUnwrapped(gNode, alt.raw)) {
return
}
} else {
// the group might still be useless
// e.g. /a(?:b|(?:c|d)|e)/
const parent = gNode.parent
if (parent.type !== "Alternative") {
return
}
if (parent.elements.length !== 1) {
return
}
}
context.report({
node,
loc: getRegexpLocation(gNode, [0, 3]),
messageId: "unexpected",
fix: fixReplaceNode(gNode, () => {
if (
allowTop === "never" &&
isTopLevel(gNode) &&
getUsageOfPattern() !== UsageOfPattern.whole
) {
// Top-level group and potentially partially used patterns
// do not autofix because they can cause side effects.
return null
}
return gNode.raw.slice(3, -1)
}),
})
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})