Skip to content

Commit a7d0ed6

Browse files
committed
docs: mention custom commands
1 parent c485dcb commit a7d0ed6

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,34 @@ const { default: bar, foo } = await import('./foo')
206206

207207
## Custom Commands
208208

209-
It's also possible to define custom commands.
209+
It's also possible to define your custom commands.
210+
211+
```js
212+
// eslint.config.mjs
213+
import command from 'eslint-plugin-command/config'
214+
import { builtinCommands, defineCommand } from 'eslint-plugin-command/commands'
215+
216+
const myCommand = defineCommand({
217+
name: 'my-command',
218+
// RegExp to match the command comment (without leading `//`)
219+
match: /^@my-command$/,
220+
action(context) {
221+
// Do something with the context
222+
},
223+
})
224+
225+
export default [
226+
// ... your other flat config
227+
command({
228+
commands: [
229+
...builtinCommands,
230+
myCommand,
231+
]
232+
}),
233+
]
234+
```
235+
236+
You can refer to [the built-in commands for examples](./src/commands/).
210237

211238
## Sponsors
212239

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
],
5252
"./config": [
5353
"./dist/config.d.ts"
54+
],
55+
"./commands": [
56+
"./dist/commands.d.ts"
57+
],
58+
"./types": [
59+
"./dist/types.d.ts"
5460
]
5561
}
5662
},

src/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export * from './commands/index'
2+
3+
export type { Command } from './types'
4+
export { defineCommand } from './types'

src/commands/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export {
1616
}
1717

1818
// @keep-sorted
19-
export const commands = {
19+
export const builtinCommands = [
2020
keepSorted,
2121
toArrow,
2222
toDynamicImport,
2323
toForEach,
2424
toForOf,
2525
toFunction,
26-
}
26+
]

src/rule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createEslintRule } from './utils'
22
import type { Command, MessageIds, RuleOptions } from './types'
3-
import { commands } from './commands'
3+
import { builtinCommands } from './commands'
44
import { CommandContext } from './context'
55

66
export function createRuleWithCommands(commands: Command[]) {
@@ -43,4 +43,4 @@ export function createRuleWithCommands(commands: Command[]) {
4343
})
4444
}
4545

46-
export default createRuleWithCommands(Object.values(commands))
46+
export default /* @__PURE__ */ createRuleWithCommands(builtinCommands)

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ export type CommandReportErrorCauseDescriptor = {
5454
*/
5555
message: string
5656
}
57+
58+
export function defineCommand(command: Command) {
59+
return command
60+
}

0 commit comments

Comments
 (0)