-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-set-operation.ts
146 lines (133 loc) · 4.56 KB
/
prefer-set-operation.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import type {
Alternative,
Character,
CharacterClass,
CharacterSet,
ExpressionCharacterClass,
LookaroundAssertion,
Node,
} from "@eslint-community/regexpp/ast"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { hasStrings } from "regexp-ast-analysis"
type CharElement =
| Character
| CharacterSet
| CharacterClass
| ExpressionCharacterClass
function isCharElement(node: Node): node is CharElement {
return (
node.type === "Character" ||
node.type === "CharacterSet" ||
node.type === "CharacterClass" ||
node.type === "ExpressionCharacterClass"
)
}
type CharLookaround = LookaroundAssertion & {
alternatives: [Alternative & { elements: [CharElement] }]
}
function isCharLookaround(node: Node): node is CharLookaround {
return (
node.type === "Assertion" &&
(node.kind === "lookahead" || node.kind === "lookbehind") &&
node.alternatives.length === 1 &&
node.alternatives[0].elements.length === 1 &&
isCharElement(node.alternatives[0].elements[0])
)
}
function escapeRaw(raw: string): string {
if (/^[&\-^]$/u.test(raw)) {
return `\\${raw}`
}
return raw
}
export default createRule("prefer-set-operation", {
meta: {
docs: {
description:
"prefer character class set operations instead of lookarounds",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"This lookaround can be combined with '{{char}}' using a set operation.",
},
type: "suggestion",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, flags, getRegexpLocation, fixReplaceNode } =
regexpContext
if (!flags.unicodeSets) {
// set operations are exclusive to the v flag.
return {}
}
function tryApply(
element: CharElement,
assertion: CharLookaround,
parent: Alternative,
): void {
const assertElement = assertion.alternatives[0].elements[0]
if (hasStrings(assertElement, flags)) {
return
}
context.report({
node,
loc: getRegexpLocation(assertion),
messageId: "unexpected",
data: {
char: element.raw,
},
fix: fixReplaceNode(parent, () => {
const op = assertion.negate ? "--" : "&&"
const left = escapeRaw(element.raw)
const right = escapeRaw(assertElement.raw)
const replacement = `[${left}${op}${right}]`
return parent.elements
.map((e) => {
if (e === assertion) {
return ""
} else if (e === element) {
return replacement
}
return e.raw
})
.join("")
}),
})
}
return {
onAlternativeEnter(alternative) {
const { elements } = alternative
for (let i = 1; i < elements.length; i++) {
const a = elements[i - 1]
const b = elements[i]
if (
isCharElement(a) &&
isCharLookaround(b) &&
b.kind === "lookbehind"
) {
tryApply(a, b, alternative)
}
if (
isCharLookaround(a) &&
a.kind === "lookahead" &&
isCharElement(b)
) {
tryApply(b, a, alternative)
}
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})