-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathno-lazy-ends.ts
77 lines (68 loc) · 2.05 KB
/
no-lazy-ends.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
import { SnapshotRuleTester } from "eslint-snapshot-rule-tester"
import rule from "../../../lib/rules/no-lazy-ends"
const tester = new SnapshotRuleTester({
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
})
tester.run("no-lazy-ends", rule as any, {
valid: [
`/a+?b*/.test(str)`,
`/a??(?:ba+?|c)*/.test(str)`,
`/ba*?$/.test(str)`,
`/a??/`, // UsageOfPattern.unknown
`/a{3}?/.test(str)`, // uselessly lazy but that's not for this rule to correct
// exported
{
code: `
/* exported a */
const a = /a??/
a.test(str)`,
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
},
},
String.raw`/[\q{ab}]?/v.test(str)`,
],
invalid: [
`/a??/.test(str)`,
`/a*?/.test(str)`,
`/a+?/.test(str)`,
`/a{3,7}?/.test(str)`,
`/a{3,}?/.test(str)`,
`/(?:a|b(c+?))/.test(str)`,
`/a(?:c|ab+?)?/.test(str)`,
`
/* ✓ GOOD */
const any = /[\\s\\S]*?/.source;
const pattern = RegExp(\`<script(\\\\s\${any})?>(\${any})<\\/script>\`, "g");
/* ✗ BAD */
const foo = /[\\s\\S]*?/
foo.exec(str)
`,
{
code: `
/* ✓ GOOD */
const any = /[\\s\\S]*?/.source;
const pattern = RegExp(\`<script(\\\\s\${any})?>(\${any})<\\/script>\`, "g");
/* ✗ BAD */
const foo = /[\\s\\S]*?/
foo.exec(str)
`,
options: [{ ignorePartial: true }],
},
{
code: `
/* ✗ BAD */
const any = /[\\s\\S]*?/.source;
const pattern = RegExp(\`<script(\\\\s\${any})?>(\${any})<\\/script>\`, "g");
const foo = /[\\s\\S]*?/
foo.exec(str)
`,
options: [{ ignorePartial: false }],
},
String.raw`/[\q{ab|}]??/v.test(str)`,
],
})