-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-empty-character-class.ts
54 lines (51 loc) · 1.89 KB
/
no-empty-character-class.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { matchesNoCharacters } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
export default createRule("no-empty-character-class", {
meta: {
docs: {
description: "disallow character classes that match no characters",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
empty: "This character class matches no characters because it is empty.",
cannotMatchAny: "This character class cannot match any characters.",
},
type: "suggestion", // "problem",
},
create(context) {
function createVisitor(
regexpContext: RegExpContext,
): RegExpVisitor.Handlers {
const { node, getRegexpLocation, flags } = regexpContext
return {
onCharacterClassEnter(ccNode) {
if (matchesNoCharacters(ccNode, flags)) {
context.report({
node,
loc: getRegexpLocation(ccNode),
messageId: ccNode.elements.length
? "cannotMatchAny"
: "empty",
})
}
},
onExpressionCharacterClassEnter(ccNode) {
if (matchesNoCharacters(ccNode, flags)) {
context.report({
node,
loc: getRegexpLocation(ccNode),
messageId: "cannotMatchAny",
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})