-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-empty-lookarounds-assertion.ts
55 lines (52 loc) · 1.76 KB
/
no-empty-lookarounds-assertion.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
import type { RegExpVisitor } from "@eslint-community/regexpp/visitor"
import { isPotentiallyEmpty } from "regexp-ast-analysis"
import type { RegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
export default createRule("no-empty-lookarounds-assertion", {
meta: {
docs: {
description:
"disallow empty lookahead assertion or empty lookbehind assertion",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
unexpected:
"Unexpected empty {{kind}}. It will trivially {{result}} all inputs.",
},
type: "suggestion",
},
create(context) {
function createVisitor({
node,
flags,
getRegexpLocation,
}: RegExpContext): RegExpVisitor.Handlers {
return {
onAssertionEnter(aNode) {
if (
aNode.kind !== "lookahead" &&
aNode.kind !== "lookbehind"
) {
return
}
if (isPotentiallyEmpty(aNode.alternatives, flags)) {
context.report({
node,
loc: getRegexpLocation(aNode),
messageId: "unexpected",
data: {
kind: aNode.kind,
result: aNode.negate ? "reject" : "accept",
},
})
}
},
}
}
return defineRegexpVisitor(context, {
createVisitor,
})
},
})