Skip to content

Commit 9ce6c66

Browse files
authored
feat: use lint-staged for lint and fixes (#2258)
### 🎯 Goal I got a bit frustrated with our pre-commit hook failing because changes in files unrelated to the commit. Plus, the command ran by the pre-commit hook was pretty slow, running for a couple of seconds. It would be nice for the pre-commit hook to only lint the files that are actually staged. ### πŸ›  Implementation details The easiest (and most standard) way to achieve that is by using the `lint-staged` package. It allows to pass a list of **staged** files (filtered by glob) to our lint commands. That prevents the pre-commit hook from failing because of errors in files that are not part of the commit. And, since we don't lint the whole codebase every time, it runs faster too. I also added a new command `fix-staged` that will run auto-fixes for the staged files only. The pre-commit hook now prompts to use this command. ### Demo https://github.com/GetStream/stream-chat-react/assets/975978/f36a7109-92e7-45a5-b431-1e99a8538285
1 parent 1fe4507 commit 9ce6c66

File tree

5 files changed

+282
-15
lines changed

5 files changed

+282
-15
lines changed

β€Ž.husky/pre-commitβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
set -e
55

6-
if ! yarn run lint; then
7-
yarn run lint-fix
8-
echo "some files were not formatted correctly (prettier/eslint) commit aborted!"
9-
echo "your changes are still staged, you can accept formatting changes with git add or ignore them by adding --no-verify to git commit"
6+
if ! yarn run lint-staged; then
7+
echo
8+
echo "Some files were not formatted correctly (see output above), commit aborted!"
9+
echo "Consider running \"yarn run fix-staged\" to attempt auto-fix."
1010
exit 1
1111
fi

β€Ž.lintstagedrc.fix.jsonβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"src/**/*.{js,ts,tsx,md}": "eslint --fix",
3+
"{src/**/*.{js,ts,tsx,md,json}, .eslintrc.json, .prettierrc, babel.config.js}": "prettier --write"
4+
}

β€Ž.lintstagedrc.jsonβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"src/**/*.{js,ts,tsx,md}": "eslint --max-warnings 0",
3+
"{src/**/*.{js,ts,tsx,md,json}, .eslintrc.json, .prettierrc, babel.config.js}": "prettier --list-different",
4+
"src/i18n/*.json": "yarn run validate-translations"
5+
}

β€Žpackage.jsonβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
"jest": "^28.1.3",
204204
"jest-axe": "^8.0.0",
205205
"jest-environment-jsdom": "^28.1.3",
206+
"lint-staged": "^15.2.1",
206207
"moment-timezone": "^0.5.43",
207208
"postcss": "^8.1.10",
208209
"postcss-loader": "^4.1.0",
@@ -241,6 +242,7 @@
241242
"lint-fix": "prettier --write 'src/**/*.{js,ts,tsx,md,json}' .eslintrc.json .prettierrc babel.config.js && eslint --fix 'src/**/*.{js,ts,tsx,md}' --max-warnings 0",
242243
"prettier": "prettier --list-different '**/*.{js,ts,tsx,md,json}' .eslintrc.json .prettierrc babel.config.js",
243244
"prettier-fix": "prettier --write '**/*.{js,ts,tsx,md,json}' .eslintrc.json .prettierrc babel.config.js",
245+
"fix-staged": "lint-staged --config .lintstagedrc.fix.json --concurrent 1",
244246
"start": "tsc --watch",
245247
"prepare": "husky install && yarn run build",
246248
"preversion": "yarn install",

0 commit comments

Comments
Β (0)