-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-plus-quantifier.ts
56 lines (53 loc) · 1.91 KB
/
prefer-plus-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 type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import { getQuantifierOffsets } from "../utils/regexp-ast"
export default createRule("prefer-plus-quantifier", {
meta: {
docs: {
description: "enforce using `+` quantifier",
category: "Stylistic Issues",
recommended: true,
},
fixable: "code",
schema: [],
messages: {
unexpected: "Unexpected quantifier '{{expr}}'. Use '+' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
function createVisitor({
node,
getRegexpLocation,
fixReplaceQuant,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onQuantifierEnter(qNode) {
if (qNode.min === 1 && qNode.max === Infinity) {
const [startOffset, endOffset] =
getQuantifierOffsets(qNode)
const text = qNode.raw.slice(startOffset, endOffset)
if (text !== "+") {
context.report({
node,
loc: getRegexpLocation(qNode, [
startOffset,
endOffset,
]),
messageId: "unexpected",
data: {
expr: text,
},
fix: fixReplaceQuant(qNode, "+"),
})
}
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})