You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/no-empty-lookarounds-assertion.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,25 @@ since: "v0.1.0"
15
15
16
16
This rule reports empty lookahead assertion or empty lookbehind assertion.
17
17
18
+
### What are _empty lookarounds_?
19
+
20
+
An empty lookaround is a lookaround for which at least one path in the lookaround expression contains only elements that do not consume characters and do not assert characters. This means that the lookaround expression will trivially accept any input string.
21
+
22
+
**Examples:**
23
+
24
+
-`(?=)`: One of simplest empty lookarounds.
25
+
-`(?=a*)`: It is possible for `a*` to not consume characters, therefore the lookahead is _empty_.
26
+
-`(?=a|b*)`: Only one path has to not consume characters. Since it is possible for `b*` to not consume characters, the lookahead is _empty_.
27
+
-`(?=a|$)`: This is **not** an empty lookaround. `$` does not _consume_ characters but it does _assert_ characters. Similarly, all other standard assertions (`\b`, `\B`, `^`) are also not empty.
28
+
29
+
### Why are empty lookarounds a problem?
30
+
31
+
Because empty lookarounds accept the empty string, they are essentially non-functional. They will always trivially reject/accept.
32
+
33
+
E.g. `(?=b?)\w` will match `a` just fine. `(?=b?)` will always trivially accept no matter the input string. The same also happens for negated lookarounds but they will trivially reject. E.g. `(?!b?)\w` won't match any input strings.
34
+
35
+
The only way to fix empty lookarounds is to either remove them or to rewrite the lookaround expression to be non-empty.
0 commit comments