forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding linter and formatter pre-commit hooks for staged BE files
* Adding linter and formatter to staged BE files. Note that `.cljfmt/indents.clj` tries to capture our indent rules and, so far, seems to get things right. There are still some inconsistencies in some of our nses in how we indent assoc(-in) and some t2 functions when it comes to key-value pairs on following lines. Should the keys align with the function call or be indented. Aesthetically, people seem to like indents, but these are functions, so should align with the first argument. IDK that it really matters as long as we have agreement. We may, as we adopt this, have some files be reformatted in unexpected ways for items that have been missed. If so, the developer can simply update the .cljfmt/indents.clj file, rerun the hook, and commit that along with their other changes. * Added whitespace linter. * Updating husky commit hook * Updated hook scripts to be exclusively for staged files. Updated cljfmt to use the latest and greatest along with the new format of config file. * Commenting out the actual formatting hook in .husky/pre-commit until we come up with a globally happy solution to forms that don't have good formatting rules, like defprotocol+ in a reader conditional. * Reverting formatting * Moving pre-commit hooks from .husky/pre-commit to the package.json's lint-staged section. Note that we're still not calling the cljfmt file (.bin/cljfmt_staged.sh) until we get agreement on formatting. One thing we might do since we're using lint-staged is we can probably exclude the "bad" files that go sideways with linting and format everything else automatically. This might be a good follow-on PR.
- Loading branch information
1 parent
f0f4e7f
commit d4957af
Showing
8 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
;; https://github.com/weavejester/cljfmt/blob/master/cljfmt/resources/cljfmt/indents/clojure.clj | ||
{:extra-indents | ||
{auto-parse [[:inner 0]] | ||
cond-> [[:inner 0]] | ||
check [[:inner 0]] | ||
deferred-tru [[:inner 0]] | ||
defprotocol+ [[:block 1] [:inner 1]] | ||
potemkin/defprotocol+ [[:block 1] [:inner 1]] | ||
let-404 [[:block 1]] | ||
metabase.mbql.util/match-one [[:inner 0]] | ||
merge-with [[:inner 0]] | ||
metabase.test/dataset [[:inner 0]] | ||
metabase.test/formatted-rows [[:inner 0]] | ||
metabase.test/mbql-query [[:inner 0]] | ||
metabase.test/test-drivers [[:inner 0]] | ||
metabase.test/with-temporary-setting-values [[:block 1]] | ||
;; If we just treat these as functions we can ignore this, despite some files not being consistent | ||
;metabase.models.permissions/set-has-full-permissions-for-set? [[:inner 0]] | ||
;metabase.models.permissions/set-has-full-permissions? [[:inner 0]] | ||
;metabase.models.permissions/set-has-partial-permissions? [[:inner 0]] | ||
metabase.models.permissions/set-has-partial-permissions-for-set? [[:inner 0]] | ||
publish-event! [[:block 1]] | ||
metabase.query-processor.streaming/streaming-response [[:inner 0]] | ||
metabase.util/prog1 [[:inner 0]] | ||
metabase.util/select-keys-when [[:inner 0]] | ||
;; This supports a lot of existing formatting, but these are functions so the | ||
;; existing formatting is probably wrong. | ||
;toucan2.core/select [[:inner 0]] | ||
;toucan2.core/update! [[:inner 0]] | ||
} | ||
:sort-ns-references? true} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#! /usr/bin/env bash | ||
|
||
# Reformat staged clj, cljc, and cljs files | ||
# Will print the results of the call. Will return an error (stopping the commit stage) if any files are reformatted. | ||
# | ||
# This is not a custom reformatter. | ||
# In the event that do you want to do some manual reformatting, you can do this: | ||
# clojure -T:cljfmt fix ;; Reformat everything | ||
|
||
set -euo pipefail | ||
|
||
STAGED_FILES=$(git diff --name-status --cached -- "*.clj" "*.cljc" "*.cljs" | grep -E '[AM]' | cut -f2 || true) | ||
|
||
if [ "${#STAGED_FILES}" -gt 0 ]; then | ||
args=() | ||
for file in $STAGED_FILES; do | ||
args+=("\"$file\"") | ||
done | ||
|
||
output=$(clojure -T:cljfmt fix "{:paths [${args[*]}]}" 2>&1) | ||
|
||
## Return a non-zero error code if any files were formatted since this will cause the commit to abort | ||
if [ -n "$output" ]; then | ||
echo $output | ||
exit 1 | ||
else | ||
echo "All staged clj, cljc, or cljs formatted correctly" | ||
fi | ||
else | ||
echo "No staged clj, cljc, or cljs files to format" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#! /usr/bin/env bash | ||
|
||
# Do whitespace linting on passed in files. | ||
# This is intended to be called from a commit hook (see package.json > lint-staged) that passes in staged changes | ||
|
||
set -euo pipefail | ||
|
||
STAGED_FILES="$@" | ||
|
||
if [ "${#STAGED_FILES}" -gt 0 ]; then | ||
args=() | ||
for file in $STAGED_FILES; do | ||
args+=("\"$file\"") | ||
done | ||
echo checking formatting for $STAGED_FILES | ||
|
||
clojure -T:whitespace-linter lint "{:paths [${args[*]}]}" | ||
else | ||
echo "No staged clj, cljc, cljs, or edn files to whitespace lint." | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters