0.5.2 • Published 3 years ago

eslint-plugin-clean-regex v0.5.2

Weekly downloads
11
License
MIT
Repository
github
Last release
3 years ago

eslint-plugin-clean-regex

Actions Status npm

An ESLint plugin for writing better regular expressions.

⚠️ Deprecated ⚠️

This project is deprecated.

Please use eslint-plugin-regexp instead.

What happened?

eslint-plugin-clean-regex and eslint-plugin-regexp have joined forces. We decided to work together on one ESLint plugin for JavaScript regexes. Since maintaining two plugins with similar rules takes too much work, I decided to stop working on eslint-plugin-clean-regex.

As of right now, eslint-plugin-regexp supports all rules of eslint-plugin-clean-regex along improvements to those rules and with many more useful rules.

Migration

See the migration guide.

About

This is an ESLint plugin to lint JavaScript regular expressions. Its goal is to help both beginners and experts to write better regular expressions by pointing out errors and suggesting improvements.

The plugin offers rules for possible errors, best practices, and coding style in regular expressions.

Right now, this project is still young (and many rules are opinionated). Feel free to open an issue if you think rules are too strict/lax/inflexible. Suggestions and feature requests are welcome as well!

Getting started

You'll need to install ESLint and eslint-plugin-clean-regex:

$ npm i eslint eslint-plugin-clean-regex --save-dev

Note: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-clean-regex globally.

Add clean-regex to the plugins section of your .eslintrc configuration file (you can omit the eslint-plugin- prefix) and configure the rules you want:

{
    "plugins": [
        "clean-regex"
    ],
    "rules": {
        "clean-regex/rule-name": 2
    }
}

You can also use the recommended config:

{
    "plugins": [
        "clean-regex"
    ],
    "extends": [
        "plugin:clean-regex/recommended"
    ]
}

The setting of every rule in the recommended config can be found in the table below.

Highlights

Some highlights of the working and working-together of rules in the recommended config.

Optimize character classes

Before:

- /[0-9]/i
- /[^\s]/
- /[a-fA-F0-9]/i
- /[a-zA-Z0-9_-]/
- /[a-z\d\w]/
- /[\S\d]/
- /[\w\p{ASCII}]/u

After:

- /\d/
- /\S/
- /[a-f0-9]/i
- /[\w-]/
- /\w/
- /\S/
- /\p{ASCII}/u

Simplify patterns

Before:

- /(?:\w|\d)+/
- /(?:a|(b)|c|(?:d)|(?:ee)){0,}/
- /(?<!\w)a+(?=$)/mi
- /[\s\S]#[\0-\uFFFF]/ysi
- /\d*\w(?:[a-z_]|\d+)*/im

After:

- /\w+/
- /(?:[acd]|(b)|ee)*/
- /\ba+$/im
- /.#./sy
- /\w+/

Detect non-functional code and potential errors

- /\1(a)/        // `\1` won't work
- /a+b*?/        // `b*?` can be removed
- /(?:\b)?a/     // `(?:\b)?` can be removed
- /[a-z]+|Foo/i  // `Foo` can be removed
- /(?=a?)\w\Ba/  // `(?=a?)` and `\B` always accept and can be removed
- /[*/+-^&|]/    // `+-^` will match everything from \x2B to \x5E including all character A to Z

Supported Rules

Fixable rules are denoted with a :wrench:.

Problems

RuleDescription
confusing-quantifierWarn about confusing quantifiers.
disjoint-alternativesDisallow different alternatives that can match the same words.
no-empty-alternativeDisallow alternatives without elements.
no-empty-backreferenceDisallow backreferences that will always be replaced with the empty string.
no-empty-lookaroundDisallow lookarounds that can match the empty string.
no-lazy-endsDisallow lazy quantifiers at the end of an expression.
no-obscure-rangeDisallow obscure ranges in character classes.
no-octal-escapeDisallow octal escapes outside of character classes.
no-optional-assertionDisallow optional assertions.
no-potentially-empty-backreferenceDisallow backreferences that reference a group that might not be matched.
no-unnecessary-assertionsDisallow assertions that are known to always accept (or reject).
:wrench:no-zero-quantifierDisallow quantifiers with a maximum of 0.
optimal-lookaround-quantifierDisallows the alternatives of lookarounds that end with a non-constant quantifier.

Suggestions

RuleDescription
:wrench:consistent-match-all-charactersUse one character class consistently whenever all characters have to be matched.
:wrench:identity-escapeHow to handle identity escapes.
no-constant-capturing-groupDisallow capturing groups that can match only one word.
:wrench:no-trivially-nested-lookaroundDisallow lookarounds that only contain another assertion.
:wrench:no-trivially-nested-quantifierDisallow nested quantifiers that can be rewritten as one quantifier.
:wrench:no-unnecessary-character-classDisallow unnecessary character classes.
:wrench:no-unnecessary-flagDisallow unnecessary regex flags.
:wrench:no-unnecessary-groupDisallow unnecessary non-capturing groups.
:wrench:no-unnecessary-lazyDisallow unnecessarily lazy quantifiers.
:wrench:no-unnecessary-quantifierDisallow unnecessary quantifiers.
:wrench:optimal-concatenation-quantifierUse optimal quantifiers for concatenated quantified characters.
:wrench:optimized-character-classDisallows unnecessary elements in character classes.
:wrench:prefer-character-classPrefer character classes wherever possible instead of alternations.
:wrench:prefer-predefined-assertionPrefer predefined assertions over equivalent lookarounds.
:wrench:prefer-predefined-character-setPrefer predefined character sets instead of their more verbose form.
:wrench:prefer-predefined-quantifiersPrefer predefined quantifiers (+*?) instead of their more verbose form.
:wrench:simple-constant-quantifierPrefer simple constant quantifiers over the range form.
:wrench:sort-flagsRequires the regex flags to be sorted.