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

Latest commit

 

History

History
63 lines (44 loc) · 1.54 KB

no-constant-capturing-group.md

File metadata and controls

63 lines (44 loc) · 1.54 KB

no-constant-capturing-group

Disallow capturing groups that can match only one word.

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

Source file
Test file

Description

Constant capturing groups can only match one word.

Because they can only match one word, they should be replaced with the constant string they capture for better performance. This is especially the case if the capturing groups only matches the empty word.

E.g. /(foo)/, a()b, a(\b)

Examples

Examples of valid code for this rule:

/(a)/i
/(a|b)/
/(a*)/
/(a)/  // constant but it doesn't match the empty word

Examples of invalid code for this rule:

/()/   // warn about `()`
/(\b)/ // warn about `(\b)`

ignoreNonEmpty: boolean

If this option is set to true, the rule will ignore capturing groups that can match non-empty words. This option is true by default.

ignoreNonEmpty: false

Examples of valid code for this rule with ignoreNonEmpty: false:

/(a)/i
/(a|b)/
/(a*)/

Examples of invalid code for this rule with ignoreNonEmpty: false:

/(a)/  // warn about `(a)`
/()/   // warn about `()`
/(\b)/ // warn about `(\b)`