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

Latest commit

 

History

History
45 lines (31 loc) · 1.49 KB

no-optional-assertion.md

File metadata and controls

45 lines (31 loc) · 1.49 KB

no-optional-assertion

Disallow optional assertions.

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

Source file
Test file

Description

Assertions that as quantified in some way can be considered optional, if the quantifier as a minimum of zero.

A simple example is the following pattern: /a(?:$)*b/. The end-of-string assertion will obviously reject but if that happens, it will simply be ignored because of the quantifier. The assertion is essentially optional, serving no function whatsoever.

More generally, an assertion is optional, if the concatenation of all possible paths that start at the start of a zero-quantified element, end at the end of that element, and contain the assertion does not consume characters.

Here's an example of that: a(?:foo|(?<!-)(?:-|\b))*b. The \b is optional. The lookbehind is not optional because following group can consume a character.

The presence of optional assertions don't change the meaning of the pattern, so they are dead code.

Examples

Examples of valid code for this rule:

/\w+(?::|\b)/

Examples of invalid code for this rule:

/(?:^)?\w+/   // warns about `^`
/\w+(?::|$)?/ // warns about `$`