-
Notifications
You must be signed in to change notification settings - Fork 39
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
1,096 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,146 @@ | ||
--- | ||
title: sort-object-types | ||
--- | ||
|
||
# sort-object-types | ||
|
||
💼 This rule is enabled in the following [configs](/configs/): `recommended-alphabetical`, `recommended-line-length`, `recommended-natural`. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## 📖 Rule Details | ||
|
||
Enforce sorted object types. | ||
|
||
This rule standardizes the order of members of an object type in a TypeScript. The order in which the members are defined within an object type does not affect the type system or the behavior of the code. | ||
|
||
## 💡 Examples | ||
|
||
### Alphabetical and Natural Sorting | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
// Incorrect | ||
type User = { | ||
name: string | ||
email: string | ||
role: Role | ||
isAdmin: boolean | ||
} | ||
|
||
// Correct | ||
type User = { | ||
email: string | ||
isAdmin: boolean | ||
name: string | ||
role: Role | ||
} | ||
``` | ||
### Sorting by Line Length | ||
<!-- prettier-ignore --> | ||
```ts | ||
// Incorrect | ||
type User = { | ||
name: string | ||
email: string | ||
role: Role | ||
isAdmin: boolean | ||
} | ||
|
||
// Correct | ||
type User = { | ||
isAdmin: boolean | ||
email: string | ||
name: string | ||
role: Role | ||
} | ||
``` | ||
## 🔧 Options | ||
This rule accepts an options object with the following properties: | ||
```ts | ||
interface Options { | ||
type?: 'alphabetical' | 'natural' | 'natural' | ||
order?: 'asc' | 'desc' | ||
'ignore-case'?: boolean | ||
} | ||
``` | ||
|
||
### type | ||
|
||
<sub>(default: `'alphabetical'`)</sub> | ||
|
||
- `alphabetical` - sort alphabetically. | ||
- `natural` - sort in natural order. | ||
- `line-length` - sort by code line length. | ||
|
||
### order | ||
|
||
<sub>(default: `'asc'`)</sub> | ||
|
||
- `asc` - enforce properties to be in ascending order. | ||
- `desc` - enforce properties to be in descending order. | ||
|
||
### ignore-case | ||
|
||
<sub>(default: `false`)</sub> | ||
|
||
Only affects alphabetical and natural sorting. When `true` the rule ignores the case-sensitivity of the order. | ||
|
||
## ⚙️ Usage | ||
|
||
### Legacy Config | ||
|
||
```json | ||
// .eslintrc | ||
{ | ||
"rules": { | ||
"perfectionist/sort-object-types": [ | ||
"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-object-types': [ | ||
'error', | ||
{ | ||
type: 'line-length', | ||
order: 'desc', | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
## 🚀 Version | ||
|
||
Coming soon. | ||
|
||
## 📚 Resources | ||
|
||
- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-object-types.ts) | ||
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-object-types.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,129 @@ | ||
import type { SortingNode } from '../typings' | ||
|
||
import { AST_NODE_TYPES } from '@typescript-eslint/types' | ||
|
||
import { createEslintRule } from '../utils/create-eslint-rule' | ||
import { toSingleLine } from '../utils/to-single-line' | ||
import { rangeToDiff } from '../utils/range-to-diff' | ||
import { SortType, SortOrder } from '../typings' | ||
import { sortNodes } from '../utils/sort-nodes' | ||
import { makeFixes } from '../utils/make-fixes' | ||
import { complete } from '../utils/complete' | ||
import { pairwise } from '../utils/pairwise' | ||
import { compare } from '../utils/compare' | ||
|
||
type MESSAGE_ID = 'unexpectedObjectTypesOrder' | ||
|
||
type Options = [ | ||
Partial<{ | ||
'ignore-case': boolean | ||
order: SortOrder | ||
type: SortType | ||
}>, | ||
] | ||
|
||
export const RULE_NAME = 'sort-object-types' | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce sorted object types', | ||
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, | ||
}, | ||
'ignore-case': { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpectedObjectTypesOrder: | ||
'Expected "{{second}}" to come before "{{first}}"', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
}, | ||
], | ||
create: context => ({ | ||
TSTypeLiteral: node => { | ||
if (node.members.length > 1) { | ||
let options = complete(context.options.at(0), { | ||
type: SortType.alphabetical, | ||
'ignore-case': false, | ||
order: SortOrder.asc, | ||
}) | ||
|
||
let source = context.getSourceCode() | ||
|
||
let nodes: SortingNode[] = node.members.map(member => { | ||
let name: string | ||
|
||
if (member.type === AST_NODE_TYPES.TSPropertySignature) { | ||
if (member.key.type === AST_NODE_TYPES.Identifier) { | ||
;({ name } = member.key) | ||
} else if (member.key.type === AST_NODE_TYPES.Literal) { | ||
name = `${member.key.value}` | ||
} else { | ||
name = source.text.slice( | ||
member.range.at(0), | ||
member.typeAnnotation?.range.at(0), | ||
) | ||
} | ||
} else if (member.type === AST_NODE_TYPES.TSIndexSignature) { | ||
let endIndex: number = | ||
member.typeAnnotation?.range.at(0) ?? member.range.at(1)! | ||
|
||
name = source.text.slice(member.range.at(0), endIndex) | ||
} else { | ||
name = source.text.slice(member.range.at(0), member.range.at(1)) | ||
} | ||
|
||
return { | ||
size: rangeToDiff(member.range), | ||
node: member, | ||
name, | ||
} | ||
}) | ||
|
||
pairwise(nodes, (first, second) => { | ||
if (compare(first, second, options)) { | ||
context.report({ | ||
messageId: 'unexpectedObjectTypesOrder', | ||
data: { | ||
first: toSingleLine(first.name), | ||
second: toSingleLine(second.name), | ||
}, | ||
node: second.node, | ||
fix: fixer => | ||
makeFixes(fixer, nodes, sortNodes(nodes, options), source), | ||
}) | ||
} | ||
}) | ||
} | ||
}, | ||
}), | ||
}) |
Oops, something went wrong.