Skip to content

Commit

Permalink
prefer-string-starts-ends-with: Remove check on String#match() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 11, 2021
1 parent 4c2b00b commit c9fa8b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 38 deletions.
24 changes: 17 additions & 7 deletions 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);
```
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -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)*
Expand Down
17 changes: 0 additions & 17 deletions rules/prefer-string-starts-ends-with.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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
});
}
};
};
Expand Down
19 changes: 6 additions & 13 deletions test/prefer-string-starts-ends-with.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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}]
};
})
}
]
});

Expand Down

0 comments on commit c9fa8b1

Please sign in to comment.