Skip to content

Latest commit

 

History

History
65 lines (44 loc) · 1.97 KB

no-useless-assertions.md

File metadata and controls

65 lines (44 loc) · 1.97 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-useless-assertions
disallow assertions that are known to always accept (or reject)
v0.9.0

regexp/no-useless-assertions

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

disallow assertions that are known to always accept (or reject)

📖 Rule Details

Some assertion are unnecessary because the rest of the pattern forces them to always be accept (or reject).

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

/* ✓ GOOD */
var foo = /\bfoo\b/;

/* ✗ BAD */
var foo = /#\bfoo/;    // \b will always accept
var foo = /foo\bbar/;  // \b will always reject
var foo = /$foo/;      // $ will always reject
var foo = /(?=\w)\d+/; // (?=\w) will always accept

Limitations

Right now, this rule is implemented by only looking a single character ahead and behind. This is enough to determine whether the builtin assertions (\b, \B, ^, $) trivially reject or accept but it is not enough for all lookarounds. The algorithm determining the characters ahead and behind is very conservative which can lead to false negatives.

🔧 Options

Nothing.

❤️ Compatibility

This rule was taken from eslint-plugin-clean-regex.
This rule is compatible with clean-regex/no-unnecessary-assertions rule.

🚀 Version

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

🔍 Implementation