-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-missing-g-flag.ts
143 lines (134 loc) · 4.38 KB
/
no-missing-g-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { ObjectOption } from "../types"
import type { RegExpContext, UnparsableRegExpContext } from "../utils"
import { createRule, defineRegexpVisitor } from "../utils"
import type { ExpressionReference } from "../utils/ast-utils"
import {
extractExpressionReferences,
isKnownMethodCall,
} from "../utils/ast-utils"
import { createTypeTracker } from "../utils/type-tracker"
function parseOption(
userOption:
| {
strictTypes?: boolean
}
| undefined,
) {
let strictTypes = true
if (userOption) {
if (userOption.strictTypes != null) {
strictTypes = userOption.strictTypes
}
}
return {
strictTypes,
}
}
export default createRule("no-missing-g-flag", {
meta: {
docs: {
description:
"disallow missing `g` flag in patterns used in `String#matchAll` and `String#replaceAll`",
category: "Possible Errors",
recommended: true,
},
fixable: "code",
schema: [
{
type: "object",
properties: {
strictTypes: { type: "boolean" },
},
additionalProperties: false,
},
],
messages: {
missingGlobalFlag:
"The pattern given to the argument of `String#{{method}}()` requires the `g` flag, but is missing it.",
},
type: "problem",
},
create(context) {
const { strictTypes } = parseOption(context.options[0] as ObjectOption)
const typeTracer = createTypeTracker(context)
/** The logic of this rule */
function visit(regexpContext: RegExpContext | UnparsableRegExpContext) {
const { regexpNode, flags, flagsString } = regexpContext
if (
flags.global ||
// We were unable to determine which flags were used.
flagsString == null
) {
return
}
for (const ref of extractExpressionReferences(
regexpNode,
context,
)) {
verifyExpressionReference(ref, regexpContext)
}
}
function verifyExpressionReference(
ref: ExpressionReference,
{
regexpNode,
fixReplaceFlags,
flagsString,
}: RegExpContext | UnparsableRegExpContext,
): void {
if (ref.type !== "argument") {
// It is not used for function call arguments.
return
}
const node = ref.callExpression
if (
// It is not specified in the first argument.
node.arguments[0] !== ref.node ||
// It is not replaceAll() and matchAll().
!isKnownMethodCall(node, {
matchAll: 1,
replaceAll: 2,
})
) {
return
}
if (
strictTypes
? !typeTracer.isString(node.callee.object)
: !typeTracer.maybeString(node.callee.object)
) {
// The callee object is not a string.
return
}
context.report({
node: ref.node,
messageId: "missingGlobalFlag",
data: {
method: node.callee.property.name,
},
fix: buildFixer(),
})
function buildFixer() {
if (
node.arguments[0] !== regexpNode ||
((regexpNode.type === "NewExpression" ||
regexpNode.type === "CallExpression") &&
regexpNode.arguments[1] &&
regexpNode.arguments[1].type !== "Literal")
) {
// It can only be safely auto-fixed if it is defined directly in the argument.
return null
}
return fixReplaceFlags(`${flagsString!}g`, false)
}
}
return defineRegexpVisitor(context, {
createVisitor(regexpContext) {
visit(regexpContext)
return {}
},
visitInvalid: visit,
visitUnknown: visit,
})
},
})