-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
976 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
title: sort-enums | ||
--- | ||
|
||
# sort-enums | ||
|
||
> Enforce sorted TypeScript enum members. | ||
## 💡 Examples | ||
|
||
### Alphabetical and natural sorting | ||
|
||
```ts | ||
// Incorrect | ||
enum Hinamizawa { | ||
'Sonozaki Shion' = 'Sonozaki Shion', | ||
'Furude Rika' = 'Furude Rika', | ||
'Sonozaki Mion' = 'Sonozaki Mion', | ||
'Ryūgū Rena' = 'Ryūgū Rena', | ||
} | ||
|
||
// Correct | ||
enum Hinamizawa { | ||
'Furude Rika' = 'Furude Rika', | ||
'Ryūgū Rena' = 'Ryūgū Rena', | ||
'Sonozaki Mion' = 'Sonozaki Mion', | ||
'Sonozaki Shion' = 'Sonozaki Shion', | ||
} | ||
``` | ||
|
||
### Sorting by line length | ||
|
||
```ts | ||
// Incorrect | ||
enum Hinamizawa { | ||
'Sonozaki Shion' = 'Sonozaki Shion', | ||
'Furude Rika' = 'Furude Rika', | ||
'Sonozaki Mion' = 'Sonozaki Mion', | ||
'Ryūgū Rena' = 'Ryūgū Rena', | ||
} | ||
|
||
// Correct | ||
enum Hinamizawa { | ||
'Sonozaki Shion' = 'Sonozaki Shion', | ||
'Sonozaki Mion' = 'Sonozaki Mion', | ||
'Furude Rika' = 'Furude Rika', | ||
'Ryūgū Rena' = 'Ryūgū Rena', | ||
} | ||
``` | ||
|
||
## 🔧 Options | ||
|
||
### `type` | ||
|
||
- `enum` (default: `natural`): | ||
- `natural` - sorting, which is similar to alphabetical order. | ||
- `line-length` - sort by code line length. | ||
|
||
### `order` | ||
|
||
- `enum` (default: `asc`): | ||
- `asc` - enforce properties to be in ascending order. | ||
- `desc` - enforce properties to be in descending order. | ||
|
||
## ⚙️ Usage | ||
|
||
### Legacy config | ||
|
||
```json | ||
// .eslintrc | ||
{ | ||
"rules": { | ||
"perfectionist/sort-enums": [ | ||
"error", | ||
{ | ||
"type": "line-length", | ||
"order": "desc" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
### Flat config | ||
|
||
```js | ||
// eslint.config.js | ||
import perfectionist from 'eslint-plugin-perfectionist' | ||
|
||
export default [ | ||
{ | ||
plugins: { | ||
perfectionist, | ||
}, | ||
rules: { | ||
'perfectionist/sort-enums': [ | ||
'error', | ||
{ | ||
type: 'line-length', | ||
order: 'desc', | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
## 🚀 Version | ||
|
||
Coming soon. | ||
|
||
## 📚 Resources | ||
|
||
- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-enums.ts) | ||
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-enums.test.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types' | ||
|
||
import { createEslintRule } from '~/utils/create-eslint-rule' | ||
import { rangeToDiff } from '~/utils/range-to-diff' | ||
import { SortType, SortOrder } from '~/typings' | ||
import { sortNodes } from '~/utils/sort-nodes' | ||
import type { SortingNode } from '~/typings' | ||
import { complete } from '~/utils/complete' | ||
import { compare } from '~/utils/compare' | ||
|
||
type MESSAGE_ID = 'unexpectedEnumsOrder' | ||
|
||
type Options = [ | ||
Partial<{ | ||
order: SortOrder | ||
type: SortType | ||
}>, | ||
] | ||
|
||
export const RULE_NAME = 'sort-enums' | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce sorted TypeScript enums', | ||
recommended: false, | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
type: { | ||
enum: [SortType.alphabetical, SortType.natural, SortType['line-length']], | ||
default: SortType.natural, | ||
}, | ||
order: { | ||
enum: [SortOrder.asc, SortOrder.desc], | ||
default: SortOrder.asc, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpectedEnumsOrder: 'Expected "{{second}}" to come before "{{first}}"', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
}, | ||
], | ||
create: context => ({ | ||
TSEnumDeclaration: node => { | ||
let options = complete(context.options.at(0), { | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
}) | ||
|
||
if (node.members.length > 1) { | ||
let source = context.getSourceCode().text | ||
|
||
let values: SortingNode[] = node.members.map(member => { | ||
let name: string | ||
|
||
if (member.id.type === AST_NODE_TYPES.Literal) { | ||
name = `${member.id.value}` | ||
} else { | ||
name = `${source.slice(...member.id.range)}` | ||
} | ||
|
||
return { | ||
size: rangeToDiff(member.range), | ||
node: member, | ||
name, | ||
} | ||
}) | ||
|
||
for (let i = 1; i < values.length; i++) { | ||
let first = values.at(i - 1)! | ||
let second = values.at(i)! | ||
|
||
if (compare(first, second, options)) { | ||
context.report({ | ||
messageId: 'unexpectedEnumsOrder', | ||
data: { | ||
first: first.name, | ||
second: second.name, | ||
}, | ||
node: second.node, | ||
fix: fixer => sortNodes(fixer, source, values, options), | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
}), | ||
}) |
Oops, something went wrong.