-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprefer-regexp-test.ts
102 lines (99 loc) · 2.89 KB
/
prefer-regexp-test.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
import { SnapshotRuleTester } from "eslint-snapshot-rule-tester"
import rule from "../../../lib/rules/prefer-regexp-test"
const tester = new SnapshotRuleTester({
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
})
tester.run("prefer-regexp-test", rule as any, {
valid: [
`
const text = 'something';
const pattern = /thing/;
if (pattern.test(text)) {}
`,
`
const text = 'something';
const pattern = /thing/;
if (text.exec(pattern)) {}
if (pattern.match(text)) {}
`,
`
const text = 'something';
const pattern = /thing/;
if (pattern.execA(text)) {}
if (text.matchA(pattern)) {}
`,
`
const text = 'something';
const pattern = /thing/;
const a = pattern.exec(text)
const b = text.match(pattern)
`,
`
const text = 'something';
const pattern = /thin[[g]]/v;
if (pattern.test(text)) {}
`,
],
invalid: [
`
const text = 'something';
const pattern = /thing/;
if (pattern.exec(text)) {}
if (text.match(pattern)) {}
`,
`
const text = 'something';
const pattern = ()=>/thing/;
if (pattern().exec(text)) {}
if (text.match(pattern())) {}
`,
`
const text = 'something';
const pattern = /thing/;
const b1 = Boolean(pattern.exec(text))
const b2 = Boolean(text.match(pattern))
const b3 = !pattern.exec(text)
const b4 = !text.match(pattern)
const b5 = !(foo && pattern.exec(text))
const b6 = !(foo || text.match(pattern))
`,
`
const re = /a/g;
const str = 'abc';
console.log(!!str.match(re)); // ignore
console.log(!!str.match(re)); // ignore
console.log(!!re.exec(str));
console.log(!!re.exec(str));
console.log(re.test(str));
console.log(re.test(str));
`,
`
const text = 'something';
if (text.match()) {}
const pattern1 = 'some';
if (text.match(pattern1)) {}
const pattern2 = Infinity;
if (text.match(pattern2)) {}
`,
`
const text = 'something';
const pattern = getPattern();
if (text.match(pattern)) {}
`,
`
const text = 'something';
/** @type {RegExp} */
const pattern = getPattern();
if (text.match(pattern)) {}
`,
`
const text = 'something';
const pattern = /thin[[g]]/v;
if (pattern.exec(text)) {}
if (text.match(pattern)) {}
`,
],
})