-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add prefer-regexp-exec rule #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bradzacher
merged 25 commits into
typescript-eslint:master
from
ldrick:prefer-regexp-exec
May 10, 2019
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
504c701
feat(eslint-plugin): add rule prefer-regexp-exec
lippmannr f5ee042
Merge branch 'master' into prefer-regexp-exec
ldrick 5ed44d2
test(eslint-plugin): add more tests
ldrick d7b6148
docs(eslint-plugin): add documentation for prefer-regexp-exec rule
ldrick 99e14d7
test(eslint-plugin): improve coverage for prefer-regexp-exec rule
ldrick 8f4a014
docs(eslint-plugin): improve docs for prefer-regexp-exec rule
ldrick 9aa8f35
chore: merge master
ldrick 7134535
fix(eslint-plugin): remove author
ldrick b6d7590
feat(eslint-plugin): add config all.json
ldrick f578be8
chore: merge master
ldrick e9b7914
Merge branch 'master' into prefer-regexp-exec
ldrick 862672e
Merge branch 'master' into prefer-regexp-exec
ldrick 47b0f12
Merge remote-tracking branch 'upstream/master' into prefer-regexp-exec
ldrick f7ddbc5
Merge branch 'master' of https://github.com/ldrick/typescript-eslint …
ldrick c8db8e0
Merge branch 'master' into prefer-regexp-exec
ldrick 44007fa
Merge remote-tracking branch 'upstream/master' into prefer-regexp-exec
lippmannr f8aba10
Revert "feat(eslint-plugin): add config all.json"
ldrick 5babaa3
feat(eslint-plugin): move getTypeName to util
ldrick b186cbf
Merge branch 'master' into prefer-regexp-exec
ldrick 45dbcb8
Merge branch 'master' into prefer-regexp-exec
ldrick 02e66be
Merge branch 'master' into prefer-regexp-exec
ldrick 92e3ecf
Merge branch 'master' into prefer-regexp-exec
ldrick de05e3e
Merge branch 'master' into prefer-regexp-exec
bradzacher 80dff66
Merge remote-tracking branch 'upstream/master' into prefer-regexp-exec
ldrick d234f35
Merge branch 'master' into prefer-regexp-exec
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,53 @@ | ||
# Enforce to use `RegExp#exec` over `String#match` (prefer-regexp-exec) | ||
|
||
`RegExp#exec` is faster than `String#match` and both work the same when not using the `/g` flag. | ||
|
||
## Rule Details | ||
|
||
This rule is aimed at enforcing the more performant way of applying regular expressions on strings. | ||
|
||
From [`String#match` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match): | ||
|
||
> If the regular expression does not include the g flag, returns the same result as RegExp.exec(). | ||
|
||
From [Stack Overflow](https://stackoverflow.com/questions/9214754/what-is-the-difference-between-regexp-s-exec-function-and-string-s-match-fun) | ||
|
||
> `RegExp.prototype.exec` is a lot faster than `String.prototype.match`, but that’s because they are not exactly the same thing, they are different. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
'something'.match(/thing/); | ||
|
||
'some things are just things'.match(/thing/); | ||
|
||
const text = 'something'; | ||
const search = /thing/; | ||
text.match(search); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
/thing/.exec('something'); | ||
|
||
'some things are just things'.match(/thing/g); | ||
|
||
const text = 'something'; | ||
const search = /thing/; | ||
search.exec(text); | ||
``` | ||
|
||
## Options | ||
|
||
There are no options. | ||
|
||
```json | ||
{ | ||
"@typescript-eslint/prefer-regexp-exec": "error" | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you prefer consistent use of `String#match` for both, with `g` flag and without it, you can turn this rule off. |
This file contains hidden or 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,66 @@ | ||
import { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
import { createRule, getParserServices, getTypeName } from '../util'; | ||
import { getStaticValue } from 'eslint-utils'; | ||
|
||
export default createRule({ | ||
name: 'prefer-regexp-exec', | ||
defaultOptions: [], | ||
|
||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Prefer RegExp#exec() over String#match() if no global flag is provided.', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
regExpExecOverStringMatch: 'Use the `RegExp#exec()` method instead.', | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
const globalScope = context.getScope(); | ||
const service = getParserServices(context); | ||
const typeChecker = service.program.getTypeChecker(); | ||
|
||
/** | ||
* Check if a given node is a string. | ||
* @param node The node to check. | ||
*/ | ||
function isStringType(node: TSESTree.Node): boolean { | ||
const objectType = typeChecker.getTypeAtLocation( | ||
service.esTreeNodeToTSNodeMap.get(node), | ||
); | ||
return getTypeName(typeChecker, objectType) === 'string'; | ||
} | ||
|
||
return { | ||
"CallExpression[arguments.length=1] > MemberExpression.callee[property.name='match'][computed=false]"( | ||
node: TSESTree.MemberExpression, | ||
) { | ||
const callNode = node.parent as TSESTree.CallExpression; | ||
const arg = callNode.arguments[0]; | ||
const evaluated = getStaticValue(arg, globalScope); | ||
|
||
// Don't report regular expressions with global flag. | ||
if ( | ||
evaluated && | ||
evaluated.value instanceof RegExp && | ||
evaluated.value.flags.includes('g') | ||
) { | ||
return; | ||
} | ||
|
||
if (isStringType(node.object)) { | ||
context.report({ | ||
node: callNode, | ||
messageId: 'regExpExecOverStringMatch', | ||
}); | ||
return; | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.