Skip to content
GitHub

ESLint

MeoCord 4.3 · Shipping · page 42 of 42 · since 4.0.0

Lint the project with MeoCord's ESLint config, add rules of your own, and catch import cycles as you write.

You'll learn

  • Extend MeoCord's ESLint config with your own rules and plugins
  • Catch import cycles before the bot refuses to start over one

meocord/eslint exports a base ESLint config for a MeoCord project. It lints your TypeScript, meocord.config.ts included, with type information from the project's tsconfig files. A new project's eslint.config.ts uses it, and its lint script runs ESLint and both typechecks.

When to use it

Keep it on in every MeoCord project. npm run lint fixes what it can, so CI runs ESLint without --fix, where a problem fails the job, and then the typechecks the script also runs: npx tsc --noEmit && npx tsc --noEmit -p tsconfig.test.json. Replace it only to lint the project with a config you maintain yourself; you then lose the import-cycle warning and the floating-promise check below unless you add them.

Example

config/eslint-config.ts
// eslint.config.ts

import meocordEslint, { typescriptConfig } from 'meocord/eslint'

export default [
  ...meocordEslint,
  {
    ...typescriptConfig,
    rules: {
      ...typescriptConfig.rules,
      // Your own rules, on top of MeoCord's
      'no-console': 'warn',
    },
  },
]

typescriptConfig is MeoCord's own entry in the default export: its parser, plugins and rules for TypeScript, applied after typescript-eslint's recommended and stylistic sets. Spread it, and add your rules over its own.

How it works

The config reads type information from tsconfig.json, tsconfig.test.json and tsconfig.eslint.json, whichever includes the file. A file ESLint reports as not included in any of them needs listing in one: a script at the root, for instance, goes in tsconfig.eslint.json.

Plugins go in the same object, spread over typescriptConfig.plugins, as a new project's config does for eslint-plugin-unused-imports.

The default export skips dist, coverage, build, lib and docs at the root, and vitest.config.ts. To lint code you keep in lib/ or build/, override the ignore with an entry of your own, or move the code under src.

The config reads these files from the folder of the eslint.config.* that lints each file, whatever the config's format and runtime, and wherever ESLint runs from, such as the root of a workspace. To keep them in another folder, set tsconfigRootDir in an entry of your own; typed rules then read them there, while the import-cycle check still reads the ones beside the config.

Adopting it by hand

A project from npx meocord create has every package the config needs. One that adopts meocord/eslint by hand installs them, at least at the versions it works with:

npm install --save-dev "eslint@>=8.57.0" "typescript@>=4.8.4 <6.1" "prettier@>=3.0.0" "typescript-eslint@>=8.0.0" "@typescript-eslint/parser@>=8.0.0" "eslint-plugin-import-x@>=4.13.2" "eslint-plugin-prettier@>=5.0.0" "eslint-config-prettier@>=7.0.0" "eslint-import-resolver-typescript@>=4.0.0" globals
bun add -d "eslint@>=8.57.0" "typescript@>=4.8.4 <6.1" "prettier@>=3.0.0" "typescript-eslint@>=8.0.0" "@typescript-eslint/parser@>=8.0.0" "eslint-plugin-import-x@>=4.13.2" "eslint-plugin-prettier@>=5.0.0" "eslint-config-prettier@>=7.0.0" "eslint-import-resolver-typescript@>=4.0.0" globals
pnpm add -D "eslint@>=8.57.0" "typescript@>=4.8.4 <6.1" "prettier@>=3.0.0" "typescript-eslint@>=8.0.0" "@typescript-eslint/parser@>=8.0.0" "eslint-plugin-import-x@>=4.13.2" "eslint-plugin-prettier@>=5.0.0" "eslint-config-prettier@>=7.0.0" "eslint-import-resolver-typescript@>=4.0.0" globals
yarn add -D "eslint@>=8.57.0" "typescript@>=4.8.4 <6.1" "prettier@>=3.0.0" "typescript-eslint@>=8.0.0" "@typescript-eslint/parser@>=8.0.0" "eslint-plugin-import-x@>=4.13.2" "eslint-plugin-prettier@>=5.0.0" "eslint-config-prettier@>=7.0.0" "eslint-import-resolver-typescript@>=4.0.0" globals

MeoCord marks them as optional peer dependencies of any version, so installing or upgrading meocord changes none of them. npm, pnpm and Yarn leave them out of a production install. Bun's bun install --production still installs them when the bot lists them as development dependencies, as every bot from npx meocord create does. typescript stays below 6.1 until typescript-eslint supports TypeScript 7.

Import cycles

Two classes that import each other can't be injected: whichever file loads second records the other's constructor type before that class exists, and the bot refuses to start. meocord/eslint warns about the cycle with import-x/no-cycle as you write it. Type-only imports are ignored, since they're gone at runtime.

The check follows imports through @src with eslint-import-resolver-typescript, which new projects include. A project without it gets no cycle warning, and the rest of its lint is unchanged; add it to turn the check on:

npm install --save-dev eslint-import-resolver-typescript
bun add -d eslint-import-resolver-typescript
pnpm add -D eslint-import-resolver-typescript
yarn add -D eslint-import-resolver-typescript

Services covers the fix: move what both need into a third service.

Promises nothing waits for

meocord/eslint turns on @typescript-eslint/no-floating-promises. A promise nothing awaits, such as respond(interaction).send() or a database write left without await, rejects outside every handler MeoCord runs, so its error reaches no exception filter and can end the bot. An interceptor's next.handle() left that way runs the code after it before the handler finishes, so that code never sees the handler's result or error. For each one it reports, either:

  • await it, or return it, where the code after it should wait, as an interceptor's next.handle() always should;
  • or write void before it where it's meant to run on its own, and handle its failure with .catch().

Gotchas

  • A warning doesn't fail lint. To hold cycles out of the main branch, run npx eslint --max-warnings=0 in CI. In a new project this also fails on the deprecated APIs it warns about (@typescript-eslint/no-deprecated), so replace them first, or turn that rule off in CI. Passed through npm run lint --, the flag reaches tsc, which rejects it.
  • A file in no tsconfig fails to lint with type information. List it in tsconfig.eslint.json.

Next steps

  • Services: what the bot injects, and how two services share what they both need.
  • Testing: run the specs beside the lint in CI.
  • The CLI: build and start the bot the lint has checked.