/
prefer-starts-ends-with.js
70 lines (64 loc) · 1.3 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
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/prefer-starts-ends-with';
const ruleTester = avaRuleTester(test, {
env: {
es6: true
}
});
const errors = {
startsWith: [{
ruleId: 'prefer-starts-ends-with',
message: 'Prefer `String#startsWith()` over a regex with `^`.'
}],
endsWith: [{
ruleId: 'prefer-starts-ends-with',
message: 'Prefer `String#endsWith()` over a regex 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()()'
]
.concat(validRegex.map(re => `${re}.test(bar)`))
.concat(validRegex.map(re => `bar.match(${re})`)),
invalid: []
.concat(invalidRegex.map(re => ({
code: `${re}.test(bar)`,
errors: errors[`${re}`.startsWith('/^') ? 'startsWith' : 'endsWith']
})))
.concat(invalidRegex.map(re => ({
code: `bar.match(${re})`,
errors: errors[`${re}`.startsWith('/^') ? 'startsWith' : 'endsWith']
})))
});