Skip to content

Commit

Permalink
feat: add eslint-plugin-command
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 25, 2024
1 parent 24d4f14 commit 20ea295
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
37 changes: 27 additions & 10 deletions README.md
Expand Up @@ -554,23 +554,40 @@ npm i -D @unocss/eslint-plugin

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

#### `perfectionist` (sorting)
#### `command`

This plugin [`eslint-plugin-perfectionist`](https://github.com/azat-io/eslint-plugin-perfectionist) allows you to sort object keys, imports, etc, with auto-fix.
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.

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

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

```js
/* eslint perfectionist/sort-objects: "error" */
const objectWantedToSort = {
a: 2,
b: 1,
c: 3,
You can add the trigger comment one line above the code you want to transform, for example (note the triple slash):

<!-- eslint-skip -->

```ts
/// to-function
const foo = async (msg: string): void => {
console.log(msg)
}
```

Will be transformed to this when you hit save with your editor or run `eslint . --fix`:

```ts
async function foo(msg: string): void {
console.log(msg)
}
```

The command comments are usually one-off and will be removed along with the transformation.

### Type Aware Rules

You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by passing the options object to the `typescript` config:
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -101,6 +101,7 @@
"eslint-flat-config-utils": "^0.2.3",
"eslint-merge-processors": "^0.1.0",
"eslint-plugin-antfu": "^2.1.2",
"eslint-plugin-command": "^0.1.2",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import-x": "^0.5.0",
"eslint-plugin-jsdoc": "^48.2.3",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/configs/command.ts
@@ -0,0 +1,11 @@
import createCommand from 'eslint-plugin-command/config'
import type { TypedFlatConfigItem } from '../types'

export async function command(): Promise<TypedFlatConfigItem[]> {
return [
{
...createCommand(),
name: 'antfu/command/rules',
},
]
}
9 changes: 5 additions & 4 deletions src/configs/index.ts
@@ -1,4 +1,7 @@
export * from './astro'
export * from './command'
export * from './comments'
export * from './formatters'
export * from './ignores'
export * from './imports'
export * from './javascript'
Expand All @@ -7,17 +10,15 @@ export * from './jsonc'
export * from './markdown'
export * from './node'
export * from './perfectionist'
export * from './formatters'
export * from './react'
export * from './solid'
export * from './sort'
export * from './stylistic'
export * from './svelte'
export * from './test'
export * from './toml'
export * from './typescript'
export * from './unicorn'
export * from './unocss'
export * from './vue'
export * from './yaml'
export * from './toml'
export * from './astro'
export * from './solid'
2 changes: 2 additions & 0 deletions src/factory.ts
Expand Up @@ -6,6 +6,7 @@ import type { Linter } from 'eslint'
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types'
import {
astro,
command,
comments,
ignores,
imports,
Expand Down Expand Up @@ -130,6 +131,7 @@ export function antfu(
stylistic: stylisticOptions,
}),
unicorn(),
command(),

// Optional plugins (installed but not enabled by default)
perfectionist(),
Expand Down

0 comments on commit 20ea295

Please sign in to comment.