-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-non-standard-flag.ts
51 lines (47 loc) · 1.5 KB
/
no-non-standard-flag.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
import type { RegExpContext, UnparsableRegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
const STANDARD_FLAGS = "dgimsuvy"
export default createRule("no-non-standard-flag", {
meta: {
docs: {
description: "disallow non-standard flags",
category: "Best Practices",
recommended: true,
},
schema: [],
messages: {
unexpected: "Unexpected non-standard flag '{{flag}}'.",
},
type: "suggestion", // "problem",
},
create(context) {
/** The logic of this rule */
function visit({
regexpNode,
getFlagsLocation,
flagsString,
}: RegExpContext | UnparsableRegExpContext) {
if (flagsString) {
const nonStandard = [...flagsString].filter(
(f) => !STANDARD_FLAGS.includes(f),
)
if (nonStandard.length > 0) {
context.report({
node: regexpNode,
loc: getFlagsLocation(),
messageId: "unexpected",
data: { flag: nonStandard[0] },
})
}
}
}
return defineRegexpVisitor(context, {
createVisitor(regexpContext) {
visit(regexpContext)
return {}
},
visitInvalid: visit,
visitUnknown: visit,
})
},
})