diff --git a/docs/rules/prefer-string-starts-ends-with.md b/docs/rules/prefer-string-starts-ends-with.md index 3966915737..daf5596389 100644 --- a/docs/rules/prefer-string-starts-ends-with.md +++ b/docs/rules/prefer-string-starts-ends-with.md @@ -1,19 +1,29 @@ -# Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives +# Prefer `String#startsWith()` & `String#endsWith()` over `RegExp#test()` -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. +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$/`. -This rule is partly fixable. +This rule is fixable. ## Fail ```js -/^bar/.test(foo); -/bar$/.test(foo); +const foo = /^bar/.test(baz); +``` + +```js +const foo = /bar$/.test(baz); ``` ## Pass ```js -foo.startsWith('bar'); -foo.endsWith('bar'); +const foo = baz.startsWith('bar'); +``` + +```js +const foo = baz.endsWith('bar'); +``` + +```js +const foo = /^bar/i.test(baz); ``` diff --git a/readme.md b/readme.md index 0a57c221d7..855b12cde4 100644 --- a/readme.md +++ b/readme.md @@ -170,7 +170,7 @@ Configure it in `package.json`. - [prefer-spread](docs/rules/prefer-spread.md) - Prefer the spread operator over `Array.from()`. *(fixable)* - [prefer-string-replace-all](docs/rules/prefer-string-replace-all.md) - Prefer `String#replaceAll()` over regex searches with the global flag. *(fixable)* - [prefer-string-slice](docs/rules/prefer-string-slice.md) - Prefer `String#slice()` over `String#substr()` and `String#substring()`. *(partly fixable)* -- [prefer-string-starts-ends-with](docs/rules/prefer-string-starts-ends-with.md) - Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives. *(partly fixable)* +- [prefer-string-starts-ends-with](docs/rules/prefer-string-starts-ends-with.md) - Prefer `String#startsWith()` & `String#endsWith()` over `RegExp#test()`. *(fixable)* - [prefer-string-trim-start-end](docs/rules/prefer-string-trim-start-end.md) - Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`. *(fixable)* - [prefer-ternary](docs/rules/prefer-ternary.md) - Prefer ternary expressions over simple `if-else` statements. *(fixable)* - [prefer-type-error](docs/rules/prefer-type-error.md) - Enforce throwing `TypeError` in type checking conditions. *(fixable)* diff --git a/rules/prefer-string-starts-ends-with.js b/rules/prefer-string-starts-ends-with.js index a0d2f238cb..8b8e9c6ec1 100644 --- a/rules/prefer-string-starts-ends-with.js +++ b/rules/prefer-string-starts-ends-with.js @@ -24,11 +24,6 @@ const regexTestSelector = [ '[callee.object.regex]' ].join(''); -const stringMatchSelector = [ - methodSelector({name: 'match', length: 1}), - '[arguments.0.regex]' -].join(''); - const checkRegex = ({pattern, flags}) => { if (flags.includes('i') || flags.includes('m')) { return; @@ -97,18 +92,6 @@ const create = context => { ]; } }); - }, - [stringMatchSelector](node) { - const {regex} = node.arguments[0]; - const result = checkRegex(regex); - if (!result) { - return; - } - - context.report({ - node, - messageId: result.messageId - }); } }; }; diff --git a/test/prefer-string-starts-ends-with.js b/test/prefer-string-starts-ends-with.js index 921cbf7b1c..211560ca28 100644 --- a/test/prefer-string-starts-ends-with.js +++ b/test/prefer-string-starts-ends-with.js @@ -45,8 +45,11 @@ test({ 'startWith("bar")', 'foo()()', - ...validRegex.map(re => `${re}.test(bar)`), - ...validRegex.map(re => `bar.match(${re})`) + // `prefer-regexp-test` cases + 'if (foo.match(/^foo/)) {}', + 'if (/^foo/.exec(foo)) {}', + + ...validRegex.map(re => `${re}.test(bar)`) ], invalid: [ ...invalidRegex.map(re => { @@ -120,17 +123,7 @@ test({ ) {} `, errors: [{messageId: MESSAGE_STARTS_WITH}] - }, - - ...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}] - }; - }) + } ] });