Skip to content

Latest commit

 

History

History
66 lines (43 loc) · 2.05 KB

no-useless-lazy.md

File metadata and controls

66 lines (43 loc) · 2.05 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-useless-lazy
disallow unnecessarily non-greedy quantifiers
v0.10.0

regexp/no-useless-lazy

💼 This rule is enabled in the ✅ plugin:regexp/recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

disallow unnecessarily non-greedy quantifiers

📖 Rule Details

This rule reports lazy quantifiers that don't need to by lazy.

There are two reasons why a lazy quantifier doesn't have to lazy:

  1. It's a constant quantifier (e.g. a{3}?).

  2. The quantifier is effectively possessive (e.g. a+?b).

    Whether a quantifier (let's call it q) is effectively possessive depends on the expression after it (let's call it e). q is effectively possessive if q cannot accept the character accepted by e and e cannot accept the characters accepted by q.

    In the example above, the character a and the character b do not overlap. Therefore the quantifier a+ is possessive.

    Since an effectively possessive quantifier cannot give up characters to the expression after it, it doesn't matter whether the quantifier greedy or lazy. However, greedy quantifiers should be preferred because they require less characters to write and are easier to visually parse.

/* eslint regexp/no-useless-lazy: "error" */

/* ✓ GOOD */
var foo = /a*?/;
var foo = /a+?/;
var foo = /a{4,}?/;
var foo = /a{2,4}?/;
var foo = /a[\s\S]*?bar/;

/* ✗ BAD */
var foo = /a{1}?/;
var foo = /a{4}?/;
var foo = /a{2,2}?/;
var foo = /ab+?c/;

🔧 Options

Nothing.

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.10.0

🔍 Implementation