-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnegation.ts
139 lines (127 loc) · 4.67 KB
/
negation.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
import type {
CharacterClass,
CharacterClassElement,
CharacterUnicodePropertyCharacterSet,
EscapeCharacterSet,
ExpressionCharacterClass,
} from "@eslint-community/regexpp/ast"
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { toUnicodeSet } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { assertNever } from "../utils/util"
type NegatableCharacterClassElement =
| CharacterClass
| ExpressionCharacterClass
| EscapeCharacterSet
| CharacterUnicodePropertyCharacterSet
/** Checks whether the given character class is negatable. */
function isNegatableCharacterClassElement<N extends CharacterClassElement>(
node: N,
): node is N & NegatableCharacterClassElement {
return (
node.type === "CharacterClass" ||
node.type === "ExpressionCharacterClass" ||
(node.type === "CharacterSet" &&
(node.kind !== "property" || !node.strings))
)
}
export default createRule("negation", {
meta: {
docs: {
description: "enforce use of escapes on negation",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected:
"Unexpected negated character class. Use '{{negatedCharSet}}' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
function createVisitor({
node,
getRegexpLocation,
fixReplaceNode,
flags,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onCharacterClassEnter(ccNode) {
if (!ccNode.negate || ccNode.elements.length !== 1) {
return
}
const element = ccNode.elements[0]
if (!isNegatableCharacterClassElement(element)) {
return
}
if (element.type !== "CharacterSet" && !element.negate) {
return
}
if (
flags.ignoreCase &&
!flags.unicodeSets &&
element.type === "CharacterSet" &&
element.kind === "property"
) {
// The ignore case canonicalization affects negated
// Unicode property escapes in a weird way. In short,
// /\p{Foo}/i is not the same as /[^\P{Foo}]/i if
// \p{Foo} contains case-varying characters.
//
// Note: This only affects Unicode property escapes.
// All other character sets are either case-invariant
// (/./, /\s/, /\d/) or inconsistent (/\w/).
const ccSet = toUnicodeSet(ccNode, flags)
const negatedElementSet = toUnicodeSet(
{
...element,
negate: !element.negate,
},
flags,
)
if (!ccSet.equals(negatedElementSet)) {
// We cannot remove the negative
return
}
}
const negatedCharSet = getNegationText(element)
context.report({
node,
loc: getRegexpLocation(ccNode),
messageId: "unexpected",
data: { negatedCharSet },
fix: fixReplaceNode(ccNode, negatedCharSet),
})
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})
/**
* Gets the text that negation the CharacterSet.
*/
function getNegationText(node: NegatableCharacterClassElement) {
if (node.type === "CharacterSet") {
// they are all of the form: /\\[dswp](?:\{[^{}]+\})?/
let kind = node.raw[1]
if (kind.toLowerCase() === kind) {
kind = kind.toUpperCase()
} else {
kind = kind.toLowerCase()
}
return `\\${kind}${node.raw.slice(2)}`
}
if (node.type === "CharacterClass") {
return `[${node.elements.map((e) => e.raw).join("")}]`
}
if (node.type === "ExpressionCharacterClass") {
return `[${node.raw.slice(2, -1)}]`
}
return assertNever(node)
}