Skip to content

Commit

Permalink
Add lint:changed and lint:changed:fix (#234)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mcmire authored Mar 2, 2024
1 parent 838d982 commit caa72ab
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
17 changes: 1 addition & 16 deletions .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,7 @@ if echo "$stdin" | grep -q "^(delete)"; then
exit 0
fi

current_branch_name="$(git branch --show-current)"

if [[ "$current_branch_name" == "main" ]]; then
raw_files_to_check="$(git diff origin/main...HEAD --name-only --diff-filter=d)"
else
raw_files_to_check="$(git diff main...HEAD --name-only --diff-filter=d)"
fi

if [[ -n "$raw_files_to_check" ]]; then
echo "*** Checking for lint violations in changed files ***************"
echo

echo "$raw_files_to_check" | while IFS=$'\n' read -r line; do
printf '%s\0' "$line"
done | xargs -0 yarn prettier --check --ignore-unknown || exit $?
fi
scripts/lint-changed-files.sh --check

echo
echo "*** Auditing dependencies ***************"
Expand Down
6 changes: 6 additions & 0 deletions docs/contributors/how-to-contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ yarn lint:fix
Provided that you ran `bin/setup` above,
any code you've changed will also be linted
whenever you push a commit.
If this step fails for any reason,
you can fix lint violations in only changed files by running:

```
yarn lint:changed:fix
```

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

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"private": true,
"scripts": {
"audit": "yarn npm audit && bundle exec bundle audit --gemfile-lock ${BUNDLE_GEMFILE:-Gemfile}",
"lint": "prettier --check .",
"lint:fix": "yarn lint --write",
"lint": "scripts/lint-all-files.sh --check",
"lint:fix": "scripts/lint-all-files.sh --write",
"lint:changed": "scripts/lint-changed-files.sh --include-uncommitted --check",
"lint:changed:fix": "scripts/lint-changed-files.sh --include-uncommitted --write",
"setup-git-hooks": "husky install"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions scripts/lint-all-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

exec prettier "$@" .
64 changes: 64 additions & 0 deletions scripts/lint-changed-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

get-files-to-lint() {
local flag="$1"
local current_branch_name="$(git branch --show-current)"

if [[ "$current_branch_name" == "main" ]]; then
git diff origin/main...HEAD --name-only --diff-filter=d
else
git diff main...HEAD --name-only --diff-filter=d
fi

if [[ $flag == "--include-uncommitted" ]]; then
git diff --name-only --diff-filter=d
fi
}

main() {
local prettier_flag
local include_uncommitted_flag

while [[ -n "$1" ]]; do
case "$1" in
--check | --write)
prettier_flag="$1"
shift
;;
--include-uncommitted)
include_uncommitted_flag="$1"
shift
;;
*)
echo "ERROR: Unknown option $1."
exit 1
;;
esac
done

local files_to_lint="$(get-files-to-lint "$include_uncommitted_flag")"

if [[ -z "$prettier_flag" ]]; then
echo "ERROR: Missing --check or --write."
exit 1
fi

echo "*** Checking for lint violations in changed files ***************"
echo

if [[ -n "$files_to_lint" ]]; then
echo "Files to check:"
echo "$files_to_lint" | while IFS=$'\n' read -r line; do
echo "- $line"
done

echo
echo "$files_to_lint" | while IFS=$'\n' read -r line; do
printf '%s\0' "$line"
done | xargs -0 yarn prettier "$prettier_flag" --ignore-unknown
else
echo "No files to lint this time."
fi
}

main "$@"

0 comments on commit caa72ab

Please sign in to comment.