-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-useless-string-literal.ts
129 lines (124 loc) · 5.22 KB
/
no-useless-string-literal.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
import type {
ClassStringDisjunction,
StringAlternative,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { RESERVED_DOUBLE_PUNCTUATOR_CHARS } from "../utils/regex-syntax"
export default createRule("no-useless-string-literal", {
meta: {
docs: {
description:
"disallow string disjunction of single characters in `\\q{...}`",
category: "Best Practices",
recommended: true,
},
schema: [],
messages: {
unexpected: "Unexpected string disjunction of single character.",
},
type: "suggestion",
fixable: "code",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation, fixReplaceNode, pattern } =
regexpContext
return {
onStringAlternativeEnter(saNode) {
if (saNode.elements.length === 1) {
const csdNode = saNode.parent
context.report({
node,
loc: getRegexpLocation(saNode),
messageId: "unexpected",
fix: fixReplaceNode(csdNode, () => {
const alternativesText = csdNode.alternatives
.filter((alt) => alt !== saNode)
.map((alt) => alt.raw)
.join("|")
if (!alternativesText.length) {
const escape =
isNeedEscapeForAdjacentPreviousCharacter(
csdNode,
saNode,
) ||
isNeedEscapeForAdjacentNextCharacter(
csdNode,
saNode,
)
? "\\"
: ""
return `${escape}${saNode.raw}`
}
if (
csdNode.parent.type ===
"ClassIntersection" ||
csdNode.parent.type === "ClassSubtraction"
) {
const escape =
saNode.raw === "^" ? "\\" : ""
return String.raw`[${escape}${saNode.raw}\q{${alternativesText}}]`
}
const escape =
isNeedEscapeForAdjacentPreviousCharacter(
csdNode,
saNode,
)
? "\\"
: ""
return String.raw`${escape}${saNode.raw}\q{${alternativesText}}`
}),
})
}
},
}
/**
* Checks whether the given character requires escaping
* when adjacent to the previous character.
*/
function isNeedEscapeForAdjacentPreviousCharacter(
disjunction: ClassStringDisjunction,
character: StringAlternative,
) {
const char = character.raw
// Avoid [A&&\q{&}] => [A&&&]
if (
RESERVED_DOUBLE_PUNCTUATOR_CHARS.has(char) &&
// The previous character is the same
pattern[disjunction.start - 1] === char
) {
return true
}
// Avoid [\q{^|ab}] => [^\q{ab}]
return (
char === "^" &&
disjunction.parent.type === "CharacterClass" &&
disjunction.parent.start === disjunction.start - 1
)
}
/**
* Checks whether the given character requires escaping
* when adjacent to the next character.
*/
function isNeedEscapeForAdjacentNextCharacter(
disjunction: ClassStringDisjunction,
character: StringAlternative,
) {
const char = character.raw
// Avoid [\q{&}&&A] => [&&&A]
return (
RESERVED_DOUBLE_PUNCTUATOR_CHARS.has(char) &&
// The next character is the same
pattern[disjunction.end] === char
)
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})