-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-useless-assertions.ts
665 lines (598 loc) · 24.5 KB
/
no-useless-assertions.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
import type {
Alternative,
Assertion,
EdgeAssertion,
Element,
LookaroundAssertion,
Node,
Pattern,
WordBoundaryAssertion,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type {
MatchingDirection,
ReadonlyFlags,
FirstLookChar,
} from "regexp-ast-analysis"
import {
Chars,
getFirstCharAfter,
getFirstConsumedChar,
getLengthRange,
getMatchingDirectionFromAssertionKind,
hasSomeDescendant,
isPotentiallyEmpty,
isZeroLength,
FirstConsumedChars,
invertMatchingDirection,
} from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { mention } from "../utils/mention"
import { assertNever } from "../utils/util"
function containsAssertion(n: Node): boolean {
return hasSomeDescendant(n, (d) => d.type === "Assertion")
}
/**
* Returns whether the given lookaround asserts exactly one character in the given direction.
*/
function isSingleCharacterAssertion(
assertion: Assertion,
direction: MatchingDirection,
flags: ReadonlyFlags,
): boolean {
switch (assertion.kind) {
case "word":
// \b and \B assert one character in BOTH directions
return false
case "start":
return direction === "rtl"
case "end":
return direction === "ltr"
default:
break
}
if (getMatchingDirectionFromAssertionKind(assertion.kind) !== direction) {
return false
}
return assertion.alternatives.every((alt) => {
if (!containsAssertion(alt)) {
// if we don't contains assertions, then we can only need to check
// the length range of the consumed characters
const range = getLengthRange(alt, flags)
return range.min === 1 && range.max === 1
}
// now it gets tricky
// e.g. (?=(?=[a-z])\w(?<=[a-g])) is a single character assertion
let consumed = false
let asserted = false
const elements =
direction === "ltr" ? alt.elements : [...alt.elements].reverse()
for (const e of elements) {
if (!consumed) {
// before we consumed our first (and only) character, we can
// have single character assertions
if (
e.type === "Assertion" &&
isSingleCharacterAssertion(e, direction, flags)
) {
asserted = true
continue
}
if (containsAssertion(e)) {
// too complex to reason about, so we just give up
return false
}
const range = getLengthRange(e, flags)
if (range.max === 0) {
// we haven't consumed anything, so onto the next one
continue
} else if (range.min === 1 && range.max === 1) {
// finally, a character
consumed = true
} else {
// it's not exactly a single character
return false
}
} else {
// since we already consumed our first (and only) character, we
// can at most have assertions in the direction of the
// first character
const otherDir = invertMatchingDirection(direction)
if (
e.type === "Assertion" &&
isSingleCharacterAssertion(e, otherDir, flags)
) {
continue
}
return false
}
}
return consumed || asserted
})
}
/**
* Combines 2 look chars such that the result is equivalent to 2 adjacent
* assertions `(?=a)(?=b)`.
*/
function firstLookCharsIntersection(
a: FirstLookChar,
b: FirstLookChar,
): FirstLookChar {
const char = a.char.intersect(b.char)
return {
char: a.char.intersect(b.char),
exact: (a.exact && b.exact) || char.isEmpty,
edge: a.edge && b.edge,
}
}
type GetFirstCharAfter = (
afterThis: Assertion,
direction: MatchingDirection,
flags: ReadonlyFlags,
) => FirstLookChar
/**
* Creates a {@link GetFirstCharAfter} function that will reorder assertions to
* get the maximum information after the characters after the given assertions.
*
* Conceptually, this will reorder adjacent assertions such that given
* assertion is moved as far as possible in the opposite direction of natural
* matching direction. E.g. when given `$` in `a(?!a)(?<=\w)$`, the characters
* after `$` will be returned as if the pattern was `a$(?!a)(?<=\w)`.
*
* @param forbidden A list of assertions that may not be reordered.
*/
function createReorderingGetFirstCharAfter(
forbidden: ReadonlySet<Assertion>,
): GetFirstCharAfter {
/** Whether the given element or one of its descendants is forbidden. */
function hasForbidden(element: Element): boolean {
if (element.type === "Assertion" && forbidden.has(element)) {
return true
}
for (const f of forbidden) {
if (hasSomeDescendant(element, f)) {
return true
}
}
return false
}
return (afterThis, direction, flags) => {
let result = getFirstCharAfter(afterThis, direction, flags)
if (afterThis.parent.type === "Alternative") {
const { elements } = afterThis.parent
const inc = direction === "ltr" ? -1 : +1
const start = elements.indexOf(afterThis)
for (let i = start + inc; i >= 0 && i < elements.length; i += inc) {
const other = elements[i]
if (!isZeroLength(other, flags)) {
break
}
if (hasForbidden(other)) {
// we hit an element that cannot be reordered
break
}
const otherResult = FirstConsumedChars.toLook(
getFirstConsumedChar(other, direction, flags),
)
result = firstLookCharsIntersection(result, otherResult)
}
}
return result
}
}
function removeAlternative(
alternative: Alternative,
): [Element | Pattern, string] {
const parent = alternative.parent
if (parent.alternatives.length > 1) {
// we can just remove the alternative
let { start, end } = alternative
if (parent.alternatives[0] === alternative) {
end++
} else {
start--
}
const before = parent.raw.slice(0, start - parent.start)
const after = parent.raw.slice(end - parent.start)
return [parent, before + after]
}
// we have to remove the parent as well
switch (parent.type) {
case "Pattern":
return [parent, "[]"]
case "Assertion": {
// the inner part of the assertion always rejects
const assertionParent = parent.parent
if (parent.negate) {
// the assertion always accepts
return [
assertionParent.type === "Quantifier"
? assertionParent
: parent,
"",
]
}
if (assertionParent.type === "Quantifier") {
if (assertionParent.min === 0) {
return [assertionParent, ""]
}
return removeAlternative(assertionParent.parent)
}
return removeAlternative(assertionParent)
}
case "CapturingGroup": {
// we don't remove capturing groups
const before = parent.raw.slice(0, alternative.start - parent.start)
const after = parent.raw.slice(alternative.end - parent.start)
return [parent, `${before}[]${after}`]
}
case "Group": {
const groupParent = parent.parent
if (groupParent.type === "Quantifier") {
if (groupParent.min === 0) {
return [groupParent, ""]
}
return removeAlternative(groupParent.parent)
}
return removeAlternative(groupParent)
}
default:
return assertNever(parent)
}
}
const messages = {
alwaysRejectByChar:
"{{assertion}} will always reject because it is {{followedOrPreceded}} by a character.",
alwaysAcceptByChar:
"{{assertion}} will always accept because it is never {{followedOrPreceded}} by a character.",
alwaysRejectByNonLineTerminator:
"{{assertion}} will always reject because it is {{followedOrPreceded}} by a non-line-terminator character.",
alwaysAcceptByLineTerminator:
"{{assertion}} will always accept because it is {{followedOrPreceded}} by a line-terminator character.",
alwaysAcceptByLineTerminatorOrEdge:
"{{assertion}} will always accept because it is {{followedOrPreceded}} by a line-terminator character or the {{startOrEnd}} of the input string.",
alwaysAcceptOrRejectFollowedByWord:
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a word character.",
alwaysAcceptOrRejectFollowedByNonWord:
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a non-word character.",
alwaysAcceptOrRejectPrecededByWordFollowedByNonWord:
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a word character and followed by a non-word character.",
alwaysAcceptOrRejectPrecededByWordFollowedByWord:
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a word character and followed by a word character.",
alwaysForLookaround:
"The {{kind}} {{assertion}} will always {{acceptOrReject}}.",
alwaysForNegativeLookaround:
"The negative {{kind}} {{assertion}} will always {{acceptOrReject}}.",
acceptSuggestion: "Remove the assertion. (Replace with empty string.)",
rejectSuggestion:
"Remove branch of the assertion. (Replace with empty set.)",
}
export default createRule("no-useless-assertions", {
meta: {
docs: {
description:
"disallow assertions that are known to always accept (or reject)",
category: "Possible Errors",
recommended: true,
},
hasSuggestions: true,
schema: [],
messages,
type: "problem",
},
create(context) {
function createVisitor({
node,
flags,
getRegexpLocation,
fixReplaceNode,
}: RegExpContext): RegExpVisitor.Handlers {
const reported = new Set<Assertion>()
function replaceWithEmptyString(assertion: Assertion) {
if (assertion.parent.type === "Quantifier") {
// the assertion always accepts does not consume characters, we can remove the quantifier as well.
return fixReplaceNode(assertion.parent, "")
}
return fixReplaceNode(assertion, "")
}
function replaceWithEmptySet(assertion: Assertion) {
if (assertion.parent.type === "Quantifier") {
if (assertion.parent.min === 0) {
// the assertion always rejects does not consume characters, we can remove the quantifier as well.
return fixReplaceNode(assertion.parent, "")
}
const [element, replacement] = removeAlternative(
assertion.parent.parent,
)
return fixReplaceNode(element, replacement)
}
const [element, replacement] = removeAlternative(
assertion.parent,
)
return fixReplaceNode(element, replacement)
}
function report(
assertion: Assertion,
messageId: keyof typeof messages,
data: Record<string, string> & {
acceptOrReject: "accept" | "reject"
},
) {
reported.add(assertion)
const { acceptOrReject } = data
context.report({
node,
loc: getRegexpLocation(assertion),
messageId,
data: {
assertion: mention(assertion),
...data,
},
suggest: [
{
messageId: `${acceptOrReject}Suggestion`,
fix:
acceptOrReject === "accept"
? replaceWithEmptyString(assertion)
: replaceWithEmptySet(assertion),
},
],
})
}
/**
* Verify for `^` or `$`
*/
function verifyStartOrEnd(
assertion: EdgeAssertion,
getFirstCharAfterFn: GetFirstCharAfter,
): void {
// Note: /^/ is the same as /(?<!.)/s and /^/m is the same as /(?<!.)/
// Note: /$/ is the same as /(?!.)/s and /$/m is the same as /(?!.)/
// get the "next" character
const direction = getMatchingDirectionFromAssertionKind(
assertion.kind,
)
const next = getFirstCharAfterFn(assertion, direction, flags)
const followedOrPreceded =
assertion.kind === "end" ? "followed" : "preceded"
const lineTerminator = Chars.lineTerminator(flags)
if (next.edge) {
// the string might start/end after the assertion
if (!flags.multiline) {
// ^/$ will always accept at an edge with no char before/after it
if (next.char.isEmpty) {
report(assertion, "alwaysAcceptByChar", {
followedOrPreceded,
acceptOrReject: "accept",
})
}
} else {
// ^/$ will always accept at an edge or line terminator before/after it
if (next.char.isSubsetOf(lineTerminator)) {
report(
assertion,
"alwaysAcceptByLineTerminatorOrEdge",
{
followedOrPreceded,
startOrEnd: assertion.kind,
acceptOrReject: "accept",
},
)
}
}
} else {
// there is always some character of `node`
if (!flags.multiline) {
// since the m flag isn't present any character will result in trivial rejection
report(assertion, "alwaysRejectByChar", {
followedOrPreceded,
acceptOrReject: "reject",
})
} else {
// only if the character is a sub set of /./, will the assertion trivially reject
if (next.char.isDisjointWith(lineTerminator)) {
report(
assertion,
"alwaysRejectByNonLineTerminator",
{
followedOrPreceded,
acceptOrReject: "reject",
},
)
} else if (next.char.isSubsetOf(lineTerminator)) {
report(assertion, "alwaysAcceptByLineTerminator", {
followedOrPreceded,
acceptOrReject: "accept",
})
}
}
}
}
/**
* Verify for `\b` or `\B`
*/
function verifyWordBoundary(
assertion: WordBoundaryAssertion,
getFirstCharAfterFn: GetFirstCharAfter,
): void {
const word = Chars.word(flags)
const next = getFirstCharAfterFn(assertion, "ltr", flags)
const prev = getFirstCharAfterFn(assertion, "rtl", flags)
const nextIsWord = next.char.isSubsetOf(word) && !next.edge
const prevIsWord = prev.char.isSubsetOf(word) && !prev.edge
const nextIsNonWord = next.char.isDisjointWith(word)
const prevIsNonWord = prev.char.isDisjointWith(word)
// Note: /\b/ == /(?:(?<!\w)(?=\w)|(?<=\w)(?!\w))/ (other flags may apply)
// the idea here is that \B accepts when \b reject and vise versa.
const accept = assertion.negate ? "reject" : "accept"
const reject = assertion.negate ? "accept" : "reject"
if (prevIsNonWord) {
// current branch: /(?<!\w)(?=\w)/
if (nextIsWord) {
report(
assertion,
"alwaysAcceptOrRejectFollowedByWord",
{
acceptOrReject: accept,
},
)
}
if (nextIsNonWord) {
report(
assertion,
"alwaysAcceptOrRejectFollowedByNonWord",
{
acceptOrReject: reject,
},
)
}
}
if (prevIsWord) {
// current branch: /(?<=\w)(?!\w)/
if (nextIsNonWord) {
report(
assertion,
"alwaysAcceptOrRejectPrecededByWordFollowedByNonWord",
{
acceptOrReject: accept,
},
)
}
if (nextIsWord) {
report(
assertion,
"alwaysAcceptOrRejectPrecededByWordFollowedByWord",
{
acceptOrReject: reject,
},
)
}
}
}
/**
* Verify for LookaroundAssertion
*/
function verifyLookaround(
assertion: LookaroundAssertion,
getFirstCharAfterFn: GetFirstCharAfter,
): void {
if (isPotentiallyEmpty(assertion.alternatives, flags)) {
// we don't handle trivial accept/reject based on emptiness
return
}
const direction = getMatchingDirectionFromAssertionKind(
assertion.kind,
)
const after = getFirstCharAfterFn(assertion, direction, flags)
const firstOf = FirstConsumedChars.toLook(
getFirstConsumedChar(
assertion.alternatives,
direction,
flags,
),
)
// the idea here is that a negate lookaround accepts when non-negated version reject and vise versa.
const accept = assertion.negate ? "reject" : "accept"
const reject = assertion.negate ? "accept" : "reject"
// Careful now! If exact is false, we are only guaranteed to have a superset of the actual character.
// False negatives are fine but we can't have false positives.
if (
after.char.isDisjointWith(firstOf.char) &&
!(after.edge && firstOf.edge)
) {
report(
assertion,
assertion.negate
? "alwaysForNegativeLookaround"
: "alwaysForLookaround",
{
kind: assertion.kind,
acceptOrReject: reject,
},
)
}
// We can only decide the accept case for exact single-character assertions.
// We want the character after the assertion to be a subset of the asserted characters. For this to be
// correct, the set of assertion characters needs to be exact. We also have to consider edges. Edges
// can be thought of as a special character, so the same subset requirement applies.
const edgeSubset = firstOf.edge || !after.edge
if (
firstOf.exact &&
edgeSubset &&
after.char.isSubsetOf(firstOf.char) &&
isSingleCharacterAssertion(
assertion,
getMatchingDirectionFromAssertionKind(assertion.kind),
flags,
)
) {
report(
assertion,
assertion.negate
? "alwaysForNegativeLookaround"
: "alwaysForLookaround",
{
kind: assertion.kind,
acceptOrReject: accept,
},
)
}
}
/**
* Verify for Assertion
*/
function verifyAssertion(
assertion: Assertion,
getFirstCharAfterFn: GetFirstCharAfter,
): void {
switch (assertion.kind) {
case "start":
case "end":
verifyStartOrEnd(assertion, getFirstCharAfterFn)
break
case "word":
verifyWordBoundary(assertion, getFirstCharAfterFn)
break
case "lookahead":
case "lookbehind":
verifyLookaround(assertion, getFirstCharAfterFn)
break
default:
throw assertNever(assertion)
}
}
const allAssertions: Assertion[] = []
return {
onAssertionEnter(assertion) {
// Phase 1:
// The context of assertions is determined by only looking
// at elements after the current assertion. This means that
// the order of assertions is kept as is.
verifyAssertion(assertion, getFirstCharAfter)
// store all assertions for the second phase
allAssertions.push(assertion)
},
onPatternLeave() {
// Phase 2:
// The context of assertions is determined by reordering
// assertions such that as much information as possible can
// be extracted from its surrounding assertions.
const reorderingGetFirstCharAfter =
createReorderingGetFirstCharAfter(reported)
for (const assertion of allAssertions) {
if (!reported.has(assertion)) {
verifyAssertion(
assertion,
reorderingGetFirstCharAfter,
)
}
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})