Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
e2f94fe
commit 7b74b40
Showing
7 changed files
with
873 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Prefer `RegExp#test()` over `String#match()` and `RegExp#exec()` | ||
|
||
When you want to know whether a pattern is found in a string, use [`RegExp#test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) instead of [`String#match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) and [`RegExp#exec()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec). | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
if (string.match(/unicorn/)) {} | ||
``` | ||
|
||
```js | ||
if (/unicorn/.exec(string)) {} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
if (/unicorn/.test(string)) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict'; | ||
const {isParenthesized} = require('eslint-utils'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const methodSelector = require('./utils/method-selector'); | ||
const {isBooleanNode} = require('./utils/boolean'); | ||
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object'); | ||
|
||
const MESSAGE_ID_REGEXP_EXEC = 'regexp-exec'; | ||
const MESSAGE_ID_STRING_MATCH = 'string-match'; | ||
const messages = { | ||
[MESSAGE_ID_REGEXP_EXEC]: 'Prefer `.test(…)` over `.exec(…)`.', | ||
[MESSAGE_ID_STRING_MATCH]: 'Prefer `RegExp#test(…)` over `String#match(…)`.' | ||
}; | ||
|
||
const regExpExecCallSelector = methodSelector({ | ||
name: 'exec', | ||
length: 1 | ||
}); | ||
|
||
const stringMatchCallSelector = methodSelector({ | ||
name: 'match', | ||
length: 1 | ||
}); | ||
|
||
const create = context => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
[regExpExecCallSelector](node) { | ||
if (!isBooleanNode(node)) { | ||
return; | ||
} | ||
|
||
node = node.callee.property; | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_REGEXP_EXEC, | ||
fix: fixer => fixer.replaceText(node, 'test') | ||
}); | ||
}, | ||
[stringMatchCallSelector](node) { | ||
if (!isBooleanNode(node)) { | ||
return; | ||
} | ||
|
||
const regexpNode = node.arguments[0]; | ||
|
||
if (regexpNode.type === 'Literal' && !regexpNode.regex) { | ||
return; | ||
} | ||
|
||
const stringNode = node.callee.object; | ||
|
||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_STRING_MATCH, | ||
* fix(fixer) { | ||
yield fixer.replaceText(node.callee.property, 'test'); | ||
|
||
let stringText = sourceCode.getText(stringNode); | ||
if ( | ||
!isParenthesized(regexpNode, sourceCode) && | ||
// Only `SequenceExpression` need add parentheses | ||
stringNode.type === 'SequenceExpression' | ||
) { | ||
stringText = `(${stringText})`; | ||
} | ||
|
||
yield fixer.replaceText(regexpNode, stringText); | ||
|
||
let regexpText = sourceCode.getText(regexpNode); | ||
if ( | ||
!isParenthesized(stringNode, sourceCode) && | ||
shouldAddParenthesesToMemberExpressionObject(regexpNode, sourceCode) | ||
) { | ||
regexpText = `(${regexpText})`; | ||
} | ||
|
||
// The nodes that pass `isBooleanNode` cannot have an ASI problem. | ||
|
||
yield fixer.replaceText(stringNode, regexpText); | ||
} | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
messages | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import {outdent} from 'outdent'; | ||
import {test} from './utils/test'; | ||
|
||
test({ | ||
valid: [ | ||
'const bar = !re.test(foo)', | ||
// Not `boolean` | ||
'const matches = foo.match(re) || []', | ||
'const matches = foo.match(re)', | ||
'const matches = re.exec(foo)', | ||
'while (foo = re.exec(bar)) {}', | ||
'while ((foo = re.exec(bar))) {}', | ||
|
||
// Method not match | ||
'if (foo.notMatch(re)) {}', | ||
'if (re.notExec(foo)) {}', | ||
// Not `CallExpression` | ||
'if (foo.match) {}', | ||
'if (re.exec) {}', | ||
// Computed | ||
'if (foo[match](re)) {}', | ||
'if (re[exec](foo)) {}', | ||
'if (foo["match"](re)) {}', | ||
'if (re["exec"](foo)) {}', | ||
// Not `MemberExpression` | ||
'if (match(re)) {}', | ||
'if (exec(foo)) {}', | ||
// More/Less arguments | ||
'if (foo.match()) {}', | ||
'if (re.exec()) {}', | ||
'if (foo.match(re, another)) {}', | ||
'if (re.exec(foo, another)) {}', | ||
'if (foo.match(...[regexp])) {}', | ||
'if (re.exec(...[string])) {}', | ||
// Not regex | ||
'if (foo.match(1)) {}', | ||
'if (foo.match("1")) {}', | ||
'if (foo.match(null)) {}', | ||
'if (foo.match(1n)) {}', | ||
'if (foo.match(true)) {}' | ||
], | ||
invalid: [] | ||
}); | ||
|
||
test.visualize([ | ||
// `String#match()` | ||
'const bar = !foo.match(re)', | ||
'const bar = Boolean(foo.match(re))', | ||
'if (foo.match(re)) {}', | ||
'const bar = foo.match(re) ? 1 : 2', | ||
'while (foo.match(re)) foo = foo.slice(1);', | ||
'do {foo = foo.slice(1)} while (foo.match(re));', | ||
'for (; foo.match(re); ) foo = foo.slice(1);', | ||
|
||
// `RegExp#exec()` | ||
'const bar = !re.exec(foo)', | ||
'const bar = Boolean(re.exec(foo))', | ||
'if (re.exec(foo)) {}', | ||
'const bar = re.exec(foo) ? 1 : 2', | ||
'while (re.exec(foo)) foo = foo.slice(1);', | ||
'do {foo = foo.slice(1)} while (re.exec(foo));', | ||
'for (; re.exec(foo); ) foo = foo.slice(1);', | ||
|
||
// Parentheses | ||
'if ((0, foo).match(re)) {}', | ||
'if ((0, foo).match((re))) {}', | ||
'if ((foo).match(re)) {}', | ||
'if ((foo).match((re))) {}', | ||
'if (foo.match(/re/)) {}', | ||
'if (foo.match(bar)) {}', | ||
'if (foo.match(bar.baz)) {}', | ||
'if (foo.match(bar.baz())) {}', | ||
'if (foo.match(new RegExp("re", "g"))) {}', | ||
'if (foo.match(new SomeRegExp())) {}', | ||
'if (foo.match(new SomeRegExp)) {}', | ||
'if (foo.match(bar?.baz)) {}', | ||
'if (foo.match(bar?.baz())) {}', | ||
'if (foo.match(bar || baz)) {}', | ||
outdent` | ||
async function a() { | ||
if (foo.match(await bar())) {} | ||
} | ||
`, | ||
'if ((foo).match(/re/)) {}', | ||
'if ((foo).match(new SomeRegExp)) {}', | ||
'if ((foo).match(bar?.baz)) {}', | ||
'if ((foo).match(bar?.baz())) {}', | ||
'if ((foo).match(bar || baz)) {}', | ||
outdent` | ||
async function a() { | ||
if ((foo).match(await bar())) {} | ||
} | ||
`, | ||
// Should not need handle ASI problem | ||
'if (foo.match([re][0])) {}', | ||
|
||
// Comments | ||
outdent` | ||
async function a() { | ||
if ( | ||
/* 1 */ foo() /* 2 */ | ||
./* 3 */ match /* 4 */ ( | ||
/* 5 */ await /* 6 */ bar() /* 7 */ | ||
, | ||
/* 8 */ | ||
) | ||
) {} | ||
} | ||
` | ||
]); |
Oops, something went wrong.