Specified pattern(s) are disallowed everywhere--in variables, functions, literals, property names, classes, types, interfaces, comments, etc.
Sometimes you don't want to see certain words in your code, period. Maybe you're refactoring out problematic verbiage (e.g., "whitelist"/"blacklist") or maybe just trying to avoid domain-specific terminology whose meanings have evolved, become ambiguous or obsolete. Either way, you want to prevent these patterns from creeping back into your codebase in the future.
There are certainly existing lint rules that can restrict patterns (for example, naming-convention for variable/function names), but forbid-pattern-everywhere
provides a focused, one-stop shop that avoids the "regex pollution" of forcing negative patterns into positive regexes. It also works everywhere, including comments.
This rule will alarm when any part of a source file's text matches any configured pattern.
Examples of incorrect code for this rule, assuming options
is set as { patterns: ['Blah'] }
:
const myBlahVar = 4;
const str = 'Blah string';
function myBlahFunc() {
return 'ok';
}
/* some comment with Blah in it */
Examples of correct code for this rule, assuming options
is set as { patterns: ['(?<!Good)Blah'] }
:
const myGoodBlahVar = 'no error';
The rule accepts an options object with the below structure. Note that any string pattern will be interpreted as a regular expression string, or you can provide an explicit RegExp
object if you're defining these patterns in javascript/typescript.
Either way, all patterns will be applied with the global flag set.
type Options = {
patterns: Array<string | RegExp>;
};
If you don't have any words/patterns you want to globally prohibit.
- naming-convention lint rule