Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Latest commit

 

History

History
43 lines (31 loc) · 1.44 KB

no-unnecessary-lazy.md

File metadata and controls

43 lines (31 loc) · 1.44 KB

no-unnecessary-lazy 🔧

Disallow unnecessarily lazy quantifiers.

configuration in plugin:clean-regex/recommended: "warn"

Source file
Test file

Description

This rule detects and provides fixers for two kinds of unnecessarily lazy quantifiers.

First, it points out lazy constant quantifiers (e.g. a{6}?). It's obvious that the lazy modifier doesn't affect the quantifier, so it can be removed.

Secondly, it detects lazy modifiers that can be removed based on the characters of the quantified element and the possible characters after the quantifier. Let's take a+?b as an example. The sequence of as always has to be followed by a b, so the regex engine can't be lazy and match as few as as possible because it doesn't have a choice. A lazy modifier only changes a pattern if the regex engine has a choice as to whether it will do another iteration of the quantified element or try to match the element after the quantifier.

Examples

Examples of valid code for this rule:

/a\w??c/
/a[\s\S]*?bar/

Examples of invalid code for this rule:

/ab{3}?c/ -> /ab{3}c/
/b{2,2}?/ -> /b{2,2}/
/ab+?c/   -> /ab+c/