-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuse-ignore-case.ts
309 lines (276 loc) · 9.56 KB
/
use-ignore-case.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import type {
CharacterClass,
CharacterClassElement,
Node,
StringAlternative,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { Rule } from "eslint"
import { CharSet, JS } from "refa"
import { Chars, toUnicodeSet } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import type {
PatternSource,
PatternRange,
} from "../utils/ast-utils/pattern-source"
import { UsageOfPattern } from "../utils/get-usage-of-pattern"
import { mention } from "../utils/mention"
import { getIgnoreCaseFlags, isCaseVariant } from "../utils/regexp-ast"
import { cachedFn } from "../utils/util"
type FlatClassElement = CharacterClassElement | StringAlternative
const ELEMENT_ORDER: Record<FlatClassElement["type"], number> = {
Character: 1,
CharacterClassRange: 2,
CharacterSet: 3,
CharacterClass: 4,
ExpressionCharacterClass: 5,
ClassStringDisjunction: 6,
StringAlternative: 7,
}
/**
* Finds all character class elements that do not contribute to the whole.
*/
function findUseless(
elements: readonly FlatClassElement[],
getChars: (e: FlatClassElement) => JS.UnicodeSet,
other: JS.UnicodeSet,
): Set<FlatClassElement> {
const get = cachedFn(getChars)
// When searching for useless elements, we want to first
// search for useless characters, then useless ranges, and
// finally useless sets.
const sortedElements = [...elements]
.reverse()
.sort((a, b) => ELEMENT_ORDER[a.type] - ELEMENT_ORDER[b.type])
const useless = new Set<FlatClassElement>()
for (const e of sortedElements) {
const cs = get(e)
if (cs.isSubsetOf(other)) {
useless.add(e)
continue
}
// the total of all other elements
const otherElements = elements.filter((o) => o !== e && !useless.has(o))
const total = other.union(...otherElements.map(get))
if (cs.isSubsetOf(total)) {
useless.add(e)
continue
}
}
return useless
}
/** Returns all elements not in the given set */
function without<T>(iter: Iterable<T>, set: ReadonlySet<T>): T[] {
const result: T[] = []
for (const item of iter) {
if (!set.has(item)) {
result.push(item)
}
}
return result
}
/**
* Removes all the given nodes from the given pattern.
*/
function removeAll(
fixer: Rule.RuleFixer,
patternSource: PatternSource,
nodes: readonly Node[],
) {
// we abuse CharSet to merge adjacent and overlapping ranges
const charSet = CharSet.empty(Number.MAX_SAFE_INTEGER).union(
nodes.map((n) => {
let min = n.start
let max = n.end - 1
if (n.type === "StringAlternative") {
const parent = n.parent
if (
parent.alternatives.length === 1 ||
parent.alternatives.every((a) => nodes.includes(a))
) {
// we have to remove the whole disjunction
min = parent.start
max = parent.end - 1
} else {
const isFirst = parent.alternatives.at(0) === n
if (isFirst) {
max++
} else {
min--
}
}
}
return { min, max }
}),
)
const sorted = charSet.ranges.map(
({ min, max }): PatternRange => ({ start: min, end: max + 1 }),
)
let pattern = patternSource.value
let removed = 0
for (const { start, end } of sorted) {
pattern =
pattern.slice(0, start - removed) + pattern.slice(end - removed)
removed += end - start
}
const range = patternSource.getReplaceRange({
start: 0,
end: patternSource.value.length,
})
if (range) {
return range.replace(fixer, pattern)
}
return null
}
/**
* Adds the `i` flag to the given flags string.
*/
function getIgnoreCaseFlagsString(flags: string): string {
if (flags.includes("i")) {
return flags
}
// keep flags sorted
for (let i = 0; i < flags.length; i++) {
if (flags[i] > "i") {
return `${flags.slice(0, i)}i${flags.slice(i)}`
}
}
return `${flags}i`
}
export default createRule("use-ignore-case", {
meta: {
docs: {
description: "use the `i` flag if it simplifies the pattern",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"The character class(es) {{ classes }} can be simplified using the `i` flag.",
},
type: "suggestion",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const {
node,
flags,
ownsFlags,
flagsString,
patternAst,
patternSource,
getUsageOfPattern,
getFlagsLocation,
fixReplaceFlags,
} = regexpContext
if (!ownsFlags || flagsString === null) {
// It's not (statically) fixable
return {}
}
if (flags.ignoreCase) {
// We can't suggest the i flag if it's already used
return {}
}
if (getUsageOfPattern() === UsageOfPattern.partial) {
// Adding flags to partial patterns isn't a good idea
return {}
}
if (isCaseVariant(patternAst, flags)) {
// We can't add the i flag
return {}
}
const uselessElements: FlatClassElement[] = []
const ccs: CharacterClass[] = []
return {
onCharacterClassEnter(ccNode) {
const elements = ccNode.elements.flatMap(
(e: CharacterClassElement): FlatClassElement[] => {
if (e.type === "ClassStringDisjunction") {
return e.alternatives
}
return [e]
},
)
const invariantElement = elements.filter(
(e) => !isCaseVariant(e, flags),
)
if (invariantElement.length === elements.length) {
// all elements are case invariant
return
}
const empty = JS.UnicodeSet.empty(Chars.maxChar(flags))
const invariant = empty.union(
...invariantElement.map((e) => toUnicodeSet(e, flags)),
)
let variantElements = without(
elements,
new Set(invariantElement),
)
// find all elements that are useless even without
// the i flag
const alwaysUseless = findUseless(
variantElements,
(e) => toUnicodeSet(e, flags),
invariant,
)
// remove useless elements
variantElements = without(variantElements, alwaysUseless)
// find useless elements
const iFlags = getIgnoreCaseFlags(flags)
const useless = findUseless(
variantElements,
(e) => toUnicodeSet(e, iFlags),
invariant,
)
uselessElements.push(...useless)
ccs.push(ccNode)
},
onPatternLeave() {
if (uselessElements.length === 0) {
return
}
context.report({
node,
loc: getFlagsLocation(),
messageId: "unexpected",
data: {
classes: ccs.map((cc) => mention(cc)).join(", "),
},
fix(fixer) {
const patternFix = removeAll(
fixer,
patternSource,
uselessElements,
)
if (!patternFix) {
return null
}
const flagsFix = fixReplaceFlags(
getIgnoreCaseFlagsString(flagsString),
false,
)(fixer)
if (!flagsFix) {
return null
}
const fix = [patternFix]
if (Array.isArray(flagsFix)) {
fix.push(...flagsFix)
} else {
fix.push(flagsFix)
}
return fix
},
})
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})