|
1 | 1 | # @aboutbits/eslint-config |
2 | 2 |
|
3 | | -[](https://badge.fury.io/js/%40aboutbits%2Feslint-config) |
4 | | -[](https://github.com/aboutbits/eslint-config/blob/main/license.md) |
| 3 | +AboutBits' [ESLint](https://eslint.org/) config presets. |
5 | 4 |
|
6 | | -AboutBit's [ESLint](https://eslint.org/) config presets |
| 5 | +This package targets **ESLint 9 (flat config)** and ships a set of composable presets for our TypeScript, React, Next.js, Tailwind CSS and related projects. All plugins are bundled as direct dependencies, so you only need to install ESLint itself and a few peer tools. |
7 | 6 |
|
8 | 7 | ## Table of content |
9 | 8 |
|
10 | | -- [Usage](#usage) |
11 | | - - [TypeScript](#typescript) |
12 | | - - [TypeScript + React](#typescript--react) |
13 | | - - [TypeScript + Next.js](#typescript--nextjs) |
14 | | - - [FormatJS](#formatjs) |
| 9 | +- [Requirements](#requirements) |
| 10 | +- [Installation](#installation) |
| 11 | +- [Configuration](#configuration) |
| 12 | + - [Presets](#presets) |
| 13 | + - [Basic setup](#basic-setup) |
15 | 14 | - [Combining presets](#combining-presets) |
16 | | - - [Overriding Rules](#overriding-rules) |
| 15 | + - [Ignoring files](#ignoring-files) |
| 16 | + - [Overriding rules](#overriding-rules) |
| 17 | +- [Scripts](#scripts) |
| 18 | +- [Preset-specific notes](#preset-specific-notes) |
| 19 | + - [Prettier](#prettier) |
| 20 | + - [Tailwind CSS](#tailwind-css) |
| 21 | + - [Next.js](#nextjs) |
| 22 | + - [Storybook](#storybook) |
| 23 | + - [FormatJS](#formatjs) |
17 | 24 | - [Build & Publish](#build--publish) |
18 | | -- [About](#about) |
| 25 | +- [Information](#information) |
| 26 | + |
| 27 | +## Requirements |
19 | 28 |
|
20 | | -## Usage |
| 29 | +- **ESLint** `^9` (flat config, i.e. an `eslint.config.js` file) |
21 | 30 |
|
22 | | -Install the package: |
| 31 | +## Installation |
| 32 | + |
| 33 | +Install the config together with its required peer dependencies: |
23 | 34 |
|
24 | 35 | ```sh |
25 | | -npm i -D @aboutbits/eslint-config |
| 36 | +npm i -D @aboutbits/eslint-config eslint prettier typescript |
26 | 37 | ``` |
27 | 38 |
|
28 | | -We recommend linting by running `eslint` without `--ext` option. What files are to be linted should be specified inside the ESLint config and the TypeScript config. |
29 | | -All files included (or not ignored) by the ESLint config (`.eslintrc.json`) must be included by the TypeScript config (`tsconfig.json`). |
30 | | -For example, if this is your `.eslintrc.json`: |
| 39 | +Unlike the previous (ESLint 8) version, you no longer need to install `@typescript-eslint/*`, `eslint-plugin-import`, `eslint-plugin-react` and the rest by hand — they are bundled with this package. |
31 | 40 |
|
32 | | -```jsonc |
33 | | -{ |
34 | | - // ...remaining config |
35 | | - "ignorePatterns": ["node_modules", "dist"] |
36 | | -} |
37 | | -``` |
| 41 | +Some presets rely on additional peer dependencies that you install only when you use them: |
38 | 42 |
|
39 | | -You may include the following files inside your `tsconfig.json`: |
| 43 | +| Preset | Additional peer dependency | |
| 44 | +| ----------- | --------------------------------------------------- | |
| 45 | +| `next` | `@next/eslint-plugin-next` (`^14 \|\| ^15 \|\| ^16`) | |
| 46 | +| `storybook` | `eslint-plugin-storybook` (`^10`) | |
40 | 47 |
|
41 | | -```jsonc |
42 | | -{ |
43 | | - // ...remaining config |
44 | | - "exclude": ["node_modules", "dist"], |
45 | | - "include": ["**/*.ts", "**/*.tsx"] |
46 | | -} |
47 | | -``` |
| 48 | +We also recommend using our shared Prettier config — see [Prettier](#prettier). |
48 | 49 |
|
49 | | -Should you have files that you want to be linted, but not inside your `tsconfig.json`, you can create a `tsconfig.eslint.json` file. |
50 | | -Then change `.eslintrc.json` to be: |
| 50 | +## Configuration |
51 | 51 |
|
52 | | -```jsonc |
53 | | -{ |
54 | | - // ...omitted |
55 | | - "parserOptions": { |
56 | | - "project": "./tsconfig.eslint.json" |
57 | | - } |
58 | | - // ...omitted |
59 | | -} |
60 | | -``` |
| 52 | +### Presets |
61 | 53 |
|
62 | | -### TypeScript |
| 54 | +Start from the base preset and add optional presets as needed. Each preset is imported from `@aboutbits/eslint-config/configs/<name>`. |
63 | 55 |
|
64 | | -Install the required packages, assuming that you have TypeScript already installed: |
| 56 | +**Base** |
65 | 57 |
|
66 | | -```sh |
67 | | -npm i -D eslint prettier @typescript-eslint/eslint-plugin eslint-plugin-prettier eslint-plugin-import eslint-plugin-unused-imports@^3 |
68 | | -``` |
| 58 | +- **`configs/ts`** — Base configuration for all TypeScript projects. Enables type-aware linting, import ordering, `unused-imports`, Prettier integration and JSON/JSONC linting. |
69 | 59 |
|
70 | | -`.eslintrc.json` |
| 60 | +**Optional** |
71 | 61 |
|
72 | | -```json |
73 | | -{ |
74 | | - "extends": "@aboutbits/eslint-config/ts", |
75 | | - "parserOptions": { |
76 | | - "project": true |
77 | | - } |
78 | | -} |
| 62 | +- **`configs/react`** — Rules for React projects (React, React Hooks, JSX a11y). |
| 63 | +- **`configs/next`** — Rules for Next.js projects (Core Web Vitals). |
| 64 | +- **`configs/tailwind`** — Rules for Tailwind CSS (via `eslint-plugin-better-tailwindcss`). |
| 65 | +- **`configs/formatjs`** — Rules for FormatJS / `react-intl` usage and translation files. |
| 66 | +- **`configs/jest`** — Rules for Jest tests. |
| 67 | +- **`configs/storybook`** — Rules for Storybook stories and `.mdx` docs. |
| 68 | + |
| 69 | +### Basic setup |
| 70 | + |
| 71 | +Create an `eslint.config.js` file in the root of your project: |
| 72 | + |
| 73 | +```js |
| 74 | +import ts from '@aboutbits/eslint-config/configs/ts' |
| 75 | + |
| 76 | +export default [...ts] |
79 | 77 | ``` |
80 | 78 |
|
81 | | -### TypeScript + React |
| 79 | +> Each preset is an array of flat-config objects, so spread it with `...`. |
| 80 | +> Type-aware linting is enabled automatically (via the TypeScript project service) — you do **not** need to set `parserOptions.project`. |
| 81 | +
|
| 82 | +### Combining presets |
82 | 83 |
|
83 | | -Install the required packages, assuming that you have TypeScript already installed: |
| 84 | +Add optional presets after the base preset. A typical Next.js + Tailwind project looks like this: |
84 | 85 |
|
85 | | -```sh |
86 | | -npm i -D eslint prettier @typescript-eslint/eslint-plugin eslint-plugin-prettier eslint-plugin-import eslint-plugin-unused-imports@^3 eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-better-tailwindcss |
| 86 | +```js |
| 87 | +import ts from '@aboutbits/eslint-config/configs/ts' |
| 88 | +import react from '@aboutbits/eslint-config/configs/react' |
| 89 | +import next from '@aboutbits/eslint-config/configs/next' |
| 90 | +import tailwind from '@aboutbits/eslint-config/configs/tailwind' |
| 91 | +import formatjs from '@aboutbits/eslint-config/configs/formatjs' |
| 92 | + |
| 93 | +export default [...ts, ...react, ...next, ...tailwind, ...formatjs] |
87 | 94 | ``` |
88 | 95 |
|
89 | | -`.eslintrc.json` |
| 96 | +### Ignoring files |
90 | 97 |
|
91 | | -```json |
92 | | -{ |
93 | | - "extends": "@aboutbits/eslint-config/ts-react", |
94 | | - "parserOptions": { |
95 | | - "project": true |
96 | | - } |
97 | | -} |
| 98 | +Add an object with an `ignores` key. Note that in flat config an object containing **only** `ignores` applies globally: |
| 99 | + |
| 100 | +```js |
| 101 | +import ts from '@aboutbits/eslint-config/configs/ts' |
| 102 | + |
| 103 | +export default [...ts, { ignores: ['node_modules', 'dist', '.next'] }] |
98 | 104 | ``` |
99 | 105 |
|
100 | | -### TypeScript + Next.js |
| 106 | +### Overriding rules |
101 | 107 |
|
102 | | -Install the required packages, assuming that you have TypeScript already installed: |
| 108 | +Append your own flat-config object after the presets: |
103 | 109 |
|
104 | | -```sh |
105 | | -npm i -D eslint prettier @typescript-eslint/eslint-plugin eslint-plugin-prettier eslint-plugin-import eslint-plugin-unused-imports@^3 eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-better-tailwindcss @next/eslint-plugin-next |
| 110 | +```js |
| 111 | +import ts from '@aboutbits/eslint-config/configs/ts' |
| 112 | + |
| 113 | +export default [ |
| 114 | + ...ts, |
| 115 | + { |
| 116 | + files: ['**/*.{js,mjs,cjs,jsx}'], |
| 117 | + rules: { |
| 118 | + // your overrides... |
| 119 | + 'no-console': 'off', |
| 120 | + }, |
| 121 | + }, |
| 122 | +] |
106 | 123 | ``` |
107 | 124 |
|
108 | | -`.eslintrc.json` |
| 125 | +## Scripts |
| 126 | + |
| 127 | +Add the following scripts to your `package.json`: |
109 | 128 |
|
110 | 129 | ```json |
111 | 130 | { |
112 | | - "extends": "@aboutbits/eslint-config/ts-next", |
113 | | - "parserOptions": { |
114 | | - "project": true |
| 131 | + "scripts": { |
| 132 | + "lint": "eslint --cache .", |
| 133 | + "lint:fix": "npm run lint -- --fix" |
115 | 134 | } |
116 | 135 | } |
117 | 136 | ``` |
118 | 137 |
|
119 | | -### FormatJS |
120 | | - |
121 | | -Install the required packages, assuming that you have TypeScript already installed: |
| 138 | +Then run: |
122 | 139 |
|
123 | 140 | ```sh |
124 | | -npm i -D eslint eslint-plugin-formatjs |
| 141 | +npm run lint # Check for issues |
| 142 | +npm run lint:fix # Fix issues automatically |
125 | 143 | ``` |
126 | 144 |
|
127 | | -`.eslintrc.json` |
| 145 | +## Preset-specific notes |
| 146 | + |
| 147 | +### Prettier |
| 148 | + |
| 149 | +The `ts` preset integrates Prettier through `eslint-plugin-prettier`, so formatting problems are reported (and auto-fixed) by ESLint. You still need a Prettier configuration. We recommend our shared config: |
| 150 | + |
| 151 | +```sh |
| 152 | +npm i -D @aboutbits/prettier-config |
| 153 | +``` |
128 | 154 |
|
129 | 155 | ```json |
130 | 156 | { |
131 | | - "extends": "@aboutbits/eslint-config/formatjs", |
132 | | - "parserOptions": { |
133 | | - "project": true |
134 | | - } |
| 157 | + "prettier": "@aboutbits/prettier-config" |
135 | 158 | } |
136 | 159 | ``` |
137 | 160 |
|
138 | | -### Combining presets |
| 161 | +### Tailwind CSS |
139 | 162 |
|
140 | | -Most presets are mutually exclusive, which means that you should not combine them. |
141 | | -However, you may combine the preset `formatjs` with any other preset you like. |
142 | | -The following shows how to combine it with the preset `ts-next`: |
| 163 | +The `tailwind` preset is preconfigured to recognise classes in `className` props as well as `classNames`/`*ClassName(s)` helpers. It supports both Tailwind 3 and 4, but you must tell the plugin where your Tailwind setup lives. |
143 | 164 |
|
144 | | -Install the required packages, assuming that you have TypeScript already installed: |
| 165 | +**Tailwind 4** — point to your CSS entry point: |
145 | 166 |
|
146 | | -```sh |
147 | | -npm i -D eslint prettier @typescript-eslint/eslint-plugin eslint-plugin-prettier eslint-plugin-import eslint-plugin-unused-imports@^3 eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-plugin-better-tailwindcss @next/eslint-plugin-next eslint-plugin-formatjs |
| 167 | +```js |
| 168 | +import tailwind from '@aboutbits/eslint-config/configs/tailwind' |
| 169 | + |
| 170 | +export default [ |
| 171 | + ...tailwind, |
| 172 | + { |
| 173 | + settings: { |
| 174 | + 'better-tailwindcss': { |
| 175 | + entryPoint: 'src/app.css', |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | +] |
148 | 180 | ``` |
149 | 181 |
|
150 | | -`.eslintrc.json` |
| 182 | +**Tailwind 3** — point to your config file: |
151 | 183 |
|
152 | | -```json |
153 | | -{ |
154 | | - "extends": [ |
155 | | - "@aboutbits/eslint-config/ts-next", |
156 | | - "@aboutbits/eslint-config/formatjs" |
157 | | - ], |
158 | | - "parserOptions": { |
159 | | - "project": true |
160 | | - } |
161 | | -} |
| 184 | +```js |
| 185 | +import tailwind from '@aboutbits/eslint-config/configs/tailwind' |
| 186 | + |
| 187 | +export default [ |
| 188 | + ...tailwind, |
| 189 | + { |
| 190 | + settings: { |
| 191 | + 'better-tailwindcss': { |
| 192 | + tailwindConfig: 'tailwind.config.js', |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | +] |
162 | 197 | ``` |
163 | 198 |
|
164 | | -### Overriding rules |
| 199 | +### Next.js |
165 | 200 |
|
166 | | -`.eslintrc.json` |
| 201 | +Install `@next/eslint-plugin-next` as a dev dependency, matching your installed Next.js version: |
167 | 202 |
|
168 | | -```jsonc |
169 | | -{ |
170 | | - "extends": "@aboutbits/eslint-config/ts", |
171 | | - "parserOptions": { |
172 | | - "project": true |
173 | | - }, |
174 | | - "rules": { |
175 | | - // your rules... |
176 | | - } |
177 | | -} |
| 203 | +```sh |
| 204 | +npm i -D @next/eslint-plugin-next |
178 | 205 | ``` |
179 | 206 |
|
| 207 | +### Storybook |
| 208 | + |
| 209 | +Install `eslint-plugin-storybook` as a dev dependency, matching your installed Storybook version: |
| 210 | + |
| 211 | +```sh |
| 212 | +npm i -D eslint-plugin-storybook |
| 213 | +``` |
| 214 | + |
| 215 | +### FormatJS |
| 216 | + |
| 217 | +The `formatjs` preset enforces our message conventions and additionally sorts the keys of translation files under any `translations/` directory (`**/translations/*.json`). |
| 218 | + |
180 | 219 | ## Build & Publish |
181 | 220 |
|
182 | 221 | To build and publish the package, visit the GitHub Actions page of the repository. |
183 | 222 |
|
184 | 223 | You can choose between two workflows: |
| 224 | + |
185 | 225 | - `Release Package` to publish a new version of the package. |
186 | 226 | - `Pre-Release Package` to publish a new pre-release version of the package. |
187 | 227 |
|
|
0 commit comments