-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-missing-g-flag.ts
122 lines (120 loc) · 3.05 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
import { SnapshotRuleTester } from "eslint-snapshot-rule-tester"
import rule from "../../../lib/rules/no-missing-g-flag"
const tester = new SnapshotRuleTester({
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
})
tester.run("no-missing-g-flag", rule as any, {
valid: [
`
const s = 'foo'
const ret = s.replaceAll(/foo/g, 'bar')
`,
`
const s = 'foo'
const ret = s.matchAll(/foo/g)
`,
`
const s = 'foo'
const ret = s.replaceAll(new RegExp('foo', 'g'), 'bar')
`,
`
const s = 'foo'
const ret = s.matchAll(new RegExp('foo', 'g'))
`,
// not matchAll and replaceAll
`
const s = 'foo'
const ret = s.replace(/foo/, 'bar')
`,
`
const s = 'foo'
const ret = s.match(/foo/)
`,
// unknown usage
`
const s = 'foo'
const ret = s.replaceAll('bar', /foo/)
`,
`
const ret = /foo/.replaceAll(p, 'bar')
`,
// unknown type
`
const ret = unknown.replaceAll(/foo/, 'bar')
`,
// unknown flags
`
const s = 'foo'
const ret = s.replaceAll(new RegExp('foo', unknown), 'bar')
`,
// ES2024
String.raw`
const s = 'foo'
const ret = s.replaceAll(/[\q{foo}]/gv, 'bar')
`,
],
invalid: [
`
const s = 'foo'
const ret = s.replaceAll(/foo/, 'bar')
`,
`
const s = 'foo'
const ret = s.matchAll(/foo/)
`,
`
const s = 'foo'
const ret = s.replaceAll(new RegExp('foo'), 'bar')
`,
`
const s = 'foo'
const ret = s.matchAll(new RegExp('foo'))
`,
// Unknown pattern
`
const s = 'foo'
const ret = s.matchAll(new RegExp(foo))
`,
// Invalid pattern
`
const s = 'foo'
const ret = s.matchAll(new RegExp('{', 'u'))
`,
// can't fix
`
const p = /foo/
const s = 'foo'
const ret = s.replaceAll(p, 'bar')
`,
// Variable flag
`
const s = 'foo'
const flag = 'i'
const ret = s.replaceAll(new RegExp('foo', flag), 'bar')
`,
// Object property flag
`
const s = 'foo'
const f = { flag: 'i' }
const ret = s.replaceAll(new RegExp('foo', f.flag), 'bar')
`,
// unknown type with strictTypes: false
{
code: `
const ret = s.replaceAll(/foo/, 'bar')
`,
options: [{ strictTypes: false }],
},
{
// ES2024
code: String.raw`
const s = 'foo'
const ret = s.replaceAll(/[\q{foo}]/v, 'bar')
`,
options: [{ strictTypes: false }],
},
],
})