Skip to content

Latest commit

 

History

History

eslint-config-bases

@your-org/eslint-config-bases

GitHub Workflow Status

About

Example of composable eslint config bases that can be easily shared and fine-tuned by apps and packages that lives in a monorepo.

Features

  • Monorepo friendly: Each workspace can have its own config.
  • Composable: Compose your workspace eslint config from pre-defined bases.
  • Peace of mind: Plugins does not need to be installed per workspaces, thx to @rushstack/eslint-patch.
  • Performance!: Plugins enabled on file conventions patterns to increase perf.

Install

Add the following devDependencies to workspace (apps/packages in monorepo) or main project package.json.

$ yarn add --dev eslint @your-org/eslint-config-bases

PS: To keep the size low, if you use the following plugins:

  • graphql: yarn add --dev @graphql-eslint/eslint-plugin
  • mdx: yarn add --dev eslint-plugin-mdx.
  • tailwind: yarn add --dev eslint-plugin-tailwindcss. In one line
yarn add

Usage

Create an ./apps/my-app/.eslintrc.js or ./apps/my-app/.eslintrc.cjs file that extends any of the existing base configs. For example:

// next line only required if you're using a monorepo
require("@your-org/eslint-config-bases/patch/modern-module-resolution");

module.exports = {
  root: true,
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: "tsconfig.json",
  },
  ignorePatterns: ["**/node_modules", "**/.cache", "build", ".next"],
  extends: [
    "@your-org/eslint-config-bases/typescript",
    "@your-org/eslint-config-bases/sonar",
    "@your-org/eslint-config-bases/regexp",
    "@your-org/eslint-config-bases/react",
    "@your-org/eslint-config-bases/react-query",
    "@your-org/eslint-config-bases/jest",
    "@your-org/eslint-config-bases/rtl",

    // "@your-org/eslint-config-bases/mdx",

    // "@your-org/eslint-config-bases/graphql-schema",
    // "@your-org/eslint-config-bases/storybook",
    // "@your-org/eslint-config-bases/playwright",

    // Add specific rules for your framework if needed.
    // ie:
    // - nextjs: 'next/core-web-vitals',
    // - remix:  '@remix-run/eslint-config',
    // ...

    // Post configure the prettier base and run prettier
    // without conflicts thx to eslint-plugin-prettier
    "@your-org/eslint-config-bases/prettier-plugin",
    // Alternatively to the above if you're already running prettier
    // we can get a speed up by using on eslint-prettier-config
    // "@your-org/eslint-config-bases/prettier-config",
  ],
  rules: {
    // Specific global rules for your app or package
    // Might help is next eslint plugin does not locate pages
    // https://nextjs.org/docs/messages/no-html-link-for-pages#pagesdir
    // '@next/next/no-html-link-for-pages': ['error', `${__dirname}/src/pages`],
  },
  overrides: [
    // Specific file rules for your app or package
  ],
};

Tip:

  • Prettier: @your-org/eslint-config-bases/prettier-plugin and @your-org/eslint-config-bases/prettier-config are mutually exclusives. Choose one. The prettier-config suppose that you run prettier independently. The prettier-plugin will run prettier for you. Easiest the prettier-plugin, fastest prettier-config (this mostly depends if you set up and persist caches as well)
  • Performance: Some rules are known to be slow (ie: import/namespace...). Slowest identified rules are disabled depending on context (ie: *.test.tsx? might not need everything). Depending on project it's possible to disable entirely some slow rules (ie: 'import/namespace': 'off'). A good tip run eslint with the TIMING=1 to identify slow rules.

Bases

You can find the bases in ./src/bases.

Base Match convention Scope
typescript all Naming conventions, consistent imports, import sorting...
sonar *.{js,jsx,ts,tsx} Keep levels of code complexity sane. (excl test and stories)
regexp *.{js,jsx,jsx,tsx} Keep regexp consistent and safer.
react *.{jsx,tsx} Recommendations for react, react-hooks and jsx projects.
react-query **/?(*.)+(test).{js,jsx,ts,tsx} Enforce "recommended" react-query usage.
jest **/?(*.)+(test).{js,jsx,ts,tsx} Catch inconsistencies or error in jest tests.
rtl **/?(*.)+(test).{js,jsx,ts,tsx} Potential errors / deprecations in react-testing-library tests.
graphql-schema *.graphql Ensure validity of graphql schema files.
mdx all Mdx validation
storybook *.stories.{ts,tsx,mdx} Potential errors / deprecations in stories.
playwright **/e2e/**/*.test.{js,ts} Keep "recommended" playwright usage.
prettier-plugin all Post configure eslint for prettier compatibility.

Notes:

  • The order is important. Some bases will disable or tune previously defined rules. For example the react base will tune the naming conventions for function components and increase recommended cognitive complexity. The typescript base will also relax conventions for javascript files.
  • Based on filename conventions some rules are relaxed or disabled to avoid false positives and keep a good level of performance. For example the sonar base won't run on test and storybook files. If you work on different conventions the patterns must be updated.

Cyclic deps

Due to performance considerations the import/no-cycle isn't enabled by default. This rule can prevent subtle and hard to debug bugs. Depending on the project you can enable it either by setting and env variable ESLINT_IMPORT_NO_CYCLE=true yarn lint (will default to import/no-cycle: 2) or by adding it to the extended rules.

Prettier integration

Two ways to work with prettier.

  • @your-org/eslint-config-bases/prettier-plugin - eslint will run prettier under the hood
  • @your-org/eslint-config-bases/prettier-config - eslint will just disable some conflicting rules (so you'll need to run prettier after)

The first method is recommended for simplicity. For best perf use the cache option to run eslint.

Tune the behaviour by creating a config in .prettierrc.js

// @ts-check
const { getPrettierConfig } = require("@your-org/eslint-config-bases/helpers");

/**
 * @type {import('prettier').Config}
 */
module.exports = {
  ...getPrettierConfig(),
  overrides: [
    // whatever you need
  ],
};

Tip: You can tune the provided prettier.base.config for your own needs.

Notes

Typescript

Generic typescript project, mostly based on

Type/Plugin Comment
eslint:recommended The basics for code linting.
@typescript-eslint/recommended The basics for typescript.
@typescript-eslint/consistent-type Use TS 3.8+ imports/exports, helps with esbuild
@typescript-eslint/naming-convention
eslint-plugin-import Order imports

Sonarjs

Type/Plugin Comment
eslint-plugin-sonarjs/recommended Help to keep complexity sane

React

Type/Plugin Comment
eslint-plugin-react/recommended
eslint-plugin-react-hooks/recommended
eslint-plugin-jsx-a11y/recommended Helps to produce accessibility-ready jsx

Jest

Type/Plugin Comment
eslint-plugin-jest/recommended Jest recommended practices.

React Testing Library

Type/Plugin Comment
eslint-plugin-testing-library/recommended Ease when using react-testing-library

Regexp

Type/Plugin Comment
eslint-plugin-regexp/recommended

Mdx

To tune the behaviour, you can add setting in the top level config

module.exports = {
  settings: {
    "mdx/code-blocks": true,
    // optional, if you want to disable language mapper, set it to `false`
    // if you want to override the default language mapper inside, you can provide your own
    "mdx/language-mapper": {},
  },
};

Etc

...