Skip to content

Commit 6b8fb52

Browse files
Use mention if necessary (#295)
1 parent 7b40367 commit 6b8fb52

14 files changed

+87
-70
lines changed

lib/rules/match-any.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { RegExpContext } from "../utils"
55
import { createRule, defineRegexpVisitor } from "../utils"
66
import { isRegexpLiteral } from "../utils/ast-utils/utils"
77
import { matchesAllCharacters } from "regexp-ast-analysis"
8+
import { mention } from "../utils/mention"
89

910
const OPTION_SS1 = "[\\s\\S]" as const
1011
const OPTION_SS2 = "[\\S\\s]" as const
@@ -47,7 +48,7 @@ export default createRule("match-any", {
4748
},
4849
],
4950
messages: {
50-
unexpected: "Unexpected using '{{expr}}' to match any character.",
51+
unexpected: "Unexpected using {{expr}} to match any character.",
5152
},
5253
type: "suggestion", // "problem",
5354
},
@@ -145,7 +146,7 @@ export default createRule("match-any", {
145146
loc: getRegexpLocation(csNode),
146147
messageId: "unexpected",
147148
data: {
148-
expr: ".",
149+
expr: mention(csNode),
149150
},
150151
fix(fixer) {
151152
return fix(fixer, regexpContext, csNode)
@@ -163,7 +164,7 @@ export default createRule("match-any", {
163164
loc: getRegexpLocation(ccNode),
164165
messageId: "unexpected",
165166
data: {
166-
expr: ccNode.raw,
167+
expr: mention(ccNode),
167168
},
168169
fix(fixer) {
169170
return fix(fixer, regexpContext, ccNode)

lib/rules/no-dupe-disjunctions.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import { RegExpParser } from "regexpp"
3131
import { UsageOfPattern } from "../utils/get-usage-of-pattern"
3232
import { canReorder } from "../utils/reorder-alternatives"
33+
import { mention } from "../utils/mention"
3334

3435
type ParentNode = Group | CapturingGroup | Pattern | LookaroundAssertion
3536

@@ -714,13 +715,13 @@ export default createRule("no-dupe-disjunctions", {
714715
duplicate:
715716
"Unexpected duplicate alternative. This alternative can be removed.{{cap}}{{exp}}",
716717
subset:
717-
"Unexpected useless alternative. This alternative is a strict subset of '{{others}}' and can be removed.{{cap}}{{exp}}",
718+
"Unexpected useless alternative. This alternative is a strict subset of {{others}} and can be removed.{{cap}}{{exp}}",
718719
prefixSubset:
719-
"Unexpected useless alternative. This alternative is already covered by '{{others}}' and can be removed.{{cap}}",
720+
"Unexpected useless alternative. This alternative is already covered by {{others}} and can be removed.{{cap}}",
720721
superset:
721-
"Unexpected superset. This alternative is a superset of '{{others}}'. It might be possible to remove the other alternative(s).{{cap}}{{exp}}",
722+
"Unexpected superset. This alternative is a superset of {{others}}. It might be possible to remove the other alternative(s).{{cap}}{{exp}}",
722723
overlap:
723-
"Unexpected overlap. This alternative overlaps with '{{others}}'. The overlap is '{{expr}}'.{{cap}}{{exp}}",
724+
"Unexpected overlap. This alternative overlaps with {{others}}. The overlap is {{expr}}.{{cap}}{{exp}}",
724725
},
725726
type: "suggestion", // "problem",
726727
},
@@ -936,7 +937,9 @@ export default createRule("no-dupe-disjunctions", {
936937
? " Careful! This alternative contains capturing groups which might be difficult to remove."
937938
: ""
938939

939-
const others = result.others.map((a) => a.raw).join("|")
940+
const others = mention(
941+
result.others.map((a) => a.raw).join("|"),
942+
)
940943

941944
switch (result.type) {
942945
case "Duplicate":
@@ -984,7 +987,9 @@ export default createRule("no-dupe-disjunctions", {
984987
exp,
985988
cap,
986989
others,
987-
expr: faToSource(result.overlap, flags),
990+
expr: mention(
991+
faToSource(result.overlap, flags),
992+
),
988993
},
989994
})
990995
break

lib/rules/no-super-linear-backtracking.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { UsageOfPattern } from "../utils/get-usage-of-pattern"
55
import type { ParsedLiteral } from "scslre"
66
import { analyse } from "scslre"
77
import type { Position, SourceLocation } from "estree"
8+
import { mention } from "../utils/mention"
89

910
/**
1011
* Returns the combined source location of the two given locations.
@@ -74,11 +75,11 @@ export default createRule("no-super-linear-backtracking", {
7475
],
7576
messages: {
7677
self:
77-
"This quantifier can reach itself via the loop '{{parent}}'." +
78+
"This quantifier can reach itself via the loop {{parent}}." +
7879
" Using any string accepted by {{attack}}, this can be exploited to cause at least polynomial backtracking." +
7980
"{{exp}}",
8081
trade:
81-
"The quantifier '{{start}}' can exchange characters with '{{end}}'." +
82+
"The quantifier {{start}} can exchange characters with {{end}}." +
8283
" Using any string accepted by {{attack}}, this can be exploited to cause at least polynomial backtracking." +
8384
"{{exp}}",
8485
},
@@ -134,7 +135,7 @@ export default createRule("no-super-linear-backtracking", {
134135
data: {
135136
exp,
136137
attack,
137-
parent: report.parentQuant.raw,
138+
parent: mention(report.parentQuant),
138139
},
139140
fix,
140141
})
@@ -149,8 +150,8 @@ export default createRule("no-super-linear-backtracking", {
149150
data: {
150151
exp,
151152
attack,
152-
start: report.startQuant.raw,
153-
end: report.endQuant.raw,
153+
start: mention(report.startQuant),
154+
end: mention(report.endQuant),
154155
},
155156
fix,
156157
})

lib/rules/no-useless-assertions.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ import {
1616
hasSomeDescendant,
1717
isPotentiallyEmpty,
1818
} from "regexp-ast-analysis"
19+
import { mention } from "../utils/mention"
1920

2021
const messages = {
2122
alwaysRejectByChar:
22-
"'{{assertion}}' will always reject because it is {{followedOrPreceded}} by a character.",
23+
"{{assertion}} will always reject because it is {{followedOrPreceded}} by a character.",
2324
alwaysRejectByNonLineTerminator:
24-
"'{{assertion}}' will always reject because it is {{followedOrPreceded}} by a non-line-terminator character.",
25+
"{{assertion}} will always reject because it is {{followedOrPreceded}} by a non-line-terminator character.",
2526
alwaysAcceptByLineTerminator:
26-
"'{{assertion}}' will always accept because it is {{followedOrPreceded}} by a line-terminator character.",
27+
"{{assertion}} will always accept because it is {{followedOrPreceded}} by a line-terminator character.",
2728
alwaysAcceptOrRejectFollowedByWord:
28-
"'{{assertion}}' will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a word character.",
29+
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a word character.",
2930
alwaysAcceptOrRejectFollowedByNonWord:
30-
"'{{assertion}}' will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a non-word character.",
31+
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a non-word character and followed by a non-word character.",
3132
alwaysAcceptOrRejectPrecededByWordFollowedByNonWord:
32-
"'{{assertion}}' will always {{acceptOrReject}} because it is preceded by a word character and followed by a non-word character.",
33+
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a word character and followed by a non-word character.",
3334
alwaysAcceptOrRejectPrecededByWordFollowedByWord:
34-
"'{{assertion}}' will always {{acceptOrReject}} because it is preceded by a word character and followed by a word character.",
35+
"{{assertion}} will always {{acceptOrReject}} because it is preceded by a word character and followed by a word character.",
3536
alwaysForLookaround:
36-
"The {{kind}} '{{assertion}}' will always {{acceptOrReject}}.",
37+
"The {{kind}} {{assertion}} will always {{acceptOrReject}}.",
3738
alwaysForNegativeLookaround:
38-
"The negative {{kind}} '{{assertion}}' will always {{acceptOrReject}}.",
39+
"The negative {{kind}} {{assertion}} will always {{acceptOrReject}}.",
3940
}
4041

4142
export default createRule("no-useless-assertions", {
@@ -72,7 +73,7 @@ export default createRule("no-useless-assertions", {
7273
loc: getRegexpLocation(assertion),
7374
messageId,
7475
data: {
75-
assertion: assertion.raw,
76+
assertion: mention(assertion),
7677
...data,
7778
},
7879
})

lib/rules/no-useless-backreference.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getMatchingDirection,
1313
isZeroLength,
1414
} from "regexp-ast-analysis"
15+
import { mention } from "../utils/mention"
1516

1617
/**
1718
* Returns whether the list of ancestors from `from` to `to` contains a negated
@@ -89,17 +90,17 @@ export default createRule("no-useless-backreference", {
8990
schema: [],
9091
messages: {
9192
nested:
92-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' from within that group.",
93+
"Backreference {{ bref }} will be ignored. It references group {{ group }} from within that group.",
9394
forward:
94-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears later in the pattern.",
95+
"Backreference {{ bref }} will be ignored. It references group {{ group }} which appears later in the pattern.",
9596
backward:
96-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears before in the same lookbehind.",
97+
"Backreference {{ bref }} will be ignored. It references group {{ group }} which appears before in the same lookbehind.",
9798
disjunctive:
98-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in another alternative.",
99+
"Backreference {{ bref }} will be ignored. It references group {{ group }} which is in another alternative.",
99100
intoNegativeLookaround:
100-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in a negative lookaround.",
101+
"Backreference {{ bref }} will be ignored. It references group {{ group }} which is in a negative lookaround.",
101102
empty:
102-
"Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which always captures zero characters.",
103+
"Backreference {{ bref }} will be ignored. It references group {{ group }} which always captures zero characters.",
103104
},
104105
type: "suggestion", // "problem",
105106
},
@@ -121,8 +122,8 @@ export default createRule("no-useless-backreference", {
121122
loc: getRegexpLocation(backRef),
122123
messageId,
123124
data: {
124-
bref: backRef.raw,
125-
group: backRef.resolved.raw,
125+
bref: mention(backRef),
126+
group: mention(backRef.resolved),
126127
},
127128
})
128129
}

lib/rules/optimal-lookaround-quantifier.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Alternative, LookaroundAssertion, Quantifier } from "regexpp/ast"
33
import type { RegExpContext } from "../utils"
44
import { createRule, defineRegexpVisitor } from "../utils"
55
import { hasSomeDescendant } from "regexp-ast-analysis"
6+
import { mention } from "../utils/mention"
67

78
/**
89
* Extract invalid quantifiers for lookarounds
@@ -65,9 +66,9 @@ export default createRule("optimal-lookaround-quantifier", {
6566
schema: [],
6667
messages: {
6768
remove:
68-
"The quantified expression '{{expr}}' at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
69+
"The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.",
6970
replacedWith:
70-
"The quantified expression '{{expr}}' at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be replaced with {{replacer}} without affecting the lookaround.",
71+
"The quantified expression {{expr}} at the {{endOrStart}} of the expression tree should only be matched a constant number of times. The expression can be replaced with {{replacer}} without affecting the lookaround.",
7172
},
7273
type: "problem",
7374
},
@@ -105,7 +106,7 @@ export default createRule("optimal-lookaround-quantifier", {
105106
messageId:
106107
q.min === 0 ? "remove" : "replacedWith",
107108
data: {
108-
expr: q.raw,
109+
expr: mention(q),
109110
endOrStart,
110111
replacer,
111112
},

lib/rules/optimal-quantifier-concatenation.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { createRule, defineRegexpVisitor, quantToString } from "../utils"
1919
import { Chars, hasSomeDescendant } from "regexp-ast-analysis"
2020
import { getPossiblyConsumedChar } from "../utils/regexp-ast"
2121
import type { CharSet } from "refa"
22+
import { mention } from "../utils/mention"
2223

2324
/**
2425
* Returns whether the given node is or contains a capturing group.
@@ -511,17 +512,17 @@ export default createRule("optimal-quantifier-concatenation", {
511512
schema: [],
512513
messages: {
513514
combine:
514-
"'{{left}}' and '{{right}}' can be combined into one quantifier '{{fix}}'.{{cap}}",
515+
"{{left}} and {{right}} can be combined into one quantifier {{fix}}.{{cap}}",
515516
removeLeft:
516-
"'{{left}}' can be removed because it is already included by '{{right}}'.{{cap}}",
517+
"{{left}} can be removed because it is already included by {{right}}.{{cap}}",
517518
removeRight:
518-
"'{{right}}' can be removed because it is already included by '{{left}}'.{{cap}}",
519+
"{{right}} can be removed because it is already included by {{left}}.{{cap}}",
519520
replace:
520-
"'{{left}}' and '{{right}}' can be replaced with '{{fix}}'.{{cap}}",
521+
"{{left}} and {{right}} can be replaced with {{fix}}.{{cap}}",
521522
nestedRemove:
522-
"'{{nested}}' can be removed because of '{{dominate}}'.{{cap}}",
523+
"{{nested}} can be removed because of {{dominate}}.{{cap}}",
523524
nestedReplace:
524-
"'{{nested}}' can be replaced with '{{fix}}' because of '{{dominate}}'.{{cap}}",
525+
"{{nested}} can be replaced with {{fix}} because of {{dominate}}.{{cap}}",
525526
},
526527
type: "suggestion",
527528
},
@@ -562,9 +563,9 @@ export default createRule("optimal-quantifier-concatenation", {
562563
loc: getLoc(left, right, regexpContext),
563564
messageId: replacement.messageId,
564565
data: {
565-
left: left.raw,
566-
right: right.raw,
567-
fix: replacement.raw,
566+
left: mention(left),
567+
right: mention(right),
568+
fix: mention(replacement.raw),
568569
cap,
569570
},
570571
fix: fixReplaceNode(aNode, () => {
@@ -589,9 +590,9 @@ export default createRule("optimal-quantifier-concatenation", {
589590
loc: getRegexpLocation(replacement.nested),
590591
messageId: replacement.messageId,
591592
data: {
592-
nested: replacement.nested.raw,
593-
dominate: replacement.dominate.raw,
594-
fix: replacement.raw,
593+
nested: mention(replacement.nested),
594+
dominate: mention(replacement.dominate),
595+
fix: mention(replacement.raw),
595596
cap,
596597
},
597598
fix: fixReplaceNode(replacement.nested, () => {

lib/rules/prefer-d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CP_DIGIT_NINE,
88
} from "../utils"
99
import { Chars } from "regexp-ast-analysis"
10+
import { mention } from "../utils/mention"
1011

1112
export default createRule("prefer-d", {
1213
meta: {
@@ -19,7 +20,7 @@ export default createRule("prefer-d", {
1920
schema: [],
2021
messages: {
2122
unexpected:
22-
"Unexpected {{type}} '{{expr}}'. Use '{{instead}}' instead.",
23+
"Unexpected {{type}} {{expr}}. Use '{{instead}}' instead.",
2324
},
2425
type: "suggestion", // "problem",
2526
},
@@ -56,7 +57,7 @@ export default createRule("prefer-d", {
5657
messageId: "unexpected",
5758
data: {
5859
type: "character class",
59-
expr: ccNode.raw,
60+
expr: mention(ccNode),
6061
instead: predefined,
6162
},
6263
fix: fixReplaceNode(ccNode, predefined),
@@ -83,7 +84,7 @@ export default createRule("prefer-d", {
8384
messageId: "unexpected",
8485
data: {
8586
type: "character class range",
86-
expr: ccrNode.raw,
87+
expr: mention(ccrNode),
8788
instead,
8889
},
8990
fix: fixReplaceNode(ccrNode, instead),

lib/rules/prefer-question-quantifier.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mention } from "../utils/mention"
12
import type { Group, Quantifier } from "regexpp/ast"
23
import type { RegExpVisitor } from "regexpp/visitor"
34
import type { RegExpContext } from "../utils"
@@ -15,7 +16,7 @@ export default createRule("prefer-question-quantifier", {
1516
messages: {
1617
unexpected: "Unexpected quantifier '{{expr}}'. Use '?' instead.",
1718
unexpectedGroup:
18-
"Unexpected group '{{expr}}'. Use '{{instead}}' instead.",
19+
"Unexpected group {{expr}}. Use '{{instead}}' instead.",
1920
},
2021
type: "suggestion", // "problem",
2122
},
@@ -96,7 +97,7 @@ export default createRule("prefer-question-quantifier", {
9697
loc: getRegexpLocation(reportNode),
9798
messageId: "unexpectedGroup",
9899
data: {
99-
expr: reportNode.raw,
100+
expr: mention(reportNode),
100101
instead,
101102
},
102103
fix: fixReplaceNode(reportNode, instead),

0 commit comments

Comments
 (0)