eslint-plugin-regular-expression

logo

Custom ESLint rules with regexp

logo

Overview

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.

Features

  • 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.

Installation

Install with the following command:

npm i eslint eslint-plugin-regular-expression -D

Usage

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 matching forbidde*.
  • The required rule enforces that the pattern required appears somewhere in the code.

This plugin can be used in both JavaScript and TypeScript projects.

Rule List

banned

Description

Prohibits identifiers and literals matching the given regular expressions.

TargetExamplesSupported
IdentifierVariable names, function names
LiteralStrings, numbers

Options

  • patterns: An array of regular expression patterns to ban.

Example

rules: {
  'regexp-rules/banned': ["error", { "patterns": ["^foo", "abc$"] }]
}

❌ Invalid

const fooVar = "test";
// Identifier "fooVar" is banned by pattern "^foo".eslint(regexp-rules/banned)
const str = "myabc";
// Literal "myabc" is banned by pattern "abc$".eslint(regexp-rules/banned)

✅ Valid

const barVar = "test"; // OK
const str = "defghi"; // OK

banned-identifier

Description

Prohibits identifiers (e.g., variable or function names) that match specified regular expressions.

TargetExamplesSupported
IdentifierVariable names, function names
LiteralStrings, numbers

Options

  • patterns: An array of regular expression patterns to ban.

Example

rules: {
  'regexp-rules/banned-identifier': ["error", { "patterns": ["^foo", "bar$"] }]
}

❌ Invalid

const fooVariable = 1;
// Identifier "fooVariable" is banned by pattern "^foo".eslint(regexp-rules/banned-identifier)
let myBar = "test";
// Identifier "bar" is banned by pattern "bar$".eslint(regexp-rules/banned-identifier)

✅ Valid

const testVariable = 1; // OK
let baz = "test"; // OK

banned-literal

Description

Prohibits literals (e.g., strings, numbers) that match specified regular expressions.

TargetExamplesSupported
IdentifierVariable, function
LiteralStrings, numbers

Options

  • patterns: An array of regular expression patterns to ban.

Example

rules: {
  'regexp-rules/banned-literal': ["error", { "patterns": ["^abc", "xyz$"] }]
}

❌ Invalid

const str = "abcTest";
// Literal "abcTest" is banned by pattern "^abc".eslint(regexp-rules/banned-literal)
const another = "endxyz";
// Literal "endxyz" is banned by pattern "xyz$".eslint(regexp-rules/banned-literal)

✅ Valid

const str = "defTest";
const another = "end";

required

Description

Identifiers or literals must match at least one of the specified regular expressions.

TargetExamplesSupported
IdentifierVariable names, function names
LiteralStrings, numbers

Options

  • patterns: An array of required regular expression patterns.

Example

rules: {
  'regexp-rules/required': ["error", { "patterns": ["^foo", "bar"] }]
}

❌ Invalid

const varName = "invalid";
// No identifiers or literals matching the required patterns: ^foo, bar.eslint(regexp-rules/required)

✅ Valid

const fooVar = "barValue";
// OK

required-identifier

Description

Identifiers must match at least one of the specified regular expressions.

TargetExamplesSupported
IdentifierVariable names, function names
LiteralStrings, numbers

Options

  • patterns: An array of required regular expression patterns.

Example

rules: {
  'regexp-rules/required-identifier': ["error", { "patterns": ["^my", "^foo"] }]
}

❌ Invalid

const varName = 1;
// No identifiers matching the required patterns: ^my, ^foo.eslint(regexp-rules/required-identifier)

✅ Valid

const myVar = 1;
const fooBar = 2;
// OK

required-literal

Description

Literals must match at least one of the specified regular expressions.

TargetExamplesSupported
IdentifierVariable, func.
LiteralStrings, numbers

Options

  • patterns: An array of required regular expression patterns.

Example

rules: {
  'regexp-rules/required-literal': ["error", { "patterns": ["hello", "world"] }]
}

❌ Invalid

const text = "invalid";
// No string literals matching the required patterns: hello, world.eslint(regexp-rules/required-literal)

✅ Valid

const greeting = "hello";
const place = "world";
// OK