-
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
6 changed files
with
803 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--- | ||
title: sort-exports | ||
description: ESLint Plugin Perfectionist rule which enforce sorted exports | ||
--- | ||
|
||
# sort-exports | ||
|
||
💼 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 exports. | ||
|
||
Sorting exports alphabetically or in a consistent order can enhance the readability and maintainability of your code. When exports are sorted, it becomes simpler to identify any missing or incorrect exports. | ||
|
||
## 💡 Examples | ||
|
||
::: code-group | ||
|
||
<!-- prettier-ignore --> | ||
```js [Alphabetical and Natural Sorting] | ||
// ❌ Incorrect | ||
export { readPackageJson } from './read-package-json' | ||
export { loadDotEnv } from './load-dot-env' | ||
export { getGitBranch } from './get-git-branch' | ||
|
||
// ✅ Correct | ||
export { getGitBranch } from './get-git-branch' | ||
export { loadDotEnv } from './load-dot-env' | ||
export { readPackageJson } from './read-package-json' | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```js [Sorting by Line Length] | ||
// ❌ Incorrect | ||
export { readPackageJson } from './read-package-json' | ||
export { loadDotEnv } from './load-dot-env' | ||
export { getGitBranch } from './get-git-branch' | ||
|
||
// ✅ Correct | ||
export { readPackageJson } from './read-package-json' | ||
export { getGitBranch } from './get-git-branch' | ||
export { loadDotEnv } from './load-dot-env' | ||
``` | ||
|
||
::: | ||
|
||
## 🔧 Options | ||
|
||
This rule accepts an options object with the following properties: | ||
|
||
```ts | ||
interface Options { | ||
type?: 'alphabetical' | 'natural' | 'line-length' | ||
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 | ||
|
||
::: code-group | ||
|
||
```json [Legacy Config] | ||
// .eslintrc | ||
{ | ||
"plugins": ["perfectionist"], | ||
"rules": { | ||
"perfectionist/sort-exports": [ | ||
"error", | ||
{ | ||
"type": "line-length", | ||
"order": "desc" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
```js [Flat Config] | ||
// eslint.config.js | ||
import perfectionist from 'eslint-plugin-perfectionist' | ||
|
||
export default [ | ||
{ | ||
plugins: { | ||
perfectionist, | ||
}, | ||
rules: { | ||
'perfectionist/sort-exports': [ | ||
'error', | ||
{ | ||
type: 'line-length', | ||
order: 'desc', | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
::: | ||
|
||
## 🚀 Version | ||
|
||
Coming soon. | ||
|
||
## 📚 Resources | ||
|
||
- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-exports.ts) | ||
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-exports.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,128 @@ | ||
import type { TSESTree } from '@typescript-eslint/types' | ||
|
||
import { AST_NODE_TYPES } from '@typescript-eslint/types' | ||
|
||
import type { SortingNode } from '../typings' | ||
|
||
import { createEslintRule } from '../utils/create-eslint-rule' | ||
import { rangeToDiff } from '../utils/range-to-diff' | ||
import { SortOrder, SortType } 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 = 'unexpectedExportsOrder' | ||
|
||
type Options = [ | ||
Partial<{ | ||
'ignore-case': boolean | ||
order: SortOrder | ||
type: SortType | ||
}>, | ||
] | ||
|
||
export const RULE_NAME = 'sort-exports' | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce sorted exports', | ||
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: { | ||
unexpectedExportsOrder: 'Expected "{{right}}" to come before "{{left}}"', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
}, | ||
], | ||
create: context => { | ||
let options = complete(context.options.at(0), { | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
'ignore-case': false, | ||
}) | ||
|
||
let parts: SortingNode[][] = [[]] | ||
|
||
let registerNode = ( | ||
node: | ||
| TSESTree.ExportNamedDeclarationWithSource | ||
| TSESTree.ExportAllDeclaration, | ||
) => { | ||
if ( | ||
node.type === AST_NODE_TYPES.ExportAllDeclaration && | ||
node.exported === null | ||
) { | ||
parts.push([]) | ||
} else { | ||
parts.at(-1)!.push({ | ||
size: rangeToDiff(node.range), | ||
name: node.source.value, | ||
node, | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
ExportAllDeclaration: registerNode, | ||
ExportNamedDeclaration: node => { | ||
if (node.source !== null) { | ||
registerNode(node) | ||
} | ||
}, | ||
'Program:exit': () => { | ||
let source = context.getSourceCode() | ||
|
||
parts.forEach(nodes => { | ||
pairwise(nodes, (left, right) => { | ||
if (compare(left, right, options)) { | ||
context.report({ | ||
messageId: 'unexpectedExportsOrder', | ||
data: { | ||
left: left.name, | ||
right: right.name, | ||
}, | ||
node: right.node, | ||
fix: fixer => | ||
makeFixes(fixer, nodes, sortNodes(nodes, options), source), | ||
}) | ||
} | ||
}) | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
Oops, something went wrong.