Skip to content

Commit 622c081

Browse files
committed
Document pre-commit setup in depth
1 parent b8634bf commit 622c081

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,53 @@ We do not include linting for Vue out of the box. Have a look at the [eslint-plu
136136

137137
This config is [Prettier](https://prettier.io/)-compatible, there isn’t anything extra needed.
138138

139+
### pre-commit
140+
141+
The [pre-commit](https://pre-commit.com/) pre-commit hook framework doesn’t work well with ESLint. There are three major points to set up correctly:
142+
143+
- Peer dependencies aren’t automatically installed like in other npm v7+ environment. We need to tell pre-commit what to install via its `additional_dependencies` configuration.
144+
- Dependency versions aren’t locked. Always pin exact versions in the configuration to have as stable of an installation as possible.
145+
- By default, the ESLint hook will only run `.js` files. Make sure to override its `types` attribute as well as `files` with the correct extensions (TypeScript, Vue).
146+
147+
Here is a sample setup with our recommended configuration:
148+
149+
```yaml
150+
default_language_version:
151+
node: system
152+
repos:
153+
- repo: https://github.com/pre-commit/mirrors-eslint
154+
rev: v8.10.0
155+
hooks:
156+
- id: eslint
157+
types: [file]
158+
files: \.(js|ts|tsx)$
159+
args: [--report-unused-disable-directives]
160+
additional_dependencies:
161+
# Use the same versions as the project’s package-lock.json.
162+
163+
164+
165+
# Even on npm v7+, we need to specify all peerDependencies
166+
# as pre-commit installs `additional_dependencies` globally.
167+
- '@typescript-eslint/[email protected]'
168+
- '@typescript-eslint/[email protected]'
169+
170+
171+
172+
173+
174+
175+
```
176+
177+
The latest versions to install can be resolved with either:
178+
179+
```bash
180+
# Retrieve latest versions from npm:
181+
npx install-peerdeps --dry-run --dev eslint-config-torchbox@latest
182+
# Or retrieve currently-installed versions from the current project:
183+
npm ls eslint eslint-config-torchbox typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-airbnb eslint-config-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks | grep -v deduped
184+
```
185+
139186
### Experimental syntax
140187

141188
By default, this config uses ESLint’s built-in parser, which doesn’t support [experimental ECMAScript features](https://github.com/eslint/eslint/blob/a675c89573836adaf108a932696b061946abf1e6/README.md#what-about-experimental-features). If your code uses experimental syntax with Babel, make sure to set the ESLint parser to [babel-eslint](https://github.com/babel/babel-eslint):
@@ -154,12 +201,19 @@ module.exports = {
154201
This configuration strikes a balance between ease of use for users, and ease of maintenance.
155202

156203
- We use the same React-aware configuration everywhere, even non-React projects.
157-
- We use a separate TypeScript configuration file only due to its experimental nature.
158204
- There is a single package with a single set of dependencies.
159205

160206
The base configuration is kept very simple, extending from the Airbnb configuration, with Prettier compatibility and more permissive rules.
161207

162-
The TypeScript configuration does not rely on type checking, so it can also be used for JavaScript projects. This will eventually allow us to have a single configuration file for all projects, once the TypeScript support is no longer deemed experimental.
208+
### TypeScript support
209+
210+
We use a separate TypeScript configuration file only due to its experimental nature. The TypeScript configuration does not rely on type checking, so it can also be used for JavaScript projects.
211+
212+
In the future, we may decide to use TypeScript for the default configuration, and have a separate configuration file for vanilla JS projects. Or document how to use the TypeScript configuration on vanilla projects (resetting the `parser` should be the only necessary change).
213+
214+
### Dependencies as peerDependencies
215+
216+
Most of the configuration’s dependencies are specified as `peerDependencies`. This is necessary due to how ESLint configurations resolve their dependencies – see [Support having plugins as dependencies in shareable config #3458](https://github.com/eslint/eslint/issues/3458). This will be changed in a future version of ESLint.
163217

164218
## What’s included
165219

0 commit comments

Comments
 (0)