-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsort-character-class-elements.ts
306 lines (289 loc) · 9.75 KB
/
sort-character-class-elements.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
import type {
CharacterClass,
CharacterClassElement,
ClassRangesCharacterClassElement,
UnicodePropertyCharacterSet,
UnicodeSetsCharacterClassElement,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { ReadonlyWord } from "refa"
import type { ReadonlyFlags } from "regexp-ast-analysis"
import { toUnicodeSet } from "regexp-ast-analysis"
import type { ObjectOption } from "../types"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { getLexicographicallySmallest } from "../utils/lexicographically-smallest"
import { mention } from "../utils/mention"
type CharacterClassElementKind =
| "\\w"
| "\\d"
| "\\s"
| "\\p"
| "*"
| "\\q"
| "[]"
const DEFAULT_ORDER: CharacterClassElementKind[] = [
"\\s",
"\\w",
"\\d",
"\\p",
"*",
"\\q",
"[]",
]
/**
* Get kind of CharacterClassElement for given CharacterClassElement
*/
function getCharacterClassElementKind(
node: CharacterClassElement,
): CharacterClassElementKind {
if (node.type === "CharacterSet") {
return node.kind === "word"
? "\\w"
: node.kind === "digit"
? "\\d"
: node.kind === "space"
? "\\s"
: "\\p"
}
if (node.type === "ClassStringDisjunction") {
return "\\q"
}
if (
node.type === "CharacterClass" ||
node.type === "ExpressionCharacterClass"
) {
return "[]"
}
return "*"
}
/**
* Return the lexicographically smallest string accepted by the given element.
* If the class set is negate, the original value is used for calculation.
*/
function getLexicographicallySmallestFromElement(
node: CharacterClassElement,
flags: ReadonlyFlags,
): ReadonlyWord {
const us =
node.type === "CharacterSet" && node.negate
? toUnicodeSet({ ...node, negate: false }, flags)
: toUnicodeSet(node, flags)
return getLexicographicallySmallest(us) || []
}
/**
* Compare two strings of char sets by byte order.
*/
function compareWords(a: ReadonlyWord, b: ReadonlyWord): number {
const l = Math.min(a.length, b.length)
for (let i = 0; i < l; i++) {
const aI = a[i]
const bI = b[i]
if (aI !== bI) return aI - bI
}
return a.length - b.length
}
export default createRule("sort-character-class-elements", {
meta: {
docs: {
description: "enforces elements order in character class",
category: "Stylistic Issues",
recommended: false,
},
fixable: "code",
schema: [
{
type: "object",
properties: {
order: {
type: "array",
items: {
enum: [
"\\s",
"\\w",
"\\d",
"\\p",
"*",
"\\q",
"[]",
],
},
},
},
additionalProperties: false,
},
],
messages: {
sortElements:
"Expected character class elements to be in ascending order. {{next}} should be before {{prev}}.",
},
type: "layout",
},
create(context) {
const orderOption: {
"*": number
"\\w"?: number
"\\d"?: number
"\\s"?: number
"\\p"?: number
"\\q"?: number
"[]"?: number
} = { "*": Infinity }
;(
((context.options[0] as ObjectOption)?.order ??
DEFAULT_ORDER) as CharacterClassElementKind[]
).forEach((o, i) => {
orderOption[o] = i + 1
})
function createVisitor({
node,
flags,
getRegexpLocation,
patternSource,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onCharacterClassEnter(ccNode) {
const prevList: CharacterClassElement[] = []
for (const next of ccNode.elements) {
if (prevList.length) {
const prev = prevList[0]
if (!isValidOrder(prev, next, flags)) {
let moveTarget = prev
for (const p of prevList) {
if (isValidOrder(p, next, flags)) {
break
} else {
moveTarget = p
}
}
context.report({
node,
loc: getRegexpLocation(next),
messageId: "sortElements",
data: {
next: mention(next),
prev: mention(moveTarget),
},
*fix(fixer) {
const nextRange =
patternSource.getReplaceRange(next)
const targetRange =
patternSource.getReplaceRange(
moveTarget,
)
if (!targetRange || !nextRange) {
return
}
yield targetRange.insertBefore(
fixer,
escapeRaw(next, moveTarget),
)
yield nextRange.remove(fixer)
},
})
}
}
prevList.unshift(next)
}
},
}
}
/**
* Check that the two given CharacterClassElements are in a valid order.
*/
function isValidOrder(
prev: CharacterClassElement,
next: CharacterClassElement,
flags: ReadonlyFlags,
) {
const prevKind = getCharacterClassElementKind(prev)
const nextKind = getCharacterClassElementKind(next)
const prevOrder = orderOption[prevKind] ?? orderOption["*"]
const nextOrder = orderOption[nextKind] ?? orderOption["*"]
if (prevOrder < nextOrder) {
return true
} else if (prevOrder > nextOrder) {
return false
}
const prevOrderShortCircuit = DEFAULT_ORDER.indexOf(prevKind)
const nextOrderShortCircuit = DEFAULT_ORDER.indexOf(nextKind)
if (prevOrderShortCircuit < nextOrderShortCircuit) {
return true
} else if (prevOrderShortCircuit > nextOrderShortCircuit) {
return false
}
if (
prev.type === "CharacterSet" &&
prev.kind === "property" &&
next.type === "CharacterSet" &&
next.kind === "property"
) {
return isValidOrderForUnicodePropertyCharacterSet(prev, next)
}
const prevWord = getLexicographicallySmallestFromElement(
prev,
flags,
)
const nextWord = getLexicographicallySmallestFromElement(
next,
flags,
)
if (compareWords(prevWord, nextWord) <= 0) {
return true
}
return false
}
/**
* Check that the two given UnicodePropertyCharacterSet are in a valid order.
*/
function isValidOrderForUnicodePropertyCharacterSet(
prev: UnicodePropertyCharacterSet,
next: UnicodePropertyCharacterSet,
) {
if (prev.key < next.key) {
return true
} else if (prev.key > next.key) {
return false
}
if (prev.value) {
if (next.value) {
if (prev.value <= next.value) {
return true
}
return false
}
return false
}
return true
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})
/**
* get the escape text from the given CharacterClassElement.
*/
function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) {
let raw = node.raw
if (raw.startsWith("-")) {
const parent = target.parent as CharacterClass
const elements: (
| UnicodeSetsCharacterClassElement
| ClassRangesCharacterClassElement
)[] = parent.elements
const prev = elements[elements.indexOf(target) - 1]
if (
prev &&
(prev.type === "Character" || prev.type === "CharacterSet")
) {
raw = `\\${raw}`
}
}
if (target.raw.startsWith("-")) {
if (node.type === "Character" || node.type === "CharacterSet") {
raw = `${raw}\\`
}
}
return raw
}