Skip to content

Commit 20ea295

Browse files
committed
feat: add eslint-plugin-command
1 parent 24d4f14 commit 20ea295

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

README.md

+27-10
Original file line numberDiff line numberDiff line change
@@ -554,23 +554,40 @@ npm i -D @unocss/eslint-plugin
554554

555555
This config also provides some optional plugins/rules for extended usage.
556556

557-
#### `perfectionist` (sorting)
557+
#### `command`
558558

559-
This plugin [`eslint-plugin-perfectionist`](https://github.com/azat-io/eslint-plugin-perfectionist) allows you to sort object keys, imports, etc, with auto-fix.
559+
Powered by [`eslint-plugin-command`](https://github.com/antfu/eslint-plugin-command). It is not a typical rule for linting, but an on-demand micro-codemod tool that triggers by specific comments.
560560

561-
The plugin is installed, but no rules are enabled by default.
561+
For a few triggers, for example:
562562

563-
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).
563+
- `/// to-function` - converts an arrow function to a normal function
564+
- `/// to-arrow` - converts a normal function to an arrow function
565+
- `/// to-for-each` - converts a for-in/for-of loop to `.forEach()`
566+
- `/// to-for-of` - converts a `.forEach()` to a for-of loop
567+
- `/// keep-sorted` - sorts an object/array/interface
568+
- ... etc. - refer to the [documentation](https://github.com/antfu/eslint-plugin-command#built-in-commands)
564569

565-
```js
566-
/* eslint perfectionist/sort-objects: "error" */
567-
const objectWantedToSort = {
568-
a: 2,
569-
b: 1,
570-
c: 3,
570+
You can add the trigger comment one line above the code you want to transform, for example (note the triple slash):
571+
572+
<!-- eslint-skip -->
573+
574+
```ts
575+
/// to-function
576+
const foo = async (msg: string): void => {
577+
console.log(msg)
578+
}
579+
```
580+
581+
Will be transformed to this when you hit save with your editor or run `eslint . --fix`:
582+
583+
```ts
584+
async function foo(msg: string): void {
585+
console.log(msg)
571586
}
572587
```
573588

589+
The command comments are usually one-off and will be removed along with the transformation.
590+
574591
### Type Aware Rules
575592

576593
You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by passing the options object to the `typescript` config:

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"eslint-flat-config-utils": "^0.2.3",
102102
"eslint-merge-processors": "^0.1.0",
103103
"eslint-plugin-antfu": "^2.1.2",
104+
"eslint-plugin-command": "^0.1.2",
104105
"eslint-plugin-eslint-comments": "^3.2.0",
105106
"eslint-plugin-import-x": "^0.5.0",
106107
"eslint-plugin-jsdoc": "^48.2.3",

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configs/command.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import createCommand from 'eslint-plugin-command/config'
2+
import type { TypedFlatConfigItem } from '../types'
3+
4+
export async function command(): Promise<TypedFlatConfigItem[]> {
5+
return [
6+
{
7+
...createCommand(),
8+
name: 'antfu/command/rules',
9+
},
10+
]
11+
}

src/configs/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
export * from './astro'
2+
export * from './command'
13
export * from './comments'
4+
export * from './formatters'
25
export * from './ignores'
36
export * from './imports'
47
export * from './javascript'
@@ -7,17 +10,15 @@ export * from './jsonc'
710
export * from './markdown'
811
export * from './node'
912
export * from './perfectionist'
10-
export * from './formatters'
1113
export * from './react'
14+
export * from './solid'
1215
export * from './sort'
1316
export * from './stylistic'
1417
export * from './svelte'
1518
export * from './test'
19+
export * from './toml'
1620
export * from './typescript'
1721
export * from './unicorn'
1822
export * from './unocss'
1923
export * from './vue'
2024
export * from './yaml'
21-
export * from './toml'
22-
export * from './astro'
23-
export * from './solid'

src/factory.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Linter } from 'eslint'
66
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types'
77
import {
88
astro,
9+
command,
910
comments,
1011
ignores,
1112
imports,
@@ -130,6 +131,7 @@ export function antfu(
130131
stylistic: stylisticOptions,
131132
}),
132133
unicorn(),
134+
command(),
133135

134136
// Optional plugins (installed but not enabled by default)
135137
perfectionist(),

0 commit comments

Comments
 (0)