📎 Implement rule useTopLevelRegex
#2148
Labels
A-Linter
Area: linter
L-JavaScript
Language: JavaScript and super languages
S-Feature
Status: new feature to implement
S-Help-wanted
Status: you're familiar with the code base and want to help the project
Description
I don't know if there's a similar rule out there, we can add a reference later.
I came up with this rule while looking at many JS PRs out there, and I was surprised that there are many developers that don't adopt the following practice. Given the following code, the rule should trigger a case similar to this:
The rule should suggest moving the regex in a top-level variable:
The rule should not provide a code action; it should only suggest moving the regex in a variable at the top level of the module.
The reason for this rule is simple: regex requires a parsing phase, which could be expensive for some long ones. V8 and other engines have a long history of parsing, so I'm sure there is some optimisation, but recreating a new regex every time we call the function
foo
is definitely not optimal.The text was updated successfully, but these errors were encountered: