|
| 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