Skip to content

Commit

Permalink
feat: add optional sort-keys plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Oct 13, 2023
1 parent 7c19e69 commit f8ae1f6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -242,6 +242,29 @@ When you want to override rules, or disable them inline, you need to update to t
type foo = { bar: 2 }
```

### Optional Rules

This config also provides some optional plugins/rules for extended usages.

#### `sort-keys`

This plugin [`eslint-plugin-sort-keys`](https://github.com/namnm/eslint-plugin-sort-keys) allows you to keep object keys sorted with auto-fix.

It's installed but no rules are enabled by default.

It's recommended to opt-in on each file individually using [configuration comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1).

```js
/* eslint sort-keys/sort-keys-fix: "error" */
const objectWantedToSort = {
a: 2,
b: 1,
c: 3,
}
/* eslint sort-keys/sort-keys-fix: "off" */
```


### Rules Overrides

Certain rules would only be enabled in specific files, for example, `ts/*` rules would only be enabled in `.ts` files and `vue/*` rules would only be enabled in `.vue` files. If you want to override the rules, you need to specify the file extension:
Expand Down
4 changes: 0 additions & 4 deletions eslint.config.js
@@ -1,5 +1,4 @@
// @ts-check
import sortKeys from 'eslint-plugin-sort-keys'
import styleMigrate from '@stylistic/eslint-plugin-migrate'
import antfu from './dist/index.js'

Expand All @@ -14,9 +13,6 @@ export default antfu(
},
{
files: ['src/**/*.ts'],
plugins: {
'sort-keys': sortKeys,
},
rules: {
'sort-keys/sort-keys-fix': 'error',
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,6 +50,7 @@
"eslint-plugin-markdown": "^3.0.1",
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-sort-keys": "^2.3.5",
"eslint-plugin-unicorn": "^48.0.1",
"eslint-plugin-unused-imports": "^3.0.0",
"eslint-plugin-vitest": "^0.3.2",
Expand All @@ -71,7 +72,6 @@
"bumpp": "^9.2.0",
"eslint": "^8.51.0",
"eslint-flat-config-viewer": "^0.1.0",
"eslint-plugin-sort-keys": "^2.3.5",
"esno": "^0.17.0",
"fast-glob": "^3.3.1",
"fs-extra": "^11.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/configs/index.ts
Expand Up @@ -13,3 +13,4 @@ export * from './unicorn'
export * from './vue'
export * from './yaml'
export * from './test'
export * from './sort-keys'
18 changes: 18 additions & 0 deletions src/configs/sort-keys.ts
@@ -0,0 +1,18 @@
import type { ConfigItem } from '../types'
import { pluginSortKeys } from '../plugins'

/**
* Optional sort-keys plugin
*
* @see https://github.com/namnm/eslint-plugin-sort-keys
*/
export function sortKeys(): ConfigItem[] {
return [
{
name: 'antfu:sort-keys',
plugins: {
'sort-keys': pluginSortKeys,
},
},
]
}
4 changes: 4 additions & 0 deletions src/factory.ts
Expand Up @@ -12,6 +12,7 @@ import {
jsonc,
markdown,
node,
sortKeys,
sortPackageJson,
sortTsconfig,
stylistic,
Expand Down Expand Up @@ -90,6 +91,9 @@ export function antfu(options: OptionsConfig & ConfigItem = {}, ...userConfigs:
stylistic: stylisticOptions,
}),
unicorn(),

// Optional plugins (not enabled by default)
sortKeys(),
)

if (enableVue)
Expand Down
1 change: 1 addition & 0 deletions src/plugins.ts
Expand Up @@ -16,6 +16,7 @@ export { default as pluginVue } from 'eslint-plugin-vue'
export * as pluginYaml from 'eslint-plugin-yml'
export { default as pluginNoOnlyTests } from 'eslint-plugin-no-only-tests'
export { default as pluginVitest } from 'eslint-plugin-vitest'
export { default as pluginSortKeys } from 'eslint-plugin-sort-keys'

export * as parserTs from '@typescript-eslint/parser'
export { default as parserVue } from 'vue-eslint-parser'
Expand Down

1 comment on commit f8ae1f6

@KnifeFed
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, there's https://github.com/azat-io/eslint-plugin-perfectionist which is maintained and has more features 🙂

Please sign in to comment.