-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-quantifier.ts
187 lines (170 loc) · 6.42 KB
/
prefer-quantifier.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { Character, CharacterSet } from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import {
createRule,
defineRegexpVisitor,
isDigit,
isLetter,
isSymbol,
} from "../utils"
import type { PatternRange } from "../utils/ast-utils/pattern-source"
import { quantToString } from "../utils/regexp-ast"
type CharTarget = CharacterSet | Character
class CharBuffer {
public target: CharTarget
public elements: CharTarget[]
public times: number
public equalChar: (element: CharTarget) => boolean
public constructor(target: CharTarget) {
this.target = target
this.elements = [target]
this.times = 1
if (target.type === "CharacterSet") {
if (target.kind === "any") {
this.equalChar = (e) =>
e.type === "CharacterSet" && e.kind === "any"
} else if (target.kind === "property") {
this.equalChar = (e) =>
e.type === "CharacterSet" &&
e.kind === "property" &&
e.key === target.key &&
e.value === target.value &&
e.negate === target.negate
} else {
// Escape
this.equalChar = (e) =>
e.type === "CharacterSet" &&
e.kind === target.kind &&
e.negate === target.negate
}
} else {
this.equalChar = (e) =>
e.type === "Character" && e.value === target.value
}
}
public addElement(element: CharTarget) {
this.elements.push(element)
this.times += 1
}
public isValid(): boolean {
if (this.elements.length < 2) {
return true
}
let charKind: "digit" | "letter" | "symbol" | null = null
for (const element of this.elements) {
if (element.type === "Character") {
if (charKind == null) {
if (isDigit(element.value)) {
charKind = "digit"
} else if (isLetter(element.value)) {
charKind = "letter"
} else if (isSymbol(element.value)) {
charKind = "symbol"
} else {
return false
}
}
} else {
return false
}
}
if (
// It is valid when the same numbers are consecutive.
charKind === "digit" ||
// It is valid when the same letter character continues twice.
(charKind === "letter" && this.elements.length <= 2) ||
// It is valid when the same symbol character continues three times.
(charKind === "symbol" && this.elements.length <= 3)
) {
return true
}
return false
}
public getQuantifier(): string {
return quantToString({ min: this.times, max: this.times })
}
}
export default createRule("prefer-quantifier", {
meta: {
docs: {
description: "enforce using quantifier",
category: "Best Practices",
recommended: false,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"Unexpected consecutive same {{type}}. Use '{{quantifier}}' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
function createVisitor({
node,
patternSource,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onAlternativeEnter(aNode) {
let charBuffer: CharBuffer | null = null
for (const element of aNode.elements) {
if (
element.type === "CharacterSet" ||
element.type === "Character"
) {
if (charBuffer && charBuffer.equalChar(element)) {
charBuffer.addElement(element)
} else {
validateBuffer(charBuffer)
charBuffer = new CharBuffer(element)
}
} else {
validateBuffer(charBuffer)
charBuffer = null
}
}
validateBuffer(charBuffer)
function validateBuffer(buffer: CharBuffer | null) {
if (!buffer || buffer.isValid()) {
return
}
const bufferRange: PatternRange = {
start: buffer.elements[0].start,
end: buffer.elements[buffer.elements.length - 1]
.end,
}
context.report({
node,
loc: patternSource.getAstLocation(bufferRange),
messageId: "unexpected",
data: {
type:
buffer.target.type === "Character"
? "characters"
: buffer.target.kind === "any"
? "any characters"
: "character class escapes",
quantifier: buffer.getQuantifier(),
},
fix(fixer) {
const range =
patternSource.getReplaceRange(bufferRange)
if (!range) {
return null
}
return range.replace(
fixer,
buffer.target.raw + buffer.getQuantifier(),
)
},
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})