-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsort-alternatives.ts
797 lines (716 loc) · 23.8 KB
/
sort-alternatives.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
import type {
Alternative,
Character,
CharacterClass,
CharacterSet,
ClassStringDisjunction,
Element,
ExpressionCharacterClass,
Pattern,
StringAlternative,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { CharSet, Word, ReadonlyWord } from "refa"
import { NFA, JS, transform } from "refa"
import type {
GetLongestPrefixOptions,
ReadonlyFlags,
} from "regexp-ast-analysis"
import {
Chars,
hasSomeDescendant,
canReorder,
getLongestPrefix,
getConsumedChars,
toUnicodeSet,
hasStrings,
} from "regexp-ast-analysis"
import {
CP_MINUS,
CP_PLUS,
CP_STAR,
CP_QUESTION,
CP_SLASH,
CP_SPACE,
CP_APOSTROPHE,
createRule,
defineRegexpVisitor,
} from "../utils"
import type { RegExpContext } from "../utils"
import { getLexicographicallySmallestInConcatenation } from "../utils/lexicographically-smallest"
import { getParser, assertValidFlags } from "../utils/refa"
interface AllowedChars {
allowed: CharSet
required: CharSet
}
const cache = new Map<string, Readonly<AllowedChars>>()
function getAllowedChars(flags: ReadonlyFlags) {
assertValidFlags(flags)
const cacheKey =
(flags.ignoreCase ? "i" : "") +
(flags.unicode ? "u" : "") +
(flags.unicodeSets ? "v" : "")
let result = cache.get(cacheKey)
if (result === undefined) {
result = {
allowed: JS.createCharSet(
[
{ kind: "word", negate: false },
{ min: CP_SPACE, max: CP_SPACE },
// common punctuation and operators
{ min: CP_PLUS, max: CP_PLUS },
{ min: CP_MINUS, max: CP_MINUS },
{ min: CP_STAR, max: CP_STAR },
{ min: CP_SLASH, max: CP_SLASH },
{ min: CP_APOSTROPHE, max: CP_APOSTROPHE },
{ min: CP_QUESTION, max: CP_QUESTION },
],
flags,
),
required: Chars.word(flags),
}
cache.set(cacheKey, result)
}
return result
}
/**
* Returns whether the given element contains only literal characters and
* groups/other elements containing literal characters.
*/
function containsOnlyLiterals(
element: Element | Pattern | Alternative,
): boolean {
return !hasSomeDescendant(
element,
(d) => {
return (
d.type === "Backreference" ||
d.type === "CharacterSet" ||
(d.type === "Quantifier" && d.max === Infinity) ||
(d.type === "CharacterClass" && d.negate) ||
(d.type === "ExpressionCharacterClass" && d.negate)
)
},
(d) => d.type !== "Assertion",
)
}
const lssCache = new WeakMap<Alternative, ReadonlyWord>()
/**
* A cached version of {@link approximateLexicographicallySmallest}.
*/
function cachedApproximateLexicographicallySmallest(
alternative: Alternative,
parser: JS.Parser,
flags: ReadonlyFlags,
): ReadonlyWord {
let cached = lssCache.get(alternative)
if (cached === undefined) {
cached = approximateLexicographicallySmallest(
alternative,
parser,
flags,
)
lssCache.set(alternative, cached)
}
return cached
}
const LONGEST_PREFIX_OPTIONS: GetLongestPrefixOptions = {
includeAfter: true,
onlyInside: true,
looseGroups: true,
}
/**
* Return an approximation of the lexicographically smallest string (LSS)
* accepted by the given alternative.
*
* If the LSS is defined for the given alternative and shorter than 1000
* characters, then the LSS will be returned. Otherwise, a prefix-based
* approximation will be returned.
*
* Assertions will be ignored when computing the LSS.
*
* Backreferences will be disabled when computing the LSS, but the prefix-based
* approximation will account for them.
*/
function approximateLexicographicallySmallest(
alternative: Alternative,
parser: JS.Parser,
flags: ReadonlyFlags,
): Word {
const lss = getLexicographicallySmallestFromAlternative(
alternative,
parser,
flags,
)
if (lss !== undefined) return lss
// prefix-based approximation
const prefix = getLongestPrefix(
alternative,
"ltr",
flags,
LONGEST_PREFIX_OPTIONS,
)
return getLexicographicallySmallestFromCharSets(prefix)
}
function getLexicographicallySmallestFromAlternative(
alternative: Alternative,
parser: JS.Parser,
flags: ReadonlyFlags,
): Word | undefined
function getLexicographicallySmallestFromAlternative(
alternative: StringAlternative,
parser: JS.Parser,
flags: ReadonlyFlags,
): Word
/**
* If defined, this will return the lexicographically smallest string accepted
* by the given alternative (ignoring assertions).
*/
function getLexicographicallySmallestFromAlternative(
alternative: Alternative | StringAlternative,
parser: JS.Parser,
flags: ReadonlyFlags,
): Word | undefined {
if (
alternative.type === "StringAlternative" ||
hasOnlyCharacters(alternative, flags)
) {
// fast path to avoid converting simple alternatives into NFAs
const smallest: Word = []
for (const e of alternative.elements) {
const cs = toUnicodeSet(e, flags).chars
if (cs.isEmpty) return undefined
smallest.push(cs.ranges[0].min)
}
return smallest
}
if (isOnlyCharacterElements(alternative.elements)) {
return getLexicographicallySmallestInConcatenation(
alternative.elements.map((e) => toUnicodeSet(e, flags)),
)
}
try {
const result = parser.parseElement(alternative, {
assertions: "unknown",
backreferences: "disable",
maxBackreferenceWords: 4,
maxNodes: 1000,
})
// remove all unknowns (assertions)
const expression = transform(
{
onConcatenation(concat) {
concat.elements = concat.elements.filter(
(e) => e.type !== "Unknown",
)
},
},
result.expression,
)
const nfa = NFA.fromRegex(
expression,
{ maxCharacter: result.maxCharacter },
{},
new NFA.LimitedNodeFactory(1000),
)
return getLexicographicallySmallestFromNfa(nfa.initial, nfa.finals)
} catch {
return undefined
}
}
/**
* Returns whether the given array of nodes contains only characters.
* But note that if the pattern has the v flag, the character class may contain strings.
*/
function isOnlyCharacterElements(
nodes: Element[],
): nodes is (
| Character
| CharacterClass
| CharacterSet
| ExpressionCharacterClass
)[] {
return nodes.every(
(e) =>
e.type === "Character" ||
e.type === "CharacterClass" ||
e.type === "CharacterSet" ||
e.type === "ExpressionCharacterClass",
)
}
/**
* Returns whether the given alternative has contains only characters.
* The v flag in the pattern does not contains the string.
*/
function hasOnlyCharacters(
alternative: Alternative,
flags: ReadonlyFlags,
): alternative is Alternative & {
elements: readonly (
| Character
| CharacterClass
| CharacterSet
| ExpressionCharacterClass
)[]
} {
return (
isOnlyCharacterElements(alternative.elements) &&
alternative.elements.every((e) => !hasStrings(e, flags))
)
}
/**
* If defined, this will return the lexicographically smallest string accepted
* by the given NFA.
*/
function getLexicographicallySmallestFromNfa(
initial: NFA.ReadonlyNode,
finals: ReadonlySet<NFA.ReadonlyNode>,
): Word | undefined {
// this is a variation on Thompson's algorithm
const smallest: Word = []
let currentStates = [initial]
const newStatesSet = new Set<NFA.ReadonlyNode>()
const MAX_LENGTH = 1000
for (let i = 0; i < MAX_LENGTH; i++) {
if (currentStates.some((n) => finals.has(n))) {
// one of the current states is a final state
return smallest
}
// find the smallest character
let min = Infinity
for (const state of currentStates) {
// eslint-disable-next-line no-loop-func -- false positive
state.out.forEach((charSet) => {
if (!charSet.isEmpty) {
min = Math.min(min, charSet.ranges[0].min)
}
})
}
if (min === Infinity) {
// the NFA doesn't accept any words
return undefined
}
smallest.push(min)
const newStates: NFA.ReadonlyNode[] = []
newStatesSet.clear()
for (const state of currentStates) {
// eslint-disable-next-line no-loop-func -- false positive
state.out.forEach((charSet, to) => {
if (charSet.has(min) && !newStatesSet.has(to)) {
newStates.push(to)
newStatesSet.add(to)
}
})
}
currentStates = newStates
}
// the lexicographically smallest string either has more than
// MAX_LENGTH characters or doesn't exist.
return undefined
}
/**
* If defined, this will return the lexicographically smallest string accepted
* by the given sequence of character sets.
*
* If any of the given character sets is empty, the current smallest will be
* returned.
*/
function getLexicographicallySmallestFromCharSets(
word: Iterable<CharSet>,
): Word {
const result: Word = []
for (const set of word) {
if (set.isEmpty) break
result.push(set.ranges[0].min)
}
return result
}
/**
* Compare two string independent of the current locale by byte order.
*/
function compareByteOrder(a: string, b: string): number {
if (a === b) {
return 0
}
return a < b ? -1 : +1
}
/**
* Compare two char sets by byte order.
*/
function compareCharSets(a: CharSet, b: CharSet): number {
// The basic idea here is the following:
// We want to sort the two sets based on their characters. To do that, we
// will consider the sort lists of characters (see `CharSet#characters()`)
// of the two sets respectively. We will then lexicographically compare
// these lists of characters.
// Obviously, we don't actually look at the full list of characters.
// CharSets are represented as ranges, and we will take advantage of that.
// In lexicographical sorting, we just have to find the first character
// that differs in the two sequences, and that's quite simple to do in the
// range representation. Further, if one sequence ends before that
// character was found, we compare the length of the two sequences. That is
// trivial to do in the range form as well.
const aRanges = a.ranges
const bRanges = b.ranges
for (let i = 0; i < aRanges.length && i < bRanges.length; i++) {
const aR = aRanges[i]
const bR = bRanges[i]
if (aR.min !== bR.min) return aR.min - bR.min
if (aR.max !== bR.max) {
if (aR.max < bR.max) {
// [aR.min .. aR.max] [...?]
// [bR.min .. aR.max .. bR.max]
// If there is another range for a, then a is larger than b
return i + 1 < aRanges.length ? +1 : -1
// eslint-disable-next-line no-else-return -- x
} else {
// [aR.min .. bR.max .. aR.max]
// [bR.min .. bR.max] [...?]
// If there is another range for b, then a is smaller than b
return i + 1 < bRanges.length ? -1 : +1
}
}
}
return aRanges.length - bRanges.length
}
/**
* Compare two strings of char sets by byte order.
*/
function compareCharSetStrings(
a: readonly CharSet[],
b: readonly CharSet[],
): number {
const l = Math.min(a.length, b.length)
for (let i = 0; i < l; i++) {
const diff = compareCharSets(a[i], b[i])
if (diff !== 0) {
return diff
}
}
return a.length - b.length
}
/**
* 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
}
/**
* Sorts the given alternatives.
*
* The comparison function implemented by this function has 3 parts:
*
* 1) Comparison based on the lexicographically smallest strings (LSS) accepted
* by the alternatives.
* 2) Comparison based on the longest prefix of the alternatives.
* 3) Comparison based on the raw source code of the alternatives.
*
* For more information on why we use LSS-based comparison and how it works,
* see https://github.com/ota-meshi/eslint-plugin-regexp/pull/423.
*/
function sortAlternatives(
alternatives: Alternative[],
parser: JS.Parser,
flags: ReadonlyFlags,
): void {
alternatives.sort((a, b) => {
const lssDiff = compareWords(
cachedApproximateLexicographicallySmallest(a, parser, flags),
cachedApproximateLexicographicallySmallest(b, parser, flags),
)
if (lssDiff !== 0) {
return lssDiff
}
const prefixDiff = compareCharSetStrings(
getLongestPrefix(a, "ltr", flags, LONGEST_PREFIX_OPTIONS),
getLongestPrefix(b, "ltr", flags, LONGEST_PREFIX_OPTIONS),
)
if (prefixDiff !== 0) {
return prefixDiff
}
if (flags.ignoreCase) {
return (
compareByteOrder(a.raw.toUpperCase(), b.raw.toUpperCase()) ||
compareByteOrder(a.raw, b.raw)
)
}
return compareByteOrder(a.raw, b.raw)
})
}
/**
* Sorts the given string alternatives.
*
* Sorting is done by comparing the lexicographically smallest strings (LSS).
*
* For more information on why we use LSS-based comparison and how it works,
* see https://github.com/ota-meshi/eslint-plugin-regexp/pull/423.
*/
function sortStringAlternatives(
alternatives: StringAlternative[],
parser: JS.Parser,
flags: ReadonlyFlags,
): void {
alternatives.sort((a, b) => {
const lssDiff = compareWords(
getLexicographicallySmallestFromAlternative(a, parser, flags),
getLexicographicallySmallestFromAlternative(b, parser, flags),
)
return lssDiff
})
}
/**
* Returns whether the given string is a valid integer.
* @param str
* @returns
*/
function isIntegerString(str: string): boolean {
return /^(?:0|[1-9]\d*)$/u.test(str)
}
/**
* This tries to sort the given alternatives by assuming that all alternatives
* are a number.
*/
function trySortNumberAlternatives(
alternatives: (Alternative | StringAlternative)[],
): void {
const runs = getRuns(alternatives, (a) => isIntegerString(a.raw))
for (const { startIndex, elements } of runs) {
elements.sort((a, b) => {
return Number(a.raw) - Number(b.raw)
})
alternatives.splice(startIndex, elements.length, ...elements)
}
}
/**
* Returns the indexes of the first and last of original array that is changed
* when compared with the reordered one.
*/
function getReorderingBounds<T>(
original: readonly T[],
reorder: readonly T[],
): [number, number] | undefined {
if (original.length !== reorder.length) {
return undefined
}
const len = original.length
let first = 0
for (; first < len && original[first] === reorder[first]; first++);
if (first === len) {
return undefined
}
let last = len - 1
for (; last >= 0 && original[last] === reorder[last]; last--);
return [first, last]
}
interface Run<T> {
startIndex: number
elements: T[]
}
/**
* Returns an array of runs of elements that fulfill the given condition.
*/
function getRuns<T>(iter: Iterable<T>, condFn: (item: T) => boolean): Run<T>[] {
const runs: Run<T>[] = []
let elements: T[] = []
let index = 0
for (const item of iter) {
if (condFn(item)) {
elements.push(item)
} else {
if (elements.length > 0) {
runs.push({ startIndex: index - elements.length, elements })
elements = []
}
}
index++
}
if (elements.length > 0) {
runs.push({ startIndex: index - elements.length, elements })
elements = []
}
return runs
}
export default createRule("sort-alternatives", {
meta: {
docs: {
description: "sort alternatives if order doesn't matter",
category: "Best Practices",
recommended: false,
},
fixable: "code",
schema: [],
messages: {
sort: "The {{alternatives}} can be sorted without affecting the regex.",
},
type: "suggestion", // "problem",
},
create(context) {
const sliceMinLength = 3
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation, fixReplaceNode, flags } =
regexpContext
const allowedChars = getAllowedChars(flags)
const possibleCharsCache = new Map<Alternative, CharSet>()
const parser = getParser(regexpContext)
/** A cached version of getConsumedChars */
function getPossibleChars(a: Alternative): CharSet {
let chars = possibleCharsCache.get(a)
if (chars === undefined) {
chars = getConsumedChars(a, flags).chars
}
return chars
}
/** Tries to sort the given alternatives. */
function trySortRun(run: Run<Alternative>): void {
const alternatives = run.elements
if (canReorder(alternatives, flags)) {
// alternatives can be reordered freely
sortAlternatives(alternatives, parser, flags)
trySortNumberAlternatives(alternatives)
} else {
const consumedChars = Chars.empty(flags).union(
...alternatives.map(getPossibleChars),
)
if (!consumedChars.isDisjointWith(Chars.digit(flags))) {
// let's try to at least sort numbers
const runs = getRuns(alternatives, (a) =>
isIntegerString(a.raw),
)
for (const { startIndex: index, elements } of runs) {
if (
elements.length > 1 &&
canReorder(elements, flags)
) {
trySortNumberAlternatives(elements)
alternatives.splice(
index,
elements.length,
...elements,
)
}
}
}
}
enforceSorted(run, "alternatives of this group")
}
/**
* Creates a report if the sorted alternatives are different from
* the unsorted ones.
*/
function enforceSorted(
run: Run<Alternative | StringAlternative>,
alternatives:
| "alternatives of this group"
| "string alternatives",
): void {
const sorted = run.elements
const parent = sorted[0].parent
const unsorted = parent.alternatives.slice(
run.startIndex,
run.startIndex + sorted.length,
)
const bounds = getReorderingBounds(unsorted, sorted)
if (!bounds) {
return
}
const loc = getRegexpLocation({
start: unsorted[bounds[0]].start,
end: unsorted[bounds[1]].end,
})
context.report({
node,
loc,
messageId: "sort",
data: { alternatives },
fix: fixReplaceNode(parent, () => {
const prefix = parent.raw.slice(
0,
unsorted[0].start - parent.start,
)
const suffix = parent.raw.slice(
unsorted[unsorted.length - 1].end - parent.start,
)
return (
prefix + sorted.map((a) => a.raw).join("|") + suffix
)
}),
})
}
function onParent(parent: Alternative["parent"]): void {
if (parent.alternatives.length < 2) {
return
}
const runs = getRuns(parent.alternatives, (a) => {
if (!containsOnlyLiterals(a)) {
return false
}
const consumedChars = getPossibleChars(a)
if (consumedChars.isEmpty) {
// the alternative is either empty or only contains
// assertions
return false
}
if (!consumedChars.isSubsetOf(allowedChars.allowed)) {
// contains some chars that are not allowed
return false
}
if (consumedChars.isDisjointWith(allowedChars.required)) {
// doesn't contain required chars
return false
}
return true
})
if (
runs.length === 1 &&
runs[0].elements.length === parent.alternatives.length
) {
// All alternatives are to be sorted
trySortRun(runs[0])
} else {
// Some slices are to be sorted
for (const run of runs) {
if (
run.elements.length >= sliceMinLength &&
run.elements.length >= 2
) {
trySortRun(run)
}
}
}
}
/** The handler for ClassStringDisjunction */
function onClassStringDisjunction(
parent: ClassStringDisjunction,
): void {
if (parent.alternatives.length < 2) {
return
}
const alternatives = [...parent.alternatives]
sortStringAlternatives(alternatives, parser, flags)
trySortNumberAlternatives(alternatives)
const run: Run<StringAlternative> = {
startIndex: 0,
elements: [...alternatives],
}
enforceSorted(run, "string alternatives")
}
return {
onGroupEnter: onParent,
onPatternEnter: onParent,
onCapturingGroupEnter: onParent,
onClassStringDisjunctionEnter: onClassStringDisjunction,
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})