Skip to content

Commit 3be66be

Browse files
Add no-contradiction-with-assertion rule (#319)
1 parent d655d8d commit 3be66be

File tree

6 files changed

+558
-0
lines changed

6 files changed

+558
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
104104

105105
| Rule ID | Description | |
106106
|:--------|:------------|:---|
107+
| [regexp/no-contradiction-with-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-contradiction-with-assertion.html) | disallow elements that contradict assertions | |
107108
| [regexp/no-dupe-disjunctions](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-dupe-disjunctions.html) | disallow duplicate disjunctions | :star: |
108109
| [regexp/no-empty-alternative](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-empty-alternative.html) | disallow alternatives without elements | :star: |
109110
| [regexp/no-empty-capturing-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-empty-capturing-group.html) | disallow capturing group that captures empty. | :star: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
1313

1414
| Rule ID | Description | |
1515
|:--------|:------------|:---|
16+
| [regexp/no-contradiction-with-assertion](./no-contradiction-with-assertion.md) | disallow elements that contradict assertions | |
1617
| [regexp/no-dupe-disjunctions](./no-dupe-disjunctions.md) | disallow duplicate disjunctions | :star: |
1718
| [regexp/no-empty-alternative](./no-empty-alternative.md) | disallow alternatives without elements | :star: |
1819
| [regexp/no-empty-capturing-group](./no-empty-capturing-group.md) | disallow capturing group that captures empty. | :star: |
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/no-contradiction-with-assertion"
5+
description: "disallow elements that contradict assertions"
6+
---
7+
# regexp/no-contradiction-with-assertion
8+
9+
> disallow elements that contradict assertions
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
13+
## :book: Rule Details
14+
15+
This rule reports elements that contradict an assertion. All elements reported by this rule fall into one of two categories:
16+
17+
1. An element/alternative that can never be entered.
18+
19+
This means that the element is dead code and can be removed. Example:
20+
21+
<eslint-code-block>
22+
23+
```js
24+
/* eslint regexp/no-contradiction-with-assertion: "error" */
25+
26+
var foo = /(?=a)(\w|-)/;
27+
var foo = /(?=a)b*a/;
28+
```
29+
30+
</eslint-code-block>
31+
32+
2. An element that is always entered.
33+
34+
Right now, only quantifiers with a minimum of 0 are reported in this category. They are contradictory because the minimum of 0 is changed by the assertion to be effectively 1. Example:
35+
36+
<eslint-code-block>
37+
38+
```js
39+
/* eslint regexp/no-contradiction-with-assertion: "error" */
40+
41+
var foo = /a\b-?a/;
42+
var foo = /^foo$[\s\S]*?^end foo/m;
43+
```
44+
45+
</eslint-code-block>
46+
47+
This rule is quite similar to [regexp/no-useless-assertions]. While [regexp/no-useless-assertions] tries to find assertions that contradict the pattern, this rule tries to find parts of the pattern that contradict assertions.
48+
49+
50+
<eslint-code-block>
51+
52+
```js
53+
/* eslint regexp/no-contradiction-with-assertion: "error" */
54+
55+
/* ✓ GOOD */
56+
var foo = /a\b-a/;
57+
var foo = /a\ba/; // handled by regexp/no-useless-assertions
58+
59+
/* ✗ BAD */
60+
var foo = /a\b-?a/;
61+
var foo = /a\b(a|-)/;
62+
var foo = /a\ba*-/;
63+
```
64+
65+
</eslint-code-block>
66+
67+
## :wrench: Options
68+
69+
Nothing.
70+
71+
## :books: Further reading
72+
73+
- [regexp/no-useless-assertions]
74+
75+
[regexp/no-useless-assertions]: ./no-useless-assertions.md
76+
77+
## :mag: Implementation
78+
79+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/no-contradiction-with-assertion.ts)
80+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/no-contradiction-with-assertion.ts)

0 commit comments

Comments
 (0)