-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfusing-quantifier.ts
56 lines (54 loc) · 1.95 KB
/
confusing-quantifier.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { isPotentiallyEmpty } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { quantToString, getQuantifierOffsets } from "../utils/regexp-ast"
export default createRule("confusing-quantifier", {
meta: {
docs: {
description: "disallow confusing quantifiers",
category: "Best Practices",
recommended: true,
default: "warn",
},
schema: [],
messages: {
confusing:
"This quantifier is confusing because its minimum is {{min}} but it can match the empty string. Maybe replace it with `{{proposal}}` to reflect that it can match the empty string?",
},
type: "problem",
},
create(context) {
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onQuantifierEnter(qNode) {
if (
qNode.min > 0 &&
isPotentiallyEmpty(qNode.element, flags)
) {
const proposal = quantToString({ ...qNode, min: 0 })
context.report({
node,
loc: getRegexpLocation(
qNode,
getQuantifierOffsets(qNode),
),
messageId: "confusing",
data: {
min: String(qNode.min),
proposal,
},
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})