From 20ea2952727af42a0083d8d26a33f757f6b5d889 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 25 Apr 2024 20:45:55 +0200 Subject: [PATCH] feat: add `eslint-plugin-command` --- README.md | 37 +++++++++++++++++++++++++++---------- package.json | 1 + pnpm-lock.yaml | 12 ++++++++++++ src/configs/command.ts | 11 +++++++++++ src/configs/index.ts | 9 +++++---- src/factory.ts | 2 ++ 6 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/configs/command.ts diff --git a/README.md b/README.md index 122d5c76a2..0672544533 100644 --- a/README.md +++ b/README.md @@ -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): + + + +```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: diff --git a/package.json b/package.json index 96252b55e3..6dfded1542 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 968b319502..82ada8c18d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,9 @@ importers: eslint-plugin-antfu: specifier: ^2.1.2 version: 2.1.2(eslint@9.1.1) + eslint-plugin-command: + specifier: ^0.1.2 + version: 0.1.2(eslint@9.1.1) eslint-plugin-eslint-comments: specifier: ^3.2.0 version: 3.2.0(eslint@9.1.1) @@ -1490,6 +1493,11 @@ packages: peerDependencies: eslint: '>=7.0.0' + eslint-plugin-command@0.1.2: + resolution: {integrity: sha512-LxP9coE/GwLvF4JMFyJxqjyMxOASxl8Y3UPWyjjfaIBz4X0yWGqidX0gbAeVuZ9xkaBNWIWZtlPtLPlN/wckOA==} + peerDependencies: + eslint: '*' + eslint-plugin-es-x@7.5.0: resolution: {integrity: sha512-ODswlDSO0HJDzXU0XvgZ3lF3lS3XAZEossh15Q2UHjwrJggWeBoKqqEsLTZLXl+dh5eOAozG0zRcYtuE35oTuQ==} engines: {node: ^14.18.0 || >=16.0.0} @@ -4472,6 +4480,10 @@ snapshots: - supports-color - typescript + eslint-plugin-command@0.1.2(eslint@9.1.1): + dependencies: + eslint: 9.1.1 + eslint-plugin-es-x@7.5.0(eslint@9.1.1): dependencies: '@eslint-community/eslint-utils': 4.4.0(eslint@9.1.1) diff --git a/src/configs/command.ts b/src/configs/command.ts new file mode 100644 index 0000000000..d42fa44f90 --- /dev/null +++ b/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 { + return [ + { + ...createCommand(), + name: 'antfu/command/rules', + }, + ] +} diff --git a/src/configs/index.ts b/src/configs/index.ts index 1cf4d09a0c..c5a6b5f24d 100644 --- a/src/configs/index.ts +++ b/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' @@ -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' diff --git a/src/factory.ts b/src/factory.ts index a3269f8cfe..9196d8eee9 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -6,6 +6,7 @@ import type { Linter } from 'eslint' import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types' import { astro, + command, comments, ignores, imports, @@ -130,6 +131,7 @@ export function antfu( stylistic: stylisticOptions, }), unicorn(), + command(), // Optional plugins (installed but not enabled by default) perfectionist(),