Skip to content

Commit 232ca08

Browse files
Add sort-alternatives rule (#235)
* WIP * WIP * Update to `[email protected]` * Improved sorting for numbers * Minor refactoring * Finished work * Allow apostrophes * Added docs * Updated compare function
1 parent ae0bca9 commit 232ca08

File tree

7 files changed

+565
-0
lines changed

7 files changed

+565
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
148148
| [regexp/prefer-range](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-range.html) | enforce using character class range | :wrench: |
149149
| [regexp/prefer-regexp-exec](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-regexp-exec.html) | enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | |
150150
| [regexp/prefer-regexp-test](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-regexp-test.html) | enforce that `RegExp#test` is used instead of `String#match` and `RegExp#exec` | :wrench: |
151+
| [regexp/sort-alternatives](https://ota-meshi.github.io/eslint-plugin-regexp/rules/sort-alternatives.html) | sort alternatives if order doesn't matter | :wrench: |
151152

152153
### Stylistic Issues
153154

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
6262
| [regexp/prefer-range](./prefer-range.md) | enforce using character class range | :wrench: |
6363
| [regexp/prefer-regexp-exec](./prefer-regexp-exec.md) | enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided | |
6464
| [regexp/prefer-regexp-test](./prefer-regexp-test.md) | enforce that `RegExp#test` is used instead of `String#match` and `RegExp#exec` | :wrench: |
65+
| [regexp/sort-alternatives](./sort-alternatives.md) | sort alternatives if order doesn't matter | :wrench: |
6566

6667
### Stylistic Issues
6768

docs/rules/sort-alternatives.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/sort-alternatives"
5+
description: "sort alternatives if order doesn't matter"
6+
---
7+
# regexp/sort-alternatives
8+
9+
> sort alternatives if order doesn't matter
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+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
13+
14+
## :book: Rule Details
15+
16+
This rule will sort alternatives to improve readability and maintainability.
17+
18+
The primary target of this rule are lists of words and/or numbers. These lists are somewhat common and sorting them makes it easy for readers to check whether a particular word or number is included.
19+
20+
This rule will only sort alternatives if reordering the alternatives doesn't affect the pattern.
21+
22+
<eslint-code-block fix>
23+
24+
```js
25+
/* eslint regexp/sort-alternatives: "error" */
26+
27+
/* ✓ GOOD */
28+
var foo = /\b(1|2|3)\b/;
29+
var foo = /\b(alpha|beta|gamma)\b/;
30+
31+
/* ✗ BAD */
32+
var foo = /\b(2|1|3)\b/;
33+
var foo = /__(?:Foo|Bar)__/;
34+
var foo = /\((?:TM|R|C)\)/;
35+
```
36+
37+
</eslint-code-block>
38+
39+
## :wrench: Options
40+
41+
Nothing.
42+
43+
## :mag: Implementation
44+
45+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/sort-alternatives.ts)
46+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/sort-alternatives.ts)

0 commit comments

Comments
 (0)