Skip to content

Commit caa72ab

Browse files
authored
Add lint:changed and lint:changed:fix (#234)
The Git hook that runs on push will only lint files changed within the current branch. However, it won't fix anything; to do that, you'll have to run `yarn lint:fix`. But actually, this runs every file through the linter, so it's fairly inefficient. This commit adds `lint:changed` and `lint:changed:fix` so that you can do the same thing that the Git hook does.
1 parent 838d982 commit caa72ab

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

.husky/pre-push

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,7 @@ if echo "$stdin" | grep -q "^(delete)"; then
99
exit 0
1010
fi
1111

12-
current_branch_name="$(git branch --show-current)"
13-
14-
if [[ "$current_branch_name" == "main" ]]; then
15-
raw_files_to_check="$(git diff origin/main...HEAD --name-only --diff-filter=d)"
16-
else
17-
raw_files_to_check="$(git diff main...HEAD --name-only --diff-filter=d)"
18-
fi
19-
20-
if [[ -n "$raw_files_to_check" ]]; then
21-
echo "*** Checking for lint violations in changed files ***************"
22-
echo
23-
24-
echo "$raw_files_to_check" | while IFS=$'\n' read -r line; do
25-
printf '%s\0' "$line"
26-
done | xargs -0 yarn prettier --check --ignore-unknown || exit $?
27-
fi
12+
scripts/lint-changed-files.sh --check
2813

2914
echo
3015
echo "*** Auditing dependencies ***************"

docs/contributors/how-to-contribute.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ yarn lint:fix
9393
Provided that you ran `bin/setup` above,
9494
any code you've changed will also be linted
9595
whenever you push a commit.
96+
If this step fails for any reason,
97+
you can fix lint violations in only changed files by running:
98+
99+
```
100+
yarn lint:changed:fix
101+
```
96102

97103
[prettier-editors]: https://prettier.io/docs/en/editors.html
98104

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"private": true,
66
"scripts": {
77
"audit": "yarn npm audit && bundle exec bundle audit --gemfile-lock ${BUNDLE_GEMFILE:-Gemfile}",
8-
"lint": "prettier --check .",
9-
"lint:fix": "yarn lint --write",
8+
"lint": "scripts/lint-all-files.sh --check",
9+
"lint:fix": "scripts/lint-all-files.sh --write",
10+
"lint:changed": "scripts/lint-changed-files.sh --include-uncommitted --check",
11+
"lint:changed:fix": "scripts/lint-changed-files.sh --include-uncommitted --write",
1012
"setup-git-hooks": "husky install"
1113
},
1214
"devDependencies": {

scripts/lint-all-files.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec prettier "$@" .

scripts/lint-changed-files.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
get-files-to-lint() {
4+
local flag="$1"
5+
local current_branch_name="$(git branch --show-current)"
6+
7+
if [[ "$current_branch_name" == "main" ]]; then
8+
git diff origin/main...HEAD --name-only --diff-filter=d
9+
else
10+
git diff main...HEAD --name-only --diff-filter=d
11+
fi
12+
13+
if [[ $flag == "--include-uncommitted" ]]; then
14+
git diff --name-only --diff-filter=d
15+
fi
16+
}
17+
18+
main() {
19+
local prettier_flag
20+
local include_uncommitted_flag
21+
22+
while [[ -n "$1" ]]; do
23+
case "$1" in
24+
--check | --write)
25+
prettier_flag="$1"
26+
shift
27+
;;
28+
--include-uncommitted)
29+
include_uncommitted_flag="$1"
30+
shift
31+
;;
32+
*)
33+
echo "ERROR: Unknown option $1."
34+
exit 1
35+
;;
36+
esac
37+
done
38+
39+
local files_to_lint="$(get-files-to-lint "$include_uncommitted_flag")"
40+
41+
if [[ -z "$prettier_flag" ]]; then
42+
echo "ERROR: Missing --check or --write."
43+
exit 1
44+
fi
45+
46+
echo "*** Checking for lint violations in changed files ***************"
47+
echo
48+
49+
if [[ -n "$files_to_lint" ]]; then
50+
echo "Files to check:"
51+
echo "$files_to_lint" | while IFS=$'\n' read -r line; do
52+
echo "- $line"
53+
done
54+
55+
echo
56+
echo "$files_to_lint" | while IFS=$'\n' read -r line; do
57+
printf '%s\0' "$line"
58+
done | xargs -0 yarn prettier "$prettier_flag" --ignore-unknown
59+
else
60+
echo "No files to lint this time."
61+
fi
62+
}
63+
64+
main "$@"

0 commit comments

Comments
 (0)