Lint and fix missing trailing newlines (#16445)
Apply the Clojure Style Guide recommendation to end files with proper lines (having a trailing newline character). See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
This commit is contained in:
parent
8978e92042
commit
19ca8e28a5
25
Makefile
25
Makefile
|
@ -14,17 +14,17 @@ WHITE := $(shell tput -Txterm setaf 7)
|
|||
YELLOW := $(shell tput -Txterm setaf 3)
|
||||
RESET := $(shell tput -Txterm sgr0)
|
||||
HELP_FUN = \
|
||||
%help; \
|
||||
while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
|
||||
print "Usage: make [target]\n\nSee STARTING_GUIDE.md for more info.\n\n"; \
|
||||
for (sort keys %help) { \
|
||||
print "${WHITE}$$_:${RESET}\n"; \
|
||||
for (@{$$help{$$_}}) { \
|
||||
$$sep = " " x (32 - length $$_->[0]); \
|
||||
print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
|
||||
}; \
|
||||
print "\n"; \
|
||||
}
|
||||
%help; \
|
||||
while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-]+)\s*:.*\#\#(?:@([a-zA-Z\-]+))?\s(.*)$$/ }; \
|
||||
print "Usage: make [target]\n\nSee STARTING_GUIDE.md for more info.\n\n"; \
|
||||
for (sort keys %help) { \
|
||||
print "${WHITE}$$_:${RESET}\n"; \
|
||||
for (@{$$help{$$_}}) { \
|
||||
$$sep = " " x (32 - length $$_->[0]); \
|
||||
print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \
|
||||
}; \
|
||||
print "\n"; \
|
||||
}
|
||||
HOST_OS := $(shell uname | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
# This can come from Jenkins
|
||||
|
@ -307,6 +307,7 @@ lint: ##@test Run code style checks
|
|||
clj-kondo --config .clj-kondo/config.edn --cache false --lint src && \
|
||||
ALL_CLOJURE_FILES=$(call find_all_clojure_files) && \
|
||||
zprint '{:search-config? true}' -sfc $$ALL_CLOJURE_FILES && \
|
||||
sh scripts/lint-trailing-newline.sh && \
|
||||
yarn prettier
|
||||
|
||||
# NOTE: We run the linter twice because of https://github.com/kkinnear/zprint/issues/271
|
||||
|
@ -315,9 +316,9 @@ lint-fix: ##@test Run code style checks and fix issues
|
|||
ALL_CLOJURE_FILES=$(call find_all_clojure_files) && \
|
||||
zprint '{:search-config? true}' -sw $$ALL_CLOJURE_FILES && \
|
||||
zprint '{:search-config? true}' -sw $$ALL_CLOJURE_FILES && \
|
||||
sh scripts/lint-trailing-newline.sh --fix && \
|
||||
yarn prettier
|
||||
|
||||
|
||||
shadow-server: export TARGET := clojure
|
||||
shadow-server:##@ Start shadow-cljs in server mode for watching
|
||||
yarn shadow-cljs server
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eof pipefail
|
||||
|
||||
FILES=$(comm -23 <(sort <(git ls-files --cached --others --exclude-standard)) <(sort <(git ls-files --deleted)) | grep --ignore-case -E '\.(java|cpp|nix|json|sh|md|js|clj|cljs|cljc|edn)$')
|
||||
N_FILES=$(echo "$FILES" | wc -l)
|
||||
LINT_SHOULD_FIX=0
|
||||
|
||||
if [[ -n $1 && $1 != '--fix' ]]; then
|
||||
echo "Unknown option '$1'" >&2
|
||||
exit 1
|
||||
elif [[ $1 == '--fix' ]]; then
|
||||
LINT_SHOULD_FIX=1
|
||||
fi
|
||||
|
||||
echo "Checking ${N_FILES} files for missing trailing newlines."
|
||||
|
||||
# Do not process the whole file and only check the last character. Ignore empty
|
||||
# files. Taken from https://stackoverflow.com/a/10082466.
|
||||
for file in $FILES; do
|
||||
if [ -s "$file" ] && [ "$(tail -c1 "$file"; echo x)" != $'\nx' ]; then
|
||||
if [[ $LINT_SHOULD_FIX -eq 1 ]]; then
|
||||
echo "" >>"$file"
|
||||
else
|
||||
LINT_ERROR=1
|
||||
echo "No trailing newline: $file" >&2
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ $LINT_ERROR -eq 1 ]]; then
|
||||
exit 1
|
||||
fi
|
Loading…
Reference in New Issue