JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 356222
  • Score
    100M100P100Q170860F
  • License MIT

Anthony extended ESLint rules

Package Exports

  • eslint-plugin-command
  • eslint-plugin-command/config

Readme

eslint-plugin-command

npm version npm downloads

Support one-off comment-as-command to do code transformation with ESLint.

Use ESLint as a codemod tool, on-demand.

[!IMPORTANT] Experimental, feedbacks are welcome!

Install

npm i eslint-plugin-command -D

In your Flat config:

// eslint.config.mjs
import command from 'eslint-plugin-command/config'

export default [
  // ... your other flat config
  command(),
]

Built-in Commands

to-function

Convert an arrow function to a standard function declaration.

Trigger with /// to-function comment (triple slashes) one line above the arrow function.

Triggers:

  • /// to-function
  • /// to-fn
  • /// 2f
/// to-function
const foo = async (msg: string): void => {
  console.log(msg)
}

Will be converted to (the command comment will also be removed):

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

to-arrow

Convert a standard function declaration to an arrow function.

Triggers:

  • /// to-arrow
  • /// 2a
/// to-arrow
function foo(msg: string): void {
  console.log(msg)
}

Will be converted to:

const foo = (msg: string): void => {
  console.log(msg)
}

keep-sorted

Keep the object keys or array items sorted.

Triggers:

  • /// keep-sorted
  • // @keep-sorted
/// keep-sorted
const obj = {
  b: 2,
  a: 1,
  c: 3,
}

Will be converted to:

/// keep-sorted
const obj = {
  a: 1,
  b: 2,
  c: 3,
}

Different from the other commands, the comment will not be removed after transformation to keep the sorting.

to-for-each

Convert for-of/for-in loop to .forEach().

Triggers:

  • /// to-for-each
  • /// foreach
/// to-for-each
for (const item of items) {
  if (!item)
    continue
  console.log(item)
}

Will be converted to:

items.forEach((item) => {
  if (!item)
    return
  console.log(item)
})

For for-in loop:

/// to-for-each
for (const key in obj) {
  if (!obj[key])
    continue
  console.log(obj[key])
}

Will be converted to:

Object.keys(obj).forEach((key) => {
  if (!obj[key])
    return
  console.log(obj[key])
})

to-for-of

Convert .forEach() to for-of loop.

Triggers:

  • /// to-for-of
  • /// forof
/// to-for-of
items.forEach((item) => {
  if (!item)
    return
  console.log(item)
})

Will be converted to:

for (const item of items) {
  if (!item)
    continue
  console.log(item)
}

Sponsors

License

MIT License © 2023-PRESENT Anthony Fu