Skip to content

Commit c9fa8b1

Browse files
authored
prefer-string-starts-ends-with: Remove check on String#match() (#1002)
1 parent 4c2b00b commit c9fa8b1

File tree

4 files changed

+24
-38
lines changed

4 files changed

+24
-38
lines changed
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
# Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives
1+
# Prefer `String#startsWith()` & `String#endsWith()` over `RegExp#test()`
22

3-
There are several ways of checking whether a string starts or ends with a certain string, such as `string.indexOf('foo') === 0` or using a regex with `/^foo/` or `/foo$/`. ES2015 introduced simpler alternatives named [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). This rule enforces the use of those whenever possible.
3+
Prefer [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) over using a regex with `/^foo/` or `/foo$/`.
44

5-
This rule is partly fixable.
5+
This rule is fixable.
66

77
## Fail
88

99
```js
10-
/^bar/.test(foo);
11-
/bar$/.test(foo);
10+
const foo = /^bar/.test(baz);
11+
```
12+
13+
```js
14+
const foo = /bar$/.test(baz);
1215
```
1316

1417
## Pass
1518

1619
```js
17-
foo.startsWith('bar');
18-
foo.endsWith('bar');
20+
const foo = baz.startsWith('bar');
21+
```
22+
23+
```js
24+
const foo = baz.endsWith('bar');
25+
```
26+
27+
```js
28+
const foo = /^bar/i.test(baz);
1929
```

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Configure it in `package.json`.
170170
- [prefer-spread](docs/rules/prefer-spread.md) - Prefer the spread operator over `Array.from()`. *(fixable)*
171171
- [prefer-string-replace-all](docs/rules/prefer-string-replace-all.md) - Prefer `String#replaceAll()` over regex searches with the global flag. *(fixable)*
172172
- [prefer-string-slice](docs/rules/prefer-string-slice.md) - Prefer `String#slice()` over `String#substr()` and `String#substring()`. *(partly fixable)*
173-
- [prefer-string-starts-ends-with](docs/rules/prefer-string-starts-ends-with.md) - Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives. *(partly fixable)*
173+
- [prefer-string-starts-ends-with](docs/rules/prefer-string-starts-ends-with.md) - Prefer `String#startsWith()` & `String#endsWith()` over `RegExp#test()`. *(fixable)*
174174
- [prefer-string-trim-start-end](docs/rules/prefer-string-trim-start-end.md) - Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`. *(fixable)*
175175
- [prefer-ternary](docs/rules/prefer-ternary.md) - Prefer ternary expressions over simple `if-else` statements. *(fixable)*
176176
- [prefer-type-error](docs/rules/prefer-type-error.md) - Enforce throwing `TypeError` in type checking conditions. *(fixable)*

rules/prefer-string-starts-ends-with.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ const regexTestSelector = [
2424
'[callee.object.regex]'
2525
].join('');
2626

27-
const stringMatchSelector = [
28-
methodSelector({name: 'match', length: 1}),
29-
'[arguments.0.regex]'
30-
].join('');
31-
3227
const checkRegex = ({pattern, flags}) => {
3328
if (flags.includes('i') || flags.includes('m')) {
3429
return;
@@ -97,18 +92,6 @@ const create = context => {
9792
];
9893
}
9994
});
100-
},
101-
[stringMatchSelector](node) {
102-
const {regex} = node.arguments[0];
103-
const result = checkRegex(regex);
104-
if (!result) {
105-
return;
106-
}
107-
108-
context.report({
109-
node,
110-
messageId: result.messageId
111-
});
11295
}
11396
};
11497
};

test/prefer-string-starts-ends-with.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ test({
4545
'startWith("bar")',
4646
'foo()()',
4747

48-
...validRegex.map(re => `${re}.test(bar)`),
49-
...validRegex.map(re => `bar.match(${re})`)
48+
// `prefer-regexp-test` cases
49+
'if (foo.match(/^foo/)) {}',
50+
'if (/^foo/.exec(foo)) {}',
51+
52+
...validRegex.map(re => `${re}.test(bar)`)
5053
],
5154
invalid: [
5255
...invalidRegex.map(re => {
@@ -120,17 +123,7 @@ test({
120123
) {}
121124
`,
122125
errors: [{messageId: MESSAGE_STARTS_WITH}]
123-
},
124-
125-
...invalidRegex.map(re => {
126-
const code = `bar.match(${re})`;
127-
const messageId = re.source.startsWith('^') ? MESSAGE_STARTS_WITH : MESSAGE_ENDS_WITH;
128-
return {
129-
code,
130-
output: code,
131-
errors: [{messageId}]
132-
};
133-
})
126+
}
134127
]
135128
});
136129

0 commit comments

Comments
 (0)