/
prefer-starts-ends-with.js
74 lines (67 loc) · 1.33 KB
/
prefer-starts-ends-with.js
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
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/prefer-starts-ends-with';
const ruleTester = avaRuleTester(test, {
parserOptions: {
ecmaVersion: 2020
}
});
const MESSAGE_STARTS_WITH = 'prefer-starts-with';
const MESSAGE_ENDS_WITH = 'prefer-ends-with';
const validRegex = [
/foo/,
/^foo$/,
/^foo+/,
/foo+$/,
/^[,af]/,
/[,af]$/,
/^\w/,
/\w$/,
/^foo./,
/foo.$/,
/\^foo/,
/^foo/i
];
const invalidRegex = [
/^foo/,
/foo$/,
/^!/,
/!$/,
/^ /,
/ $/
];
ruleTester.run('prefer-starts-ends-with', rule, {
valid: [
'foo.startsWith("bar")',
'foo.endsWith("bar")',
// Ensure it doesn't crash:
'reject(new Error("foo"))',
'"".test()',
'test()',
'test.test()',
'startWith("bar")',
'foo()()',
...validRegex.map(re => `${re}.test(bar)`),
...validRegex.map(re => `bar.match(${re})`)
],
invalid: [
...invalidRegex.map(re => {
const code = `${re}.test(bar)`;
const messageId = re.source.startsWith('^') ? MESSAGE_STARTS_WITH : MESSAGE_ENDS_WITH;
return {
code,
output: code,
errors: [{messageId}]
};
}),
...invalidRegex.map(re => {
const code = `bar.match(${re})`;
const messageId = re.source.startsWith('^') ? MESSAGE_STARTS_WITH : MESSAGE_ENDS_WITH;
return {
code,
output: code,
errors: [{messageId}]
};
})
]
});