-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-named-replacement.ts
38 lines (36 loc) · 1.27 KB
/
prefer-named-replacement.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
import { SnapshotRuleTester } from "eslint-snapshot-rule-tester"
import rule from "../../../lib/rules/prefer-named-replacement"
const tester = new SnapshotRuleTester({
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
})
tester.run("prefer-named-replacement", rule as any, {
valid: [
`"str".replace(/regexp/, "foo")`,
`"str".replace(/a(b)c/, "_$1_")`,
`"str".replaceAll(/a(b)c/, "_$1_")`,
`"str".replace(/a(?<foo>b)c/, "_$<foo>_")`,
`"str".replaceAll(/a(?<foo>b)c/, "_$<foo>_")`,
`"str".replace(/a(?<foo>b)c/, "_$0_")`,
`"str".replace(/(a)(?<foo>b)c/, "_$1_")`,
`"str".replace(/a(b)c/, "_$2_")`,
`unknown.replace(/a(?<foo>b)c/, "_$1_")`,
`unknown.replaceAll(/a(?<foo>b)c/, "_$1_")`,
],
invalid: [
`"str".replace(/a(?<foo>b)c/, "_$1_")`,
`"str".replace(/a(?<foo>b)c/v, "_$1_")`,
`"str".replaceAll(/a(?<foo>b)c/, "_$1_")`,
`"str".replace(/(a)(?<foo>b)c/, "_$1$2_")`,
{
code: `unknown.replace(/a(?<foo>b)c/, "_$1_")`,
options: [{ strictTypes: false }],
},
{
code: `unknown.replaceAll(/a(?<foo>b)c/, "_$1_")`,
options: [{ strictTypes: false }],
},
],
})