-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Open
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin ruleNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a lookWaiting for team members to take a look
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/prefer-regexp-exec
Description
As the description of prefer-regexp-exec
goes, "keeping to consistently using one of the two can help improve code readability".
function getBearer(headers: Headers): string | undefined {
return headers.get('Authorization')?.match(/^Bearer (.+)$/)?.[1]
}
// somewhere else
console.log(/thing/.exec('something'))
We cannot turn both of them into regexp.exec(string)
as there is an optional chain. However, they can be turned into string.match(regexp)
. It is more often for strings compared to RegExp
s to be optional.
While RegExp#exec
might be ssssslightly faster than String#match
, it doesn't matter for most projects.
Fail
/thing/.exec('something')
Pass
'something'.match(/thing/)
Additional Info
No response
Metadata
Metadata
Assignees
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin ruleNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugintriageWaiting for team members to take a lookWaiting for team members to take a look