-
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
876 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,119 @@ | ||
--- | ||
title: sort-object-keys | ||
--- | ||
|
||
# sort-object-keys | ||
|
||
> Enforce sorted object keys. | ||
## 💡 Examples | ||
|
||
### Natural sorting | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
// Incorrect | ||
let family = { | ||
dad: 'Loid Forger', | ||
mom: 'Yor Forger', | ||
daughter: 'Anya Forger', | ||
} | ||
|
||
// Correct | ||
let family = { | ||
dad: 'Loid Forger', | ||
daughter: 'Anya Forger', | ||
mom: 'Yor Forger', | ||
} | ||
``` | ||
|
||
### Sorting by line length | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
// Incorrect | ||
let family = { | ||
dad: 'Loid Forger', | ||
mom: 'Yor Forger', | ||
daughter: 'Anya Forger', | ||
} | ||
|
||
// Correct | ||
let family = { | ||
daughter: 'Anya Forger', | ||
dad: 'Loid Forger', | ||
mom: 'Yor Forger', | ||
} | ||
``` | ||
|
||
## 🔧 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 | ||
|
||
:::tip | ||
If you use the `sort-keys` rule, you should disable it, as it may conflict with the current rule. | ||
::: | ||
|
||
### Legacy config | ||
|
||
```json | ||
// .eslintrc | ||
{ | ||
"rules": { | ||
"perfectionist/sort-object-keys": [ | ||
"error", | ||
{ | ||
"type": "line-length", | ||
"order": "desc", | ||
"spreadLast": true | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
### Flat config | ||
|
||
```js | ||
// eslint.config.js | ||
import perfectionist from 'eslint-plugin-perfectionist' | ||
|
||
export default [ | ||
{ | ||
plugins: { | ||
perfectionist, | ||
}, | ||
rules: { | ||
'perfectionist/sort-object-keys': [ | ||
'error', | ||
{ | ||
type: 'line-length', | ||
order: 'desc', | ||
spreadLast: true, | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
## 🚀 Version | ||
|
||
Coming soon. | ||
|
||
## 📚 Resources | ||
|
||
- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-object-keys.ts) | ||
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-object-keys.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,123 @@ | ||
import type { TSESTree } from '@typescript-eslint/types' | ||
|
||
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 = 'unexpectedObjectKeysOrder' | ||
|
||
type Options = [ | ||
Partial<{ | ||
order: SortOrder | ||
type: SortType | ||
}>, | ||
] | ||
|
||
export const RULE_NAME = 'sort-object-keys' | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce sorted object keys', | ||
recommended: false, | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
type: { | ||
enum: [SortType.natural, SortType['line-length']], | ||
default: SortType.natural, | ||
}, | ||
order: { | ||
enum: [SortOrder.asc, SortOrder.desc], | ||
default: SortOrder.asc, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpectedObjectKeysOrder: 'Expected "{{second}}" to come before "{{first}}"', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
type: SortType.natural, | ||
order: SortOrder.asc, | ||
}, | ||
], | ||
create: context => ({ | ||
ObjectExpression: node => { | ||
if (node.properties.length > 1) { | ||
let options = complete(context.options.at(0), { | ||
type: SortType.natural, | ||
order: SortOrder.asc, | ||
}) | ||
|
||
let source = context.getSourceCode().text | ||
|
||
let formatProperties = (props: TSESTree.ObjectLiteralElement[]): SortingNode[][] => | ||
props.reduce( | ||
(accumulator: SortingNode[][], prop) => { | ||
if (prop.type === AST_NODE_TYPES.SpreadElement) { | ||
accumulator.push([]) | ||
return accumulator | ||
} | ||
|
||
let name: string | ||
|
||
if (prop.key.type === AST_NODE_TYPES.Identifier) { | ||
;({ name } = prop.key) | ||
} else if (prop.key.type === AST_NODE_TYPES.Literal) { | ||
name = `${prop.key.value}` | ||
} else { | ||
name = source.slice(...prop.key.range) | ||
} | ||
|
||
let value = { | ||
size: rangeToDiff(prop.range), | ||
node: prop, | ||
name, | ||
} | ||
|
||
accumulator.at(-1)!.push(value) | ||
|
||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
|
||
formatProperties(node.properties).forEach(values => { | ||
if (values.length > 1) { | ||
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: 'unexpectedObjectKeysOrder', | ||
data: { | ||
first: first.name, | ||
second: second.name, | ||
}, | ||
node: second.node, | ||
fix: fixer => sortNodes(fixer, source, values, options), | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}, | ||
}), | ||
}) |
Oops, something went wrong.