|
1 | | -# Eslint + Prettier config for React (Next.js) |
| 1 | +# ESLint + Prettier Config for React (Next.js) |
| 2 | + |
| 3 | +`@kitani/eslint-config` |
| 4 | + |
| 5 | +> Shareable config for [ESLint](https://eslint.org/) and [Prettier](https://prettier.io/), aimed primarily to be used in [Next.js](https://nextjs.org) React projects. |
| 6 | +
|
| 7 | +## Overview |
| 8 | + |
| 9 | +This configuration extends [airbnb](https://www.npmjs.com/package/eslint-config-airbnb) ESLint config, with [airbnb/hooks](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb#eslint-config-airbnbhooks) enabled, and Prettier integration via the ESLint [plugin](https://github.com/prettier/eslint-plugin-prettier). Additionally, a few default rules are overriden to provide a more relaxed development experience in Next.js applications out of the box. |
| 10 | + |
| 11 | +The goal of this configuration is to get code linting and formatting up and running as quickly as possible in a modern development environment, without sacrificing cleanliness and readability, and having to configure ESLint + Prettier from scratch every time. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +To install the package, run: |
| 16 | + |
| 17 | +```shell |
| 18 | +$ npm install @kitani/eslint-config |
| 19 | +``` |
| 20 | + |
| 21 | +This will install the shared config, as well as its peer dependencies: |
| 22 | + |
| 23 | +- eslint |
| 24 | +- eslint-config-airbnb |
| 25 | +- eslint-config-prettier |
| 26 | +- eslint-import-resolver-alias |
| 27 | +- eslint-plugin-import |
| 28 | +- eslint-plugin-jsx-a11y |
| 29 | +- eslint-plugin-prettier |
| 30 | +- eslint-plugin-react |
| 31 | +- eslint-plugin-react-hooks |
| 32 | +- prettier |
| 33 | + |
| 34 | +**NOTE:** if you are on an older version of `npm` (`<7.0.0`), you will need to install these manually: |
| 35 | + |
| 36 | +```shell |
| 37 | +$ npx install-peerdeps -D @kitani/eslint-config |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +To start using this shared config, add `@kitani/eslint-config` (or just `@kitani`) to either your `package.json`: |
| 43 | + |
| 44 | +```jsx |
| 45 | +// package.json |
| 46 | +{ |
| 47 | + "eslintConfig": { |
| 48 | + "extends": ["@kitani"] |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +or the `.eslintrc` file: |
| 54 | + |
| 55 | +```jsx |
| 56 | +// .eslintrc |
| 57 | +{ |
| 58 | + "extends": ["@kitani"] |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Import Alias |
| 63 | + |
| 64 | +This config provides a default import alias resolver for `eslint-plugin-import` to support shorthand imports of local modules: |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "import/resolver": { |
| 69 | + "alias": { |
| 70 | + "map": [["@", "./src"]], |
| 71 | + "extensions": [".js", ".jsx"] |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +This will allow you to write imports like this anywhere in your code: |
| 78 | + |
| 79 | +```jsx |
| 80 | +import Foo from '@/components/foo'; |
| 81 | +``` |
| 82 | + |
| 83 | +instead of relative paths: |
| 84 | + |
| 85 | +```jsx |
| 86 | +import Foo from '../../components/foo'; |
| 87 | +``` |
| 88 | + |
| 89 | +when using [absolute imports and module path aliases](https://nextjs.org/docs/advanced-features/module-path-aliases) in **Next.js**. |
| 90 | + |
| 91 | +This can also be overriden in your local `.eslintrc` file, if needed: |
| 92 | + |
| 93 | +```jsx |
| 94 | +// .eslintrc |
| 95 | +{ |
| 96 | + "extends": ["@kitani"], |
| 97 | + "settings": { |
| 98 | + "import/resolver": { |
| 99 | + "alias": { |
| 100 | + "map": [["@", "./lib"]], |
| 101 | + "extensions": [".js"] |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Prettier |
| 109 | + |
| 110 | +This config supports Prettier integration out of the box. Rules that may conflict with ESLint are disabled via recommended configuration in [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier). |
| 111 | + |
| 112 | +If you wish to override any [Prettier options](https://prettier.io/docs/en/options.html), you can do so by specifying them under `prettier/prettier` rule in your ESLint config file. For example: |
| 113 | + |
| 114 | +```jsx |
| 115 | +// .eslintrc |
| 116 | +{ |
| 117 | + "extends": ["@kitani"], |
| 118 | + "rules": { |
| 119 | + "prettier/prettier": [ |
| 120 | + "error", |
| 121 | + { |
| 122 | + "printWidth": 90 |
| 123 | + } |
| 124 | + ] |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Make sure that these rules match the options specified in your `.prettierrc` file. |
| 130 | + |
| 131 | +## Adding Scripts |
| 132 | + |
| 133 | +Add the following to your `package.json` file to define a script that will lint all known files and output the results: |
| 134 | + |
| 135 | +```jsx |
| 136 | +// package.json |
| 137 | +{ |
| 138 | + "scripts": { |
| 139 | + "lint": "eslint --ignore-path .gitignore ." |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +To fix all automatically-fixable issues, you can add the following script to your `package.json` as well (in addition to above): |
| 145 | + |
| 146 | +```jsx |
| 147 | +// package.json |
| 148 | +{ |
| 149 | + "scripts": { |
| 150 | + "lint:fix": "eslint --ignore-path .gitignore --fix ." |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +Note that you can update the above scripts as you see fit, this is just an example. See ESLint [CLI reference](https://eslint.org/docs/user-guide/command-line-interface) for more details. |
| 156 | + |
| 157 | +## Note on Next.js Link component |
| 158 | + |
| 159 | +There is a [known issue](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/402) with Next.js's decision to construct internal links by nesting an href-free `<a>` tag inside of a `<Link>` component. Next.js is also [aware of the issue](https://github.com/vercel/next.js/issues/5533) and has an [RFC](https://github.com/vercel/next.js/discussions/8207) working towards a solution. |
| 160 | + |
| 161 | +Because of this, the [standard usage](https://nextjs.org/docs/api-reference/next/link) of Next.js `<Link>` component will result in an error for the `jsx-a11y/anchor-is-valid` rule. Until the Next.js API can be updated to a more standard pattern, `@kitani/eslint-config` overrides this rule as suggested in [this issue](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/402#issuecomment-368305051): |
| 162 | + |
| 163 | +```json |
| 164 | +{ |
| 165 | + "jsx-a11y/anchor-is-valid": [ |
| 166 | + "error", |
| 167 | + { |
| 168 | + "components": ["Link"], |
| 169 | + "specialLink": ["hrefLeft", "hrefRight"], |
| 170 | + "aspects": ["invalidHref", "preferButton"] |
| 171 | + } |
| 172 | + ] |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +Please be aware, however, that this workaround also **disables the check for `href` attribute altogether** for regular `<a>` elements. Keep that in mind to ensure you're not breaking accessibility. |
2 | 177 |
|
3 | 178 | ## License |
4 | 179 |
|
5 | | -Licensed under MIT License. |
| 180 | +[MIT License](https://github.com/KitaniIslam/eslint-shared-configuration-for-react-next.js-/blob/main/LICENSE). |
0 commit comments