Custom ESLint rules with regexp
eslint-plugin-regular-expression
is a plugin that enables defining ESLint rules using regular expressions. You can specify forbidden or required patterns for variable names, function names, string literals, and more.
- Ban Patterns: Prohibit specific patterns using regular expressions for variable names, function names, strings, and more.
- Require Patterns: Enforce the presence of specific patterns (expressed with regular expressions) in your code.
Install with the following command:
npm i eslint eslint-plugin-regular-expression -D
After installation, add the plugin to your ESLint config. eslint-plugin-regular-expression
supports Flat Config.
Example:
eslint.config.js
import regexpRules from 'eslint-plugin-regular-expression';
export default [
{
files: ["*.js", "*.ts"],
plugins: {
'regexp-rules': regexpRules,
},
rules: {
'regexp-rules/banned': ['error', { patterns: ["forbidde*"] }],
'regexp-rules/required': ['error', { patterns: ["required"] }],
},
},
];
In this example:
- The
banned
rule prohibits patterns matchingforbidde*
. - The
required
rule enforces that the patternrequired
appears somewhere in the code.
This plugin can be used in both JavaScript and TypeScript projects.