0.0.1 โ€ข Published 3 months ago

@hybrbase/eslint-config v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Shareable ESLint Config Bases

๐Ÿ“„ About

Composable eslint config bases for hybrbase projects and others.

โ†’ Purpose

The purpose of the package is to provide composable and monorepo friendly eslint configurations for JavaScript based projects. It includes features such as performance optimization and the ability to extend pre-defined base configs.

  • Workspace-specific Configuration: This package enables each workspace in a monorepo to have its own configuration.
  • Configurable: You can easily create your workspace ESLint config by composing it from pre-defined bases.
  • Convenient: The use of @rushstack/eslint-patch ensures that you don't have to install plugins for each workspace, giving you peace of mind.
  • Efficient: The performance of the package is improved by enabling plugins based on file naming conventions.

๐Ÿ’ฟ Installation

To install @hybrbase/eslint-config, add it as a dev-dependency to your monorepo workspaces and in your root package.json:

# Install as dev-dependency into monorepo root
$ pnpm add -wD \
  @hybrbase/eslint-config \
  npm-run-all

# Install as dev-dependency per workspace (app or package)
$ pnpm --filter=my-first-app add -D @hybrbase/eslint-config
$ pnpm --filter=my-first-package add -D @hybrbase/eslint-config

Assuming that you have the following structure:

.
โ”œโ”€โ”€ package.json (root)
โ”œโ”€โ”€ apps
โ”‚   โ””โ”€โ”€ my-first-app
โ”‚       โ”œโ”€โ”€ package.json
โ”‚       โ””โ”€โ”€ ... (other app files)
โ””โ”€โ”€ packages
    โ””โ”€โ”€ my-first-package
        โ”œโ”€โ”€ package.json
        โ””โ”€โ”€ ... (other package files)

PS: if you use graphql rules, add the @graphql-eslint/eslint-plugin as well (not done by default as it comes with many transitive deps you might not need)

โ†’ Scripts

Here are some example scripts you can add to your root package.json:

# Runs md, js and secrets linters
$ pnpm pkg set scripts.lint="run-p lint:turbo lint:md lint:html lint:css lint:secrets format:package-json"

# Runs lint command using turbo (affects all */packages) with auto-fix and sorts package.json after
$ pnpm pkg set scripts.format="turbo run format && pnpm format:package-json"

# Runs lint only on *.md files
$ pnpm pkg set scripts.lint:md="markdownlint --fix **/*.md --ignore '**/node_modules/**' --ignore '**/CHANGELOG.md'"

# Sorts package.json in project
$ pnpm pkg set scripts.format:package-json="sort-package-json package.json apps/*/package.json packages/*/package.json"

# Searches only for secrets
$ pnpm pkg set scripts.lint:secrets="secretlint **/*"

# Runs lint command over all packages using turbopack
$ pnpm pkg set scripts.lint:turbo="turbo lint"

You can also manually add commands to your workspaces, located in apps/* and packages/*. For example:

{
  "scripts": {
    "lint": "eslint --ext .ts,.js,.cjs,.mjs --cache --cache-location ../../.cache/eslint/eslint-config.eslintcache",
    "format": "eslint --ext .ts,.tsx,.js,.jsx,.mjs,.cjs,.mts,.cts --fix --cache --cache-location ../../.cache/eslint/eslint-config.eslintcache"
  }
}

๐Ÿ’ป Usage

To create your workspace-specific ESLint configuration, create a file ./apps/my-first-app/.eslintrc.js or ./apps/my-first-app/.eslintrc.cjs that extends any of the existing base configs.

For example, if you have a Next.js application, your .eslintrc.js would look something like this:

// Next line only required if you're using a monorepo
// Workaround for https://github.com/eslint/eslint/issues/3458 (re-export of @rushstack/eslint-patch)
require("@hybrbase/eslint-config/patch/modern-module-resolution");

module.exports = {
  root: true,
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: 'tsconfig.json',
  },
  ignorePatterns: [...getDefaultIgnorePatterns(), '.next', '.out', '/storybook-static'],
  extends: [
    '@hybrbase/eslint-config/typescript',
    '@hybrbase/eslint-config/regexp',
    '@hybrbase/eslint-config/sonar',
    '@hybrbase/eslint-config/jest',
    '@hybrbase/eslint-config/rtl',
    '@hybrbase/eslint-config/react',
    '@hybrbase/eslint-config/react-query,
    '@hybrbase/eslint-config/tailwind',
    '@hybrbase/eslint-config/mdx',
    // '@hybrbase/eslint-config/graphql-schema',
    // '@hybrbase/eslint-config/storybook',
    // '@hybrbase/eslint-config/playwright',

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

    // Post configure the prettier base and run prettier
    // without conflicts thx to eslint-plugin-prettier
    "@hybrbase/eslint-config/prettier-plugin",
    // Alternatively to the above if you're already running prettier
    // we can get a speed up by using on eslint-prettier-config
    // "@hybrbase/eslint-config/prettier-config",
  ],
  rules: {
    // Specific global rules for your app or package
    // https://github.com/vercel/next.js/discussions/16832
    '@next/next/no-img-element': 'off',
    // For the sake of example
    // https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/HEAD/docs/rules/anchor-is-valid.md
    'jsx-a11y/anchor-is-valid': 'off',
    '@typescript-eslint/no-unused-vars': 'off',
  },
  overrides: [
    // Specific file rules for your app or package
    {
      files: ['src/pages/\\_*.{ts,tsx}'],
      rules: {
        'react/display-name': 'off',
      },
    },
    {
      files: ['src/stories/*.ts'],
      rules: {
        '@typescript-eslint/naming-convention': 'off',
      },
    },
  ],
}

Tip:

  • Prettier: @hybrbase/eslint-config/prettier-plugin and @hybrbase/eslint-config/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.

๐Ÿ› ๏ธ Base Configs

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

BaseMatch conventionScope
typescriptallNaming 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*.graphqlEnsure validity of graphql schema files.
storybook*.stories.{ts,tsx,mdx}Potential errors / deprecations in stories.
playwright**/e2e/**/*.test.{js,ts}Keep "recommended" playwright usage.
prettier-pluginallPost 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.

๐Ÿงน Prettier integration

Two ways to work with prettier.

  • @hybrbase/eslint-config/prettier-plugin โ€” eslint will run prettier under the hood (default)
  • @hybrbase/eslint-config/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, named .prettierrc.js in root directory of project and in each package or app.

โ†’ Example Structure

.
โ”œโ”€โ”€ .prettierrc.js (root)
โ”œโ”€โ”€ package.json (root)
โ”œโ”€โ”€ apps
โ”‚   โ””โ”€โ”€ my-first-app
โ”‚       โ”œโ”€โ”€ .prettierrc.js
โ”‚       โ”œโ”€โ”€ package.json
โ”‚       โ””โ”€โ”€ ... (other app files)
โ””โ”€โ”€ packages
    โ””โ”€โ”€ my-first-package
        โ”œโ”€โ”€ .prettierrc.js
        โ”œโ”€โ”€ package.json
        โ””โ”€โ”€ ... (other package files)

โ†’ .prettierrc.js for root of monorepo

// @ts-check

const { prettier } = require('@hybrbase/eslint-config/configs')

/**
 * @type {import('prettier').Config}
 */
module.exports = {
  ...prettier,
  overrides: [
    ...overrides,
    ...[
      {
        files: '*.md',
        options: {
          singleQuote: false,
          quoteProps: 'preserve',
        },
      },
    ],
  ],
}

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

๐Ÿ“ Notes

โ†’ Typescript

Generic typescript project, mostly based on

Type/PluginComment
eslint:recommendedThe basics for code linting.
@typescript-eslint/recommendedThe basics for typescript.
@typescript-eslint/consistent-typeUse TS 3.8+ imports/exports, helps with esbuild
@typescript-eslint/naming-convention
eslint-plugin-importOrder imports

โ†’ Sonarjs

Type/PluginComment
eslint-plugin-sonarjs/recommendedHelp to keep complexity sane

โ†’ React

Type/PluginComment
eslint-plugin-react/recommended
eslint-plugin-react-hooks/recommended
eslint-plugin-jsx-a11y/recommendedHelps to produce accessibility-ready jsx

โ†’ Jest

Type/PluginComment
eslint-plugin-jest/recommendedJest recommended practices.

โ†’ React Testing Library

Type/PluginComment
eslint-plugin-testing-library/recommendedEase when using react-testing-library

โ†’ Regexp

Type/PluginComment
eslint-plugin-regexp/recommendedESLint plugin for finding regex mistakes and style guide violations.

โ†’ MDX

Type/PluginComment
mdx-js/eslint-mdxESLint Parser/Plugin for MDX, helps you lint all ES syntaxes.

โ†’ HTML

Type/PluginComment
html-eslint/recommendedESLint plugin for linting HTML