@hybrbase/eslint-config
v0.0.1
Published
A shareable eslint base configuration for projects.
Downloads
17
Maintainers
Keywords
Readme
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. Theprettier-config
suppose that you run prettier independently. Theprettier-plugin
will run prettier for you. Easiest theprettier-plugin
, fastestprettier-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 theTIMING=1
to identify slow rules.
🛠️ Base Configs
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. |
| 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.
🧹 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/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 | ESLint plugin for finding regex mistakes and style guide violations. |
→ MDX
| Type/Plugin | Comment | | :----------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------- | | mdx-js/eslint-mdx | ESLint Parser/Plugin for MDX, helps you lint all ES syntaxes. |
→ HTML
| Type/Plugin | Comment | | :----------------------------------------------------------------- | :----------------------------- | | html-eslint/recommended | ESLint plugin for linting HTML |