-
Notifications
You must be signed in to change notification settings - Fork 54
feat: add sort-regexp rule #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
54a24a6
44377c4
9b65948
3379930
72820b1
7220e5b
a8f9368
fa5617e
b22dc93
6ee2209
bc57a04
3084606
09ee12d
da7b5f5
1b1623d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,272 @@ | ||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||
| title: sort-regexp | ||||||||||||||||||||||||||||||||||||||
| description: Keep regular expressions predictable by sorting flags, character classes, and alternation branches. This ESLint rule helps you avoid subtle regex bugs and improves readability | ||||||||||||||||||||||||||||||||||||||
| shortDescription: Enforce sorted regular expressions | ||||||||||||||||||||||||||||||||||||||
| keywords: | ||||||||||||||||||||||||||||||||||||||
| - eslint | ||||||||||||||||||||||||||||||||||||||
| - regex sorting | ||||||||||||||||||||||||||||||||||||||
| - regular expressions | ||||||||||||||||||||||||||||||||||||||
| - eslint rule | ||||||||||||||||||||||||||||||||||||||
| - code quality | ||||||||||||||||||||||||||||||||||||||
| - javascript linting | ||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import CodeExample from '../../components/CodeExample.svelte' | ||||||||||||||||||||||||||||||||||||||
| import CodeTabs from '../../components/CodeTabs.svelte' | ||||||||||||||||||||||||||||||||||||||
| import dedent from 'dedent' | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Enforce consistent ordering in regular expressions: flags, character-class elements, and alternation branches inside capture groups or the top-level pattern. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Large patterns become easier to maintain when related branches are grouped and repeatedly used flags or character classes follow the same order. This rule helps you spot missing alternatives, avoid duplicated work, and keeps refactors safer by applying one sorting strategy across your codebase. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| The rule is **safe** – it only reorders elements without changing their meaning. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs overstate safety; add caveat and guidance Reordering alternations is not universally safe (e.g., shared prefixes like - The rule is **safe** – it only reorders elements without changing their meaning.
+ Important: Reordering can change behavior in specific cases (e.g., alternatives that share a prefix like `(ab|a)`, or patterns whose numeric backreferences depend on the textual order of capturing groups). The rule avoids such changes when it detects risk; otherwise, prefer `type: 'unsorted'` or disable the rule locally for those patterns.📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hugop95 I fixed it |
||||||||||||||||||||||||||||||||||||||
| ## Try it out | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <CodeExample | ||||||||||||||||||||||||||||||||||||||
| alphabetical={dedent` | ||||||||||||||||||||||||||||||||||||||
| const patterns = { | ||||||||||||||||||||||||||||||||||||||
| alternatives: /(apple|banana|orange|pear)/, | ||||||||||||||||||||||||||||||||||||||
| alias: /(?<role>administrator|guest|user)/, | ||||||||||||||||||||||||||||||||||||||
| characterClass: /[0-9A-Za-fz]/, | ||||||||||||||||||||||||||||||||||||||
| flags: /pattern/gimsuy, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| `} | ||||||||||||||||||||||||||||||||||||||
| lineLength={dedent` | ||||||||||||||||||||||||||||||||||||||
| const patterns = { | ||||||||||||||||||||||||||||||||||||||
| alternatives: /(banana|orange|apple|pear)/, | ||||||||||||||||||||||||||||||||||||||
| alias: /(?<role>administrator|guest|user)/, | ||||||||||||||||||||||||||||||||||||||
| characterClass: /[0-9A-Za-fz]/, | ||||||||||||||||||||||||||||||||||||||
| flags: /pattern/yusmig, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| `} | ||||||||||||||||||||||||||||||||||||||
| initial={dedent` | ||||||||||||||||||||||||||||||||||||||
| const patterns = { | ||||||||||||||||||||||||||||||||||||||
| alternatives: /(pear|apple|orange|banana)/, | ||||||||||||||||||||||||||||||||||||||
| alias: /(?<role>user|administrator|guest)/, | ||||||||||||||||||||||||||||||||||||||
| characterClass: /[z0-9a-fA-Z]/, | ||||||||||||||||||||||||||||||||||||||
| flags: /pattern/yusmig, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| `} | ||||||||||||||||||||||||||||||||||||||
| client:load | ||||||||||||||||||||||||||||||||||||||
| lang="tsx" | ||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ## Options | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| This rule accepts an options object with the following properties: | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### type | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `'alphabetical'`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Specifies the sorting strategy applied to matches (flags, character-class elements, and alternation branches). | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `'alphabetical'` — Sort values alphabetically using [`localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare). | ||||||||||||||||||||||||||||||||||||||
| - `'natural'` — Sort values in [natural](https://github.com/yobacca/natural-orderby) order (e.g., `(?=2)` comes before `(?=10)`). | ||||||||||||||||||||||||||||||||||||||
| - `'line-length'` — Sort values by their textual length. | ||||||||||||||||||||||||||||||||||||||
| - `'custom'` — Sort values according to a custom alphabet defined in [`alphabet`](#alphabet). | ||||||||||||||||||||||||||||||||||||||
| - `'unsorted'` — Do not reorder values. [`groups`](#groups) and [`customGroups`](#customgroups) are still applied. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### order | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `'asc'`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Specifies the direction of sorting. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `'asc'` — Ascending order (shorter to longer, A to Z). | ||||||||||||||||||||||||||||||||||||||
| - `'desc'` — Descending order (longer to shorter, Z to A). | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### fallbackSort | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub> | ||||||||||||||||||||||||||||||||||||||
| type: | ||||||||||||||||||||||||||||||||||||||
| ```ts | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| type: 'alphabetical' | 'natural' | 'line-length' | 'custom' | 'unsorted' | ||||||||||||||||||||||||||||||||||||||
| order?: 'asc' | 'desc' | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||
| </sub> | ||||||||||||||||||||||||||||||||||||||
| <sub>default: `{ type: 'unsorted' }`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Used when the primary [`type`](#type) considers two values equal. For example, rely on alphabetical order when two branches share the same length. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### alphabet | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `''`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Defines the custom alphabet for `'custom'` sorting. Use the [`Alphabet` helper](https://perfectionist.dev/alphabet) to build consistent alphabets quickly. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### locales | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `'en-US'`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Locales passed to `localeCompare` when alphabetical comparisons are required. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### ignoreCase | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `true`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Whether comparisons should be case-insensitive. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `true` — Treat upper- and lowercase branches as equal (default). | ||||||||||||||||||||||||||||||||||||||
| - `false` — Preserve case sensitivity. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### specialCharacters | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `'keep'`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Controls how special characters are handled before comparison. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `'keep'` — Keep them (default). | ||||||||||||||||||||||||||||||||||||||
| - `'trim'` — Trim leading special characters. | ||||||||||||||||||||||||||||||||||||||
| - `'remove'` — Remove all special characters. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### ignoreAlias | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub>default: `false`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Determines whether named capturing group aliases (e.g., `(?<role>...)`) are considered during comparison. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `false` — Sort by `alias: pattern` (default). | ||||||||||||||||||||||||||||||||||||||
| - `true` — Ignore the alias and only compare the branch content. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| This is useful when you only care about the pattern and not how it is aliased. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### customGroups | ||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For other rules, this section is after |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub> | ||||||||||||||||||||||||||||||||||||||
| type: `Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>` | ||||||||||||||||||||||||||||||||||||||
| </sub> | ||||||||||||||||||||||||||||||||||||||
| <sub>default: `[]`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Define custom groups to control how alternatives are organized before sorting. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ```ts | ||||||||||||||||||||||||||||||||||||||
| interface CustomGroupDefinition { | ||||||||||||||||||||||||||||||||||||||
| groupName: string | ||||||||||||||||||||||||||||||||||||||
| selector?: 'alias' | 'pattern' | ||||||||||||||||||||||||||||||||||||||
| elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[] | ||||||||||||||||||||||||||||||||||||||
| elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[] | ||||||||||||||||||||||||||||||||||||||
| type?: 'alphabetical' | 'natural' | 'line-length' | 'custom' | 'unsorted' | ||||||||||||||||||||||||||||||||||||||
| order?: 'asc' | 'desc' | ||||||||||||||||||||||||||||||||||||||
| fallbackSort?: { type: string; order?: 'asc' | 'desc' } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| interface CustomGroupAnyOfDefinition { | ||||||||||||||||||||||||||||||||||||||
| groupName: string | ||||||||||||||||||||||||||||||||||||||
| anyOf: Array<{ | ||||||||||||||||||||||||||||||||||||||
| selector?: 'alias' | 'pattern' | ||||||||||||||||||||||||||||||||||||||
| elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[] | ||||||||||||||||||||||||||||||||||||||
| elementValuePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[] | ||||||||||||||||||||||||||||||||||||||
| }> | ||||||||||||||||||||||||||||||||||||||
| type?: 'alphabetical' | 'natural' | 'line-length' | 'custom' | 'unsorted' | ||||||||||||||||||||||||||||||||||||||
| order?: 'asc' | 'desc' | ||||||||||||||||||||||||||||||||||||||
| fallbackSort?: { type: string; order?: 'asc' | 'desc' } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Attributes: | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - `groupName` — Identifier referenced in [`groups`](#groups). | ||||||||||||||||||||||||||||||||||||||
| - `selector` — Filter by element type: | ||||||||||||||||||||||||||||||||||||||
| - `'alias'` — Named capturing group alternatives. | ||||||||||||||||||||||||||||||||||||||
| - `'pattern'` — Plain alternatives without aliases. | ||||||||||||||||||||||||||||||||||||||
| - `elementNamePattern` — Regex applied to the alias (if present). | ||||||||||||||||||||||||||||||||||||||
| - `elementValuePattern` — Regex applied to the branch content. | ||||||||||||||||||||||||||||||||||||||
| - `type`, `order`, `fallbackSort` — Override the respective global options for the group. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Custom groups are evaluated in order; the first match wins and overrides predefined groups. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ### groups | ||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would take the same eslint-plugin-perfectionist/docs/content/rules/sort-objects.mdx Lines 460 to 477 in f9919cb
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <sub> | ||||||||||||||||||||||||||||||||||||||
| type: `Array<string | string[]>` | ||||||||||||||||||||||||||||||||||||||
| </sub> | ||||||||||||||||||||||||||||||||||||||
| <sub>default: `[]`</sub> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Controls the order in which groups are evaluated. Use the selectors from [`customGroups`](#customgroups) or custom group names. When an element does not match any provided group, it falls back to the implicit `unknown` group at the end of the list. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| You can combine multiple groups by wrapping them in an array; all members will be sorted together using the global or overridden options. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ## Usage | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| <CodeTabs | ||||||||||||||||||||||||||||||||||||||
| code={[ | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| source: dedent` | ||||||||||||||||||||||||||||||||||||||
| // eslint.config.js | ||||||||||||||||||||||||||||||||||||||
| import perfectionist from 'eslint-plugin-perfectionist' | ||||||||||||||||||||||||||||||||||||||
| export default [ | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| plugins: { | ||||||||||||||||||||||||||||||||||||||
| perfectionist, | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| rules: { | ||||||||||||||||||||||||||||||||||||||
| 'perfectionist/sort-regexp': [ | ||||||||||||||||||||||||||||||||||||||
| 'error', | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| type: 'alphabetical', | ||||||||||||||||||||||||||||||||||||||
| order: 'asc', | ||||||||||||||||||||||||||||||||||||||
| fallbackSort: { type: 'unsorted' }, | ||||||||||||||||||||||||||||||||||||||
| ignoreCase: true, | ||||||||||||||||||||||||||||||||||||||
| ignoreAlias: false, | ||||||||||||||||||||||||||||||||||||||
| specialCharacters: 'keep', | ||||||||||||||||||||||||||||||||||||||
| locales: 'en-US', | ||||||||||||||||||||||||||||||||||||||
| alphabet: '', | ||||||||||||||||||||||||||||||||||||||
| groups: [], | ||||||||||||||||||||||||||||||||||||||
| customGroups: [], | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| name: 'Flat Config', | ||||||||||||||||||||||||||||||||||||||
| value: 'flat', | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| source: dedent` | ||||||||||||||||||||||||||||||||||||||
| // .eslintrc.js | ||||||||||||||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||||||||||||||
| plugins: [ | ||||||||||||||||||||||||||||||||||||||
| 'perfectionist', | ||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||
| rules: { | ||||||||||||||||||||||||||||||||||||||
| 'perfectionist/sort-regexp': [ | ||||||||||||||||||||||||||||||||||||||
| 'error', | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| type: 'alphabetical', | ||||||||||||||||||||||||||||||||||||||
| order: 'asc', | ||||||||||||||||||||||||||||||||||||||
| fallbackSort: { type: 'unsorted' }, | ||||||||||||||||||||||||||||||||||||||
| ignoreCase: true, | ||||||||||||||||||||||||||||||||||||||
| ignoreAlias: false, | ||||||||||||||||||||||||||||||||||||||
| specialCharacters: 'keep', | ||||||||||||||||||||||||||||||||||||||
| locales: 'en-US', | ||||||||||||||||||||||||||||||||||||||
| alphabet: '', | ||||||||||||||||||||||||||||||||||||||
| groups: [], | ||||||||||||||||||||||||||||||||||||||
| customGroups: [], | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| `, | ||||||||||||||||||||||||||||||||||||||
| name: 'Legacy Config', | ||||||||||||||||||||||||||||||||||||||
| value: 'legacy', | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| ]} | ||||||||||||||||||||||||||||||||||||||
| type="config-type" | ||||||||||||||||||||||||||||||||||||||
| client:load | ||||||||||||||||||||||||||||||||||||||
| lang="tsx" | ||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ## Version | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| This rule was introduced in [v5.0.0](https://github.com/azat-io/eslint-plugin-perfectionist/releases/tag/v5.0.0). | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ## Resources | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| - [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-regexp.ts) | ||||||||||||||||||||||||||||||||||||||
| - [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/rules/sort-regexp.test.ts) | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ | |
| "test:unit": "vitest --run --coverage" | ||
| }, | ||
| "dependencies": { | ||
| "@eslint-community/regexpp": "^4.12.1", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For information:
Just something to keep in mind based on the adoption of the rule. |
||
| "@typescript-eslint/utils": "^8.46.2", | ||
| "natural-orderby": "^5.0.0" | ||
| }, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@azat-io
Honestly, I'm not confident that we can 100% ensure full safety here at this stage. I would even say that regex re-ordering probably has more weird edge cases that any other rules we have (which already have complex edge cases due to dependency ordering).
I expect bugs related to ordering that change the behavior when the rule is launched. It might be OK in 95% of the cases, but I have doubt about the remaining complex cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added additional safeguards in the latest commits:
/vflag guard - skips character class sorting to avoid breaking set operations (&&,--)/(ab|a)/casesThe rule is conservative - it skips sorting when it detects potentially unsafe patterns. That said, I agree 100% safety is hard to guarantee with regex.