diff options
author | Robin Jarry <robin@jarry.cc> | 2022-10-16 12:05:25 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-10-19 10:19:19 +0200 |
commit | ebcd6fcea1517b00153214e3284d6307809175f5 (patch) | |
tree | 654bb83820741ea5bf279f13aef60aabdde8fb07 /contrib/check-whitespace | |
parent | b22639ab20a329026d41e90f03a153c3e49c1b4c (diff) | |
download | aerc-ebcd6fcea1517b00153214e3284d6307809175f5.tar.gz |
lint: check for bad white space habits
A little coding hygiene cannot hurt. Add a simple awk script to check
all source files for bad white space habits:
- trailing white space
- trailing new lines at the end of files
- missing new line at the end of files
- spaces followed by tabs
The script outputs color when the terminal supports it. It exits with
a non-zero code when there was at least one white space issue found.
Call the script in the lint step.
Example output of the awk script:
config/default_styleset:1:# <-- trailing whitespace
config/default_styleset:3:# <-- trailing whitespace
doc/aerc.1.scd:78: Executes an arbitrary command in the background. Aerc will set the <-- trailing whitespace
doc/aerc.1.scd:234: <-- trailing whitespace
doc/aerc.1.scd:237: <-- trailing whitespace
worker/types/thread_test.go:74: // return ErrSkipThread<-- space(s) followed by tab(s)
worker/lib/testdata/message/invalid/hexa: trailing new line(s)
Fix issues reported by the script.
NB: The ENDFILE match is a GNU extension. It will be ignored on BSD-awk
and trailing new lines will not be detected. The lint make target is
only invoked on alpine linux which has GNU awk anyway.
NB: Empty cells in scdoc tables require trailing white space... Avoid
this by setting content in these cells. I don't really see a use for
empty cells.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'contrib/check-whitespace')
-rwxr-xr-x | contrib/check-whitespace | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/contrib/check-whitespace b/contrib/check-whitespace new file mode 100755 index 00000000..5afd2f4c --- /dev/null +++ b/contrib/check-whitespace @@ -0,0 +1,51 @@ +#!/usr/bin/awk -f + +BEGIN { + isatty = system("test -t 1") == "0" + retcode = 0 +} + +function color(code, s) { + if (isatty) { + return "\033[" code "m" s "\033[0m" + } + return s +} +function red(s) { return color("31", s) } +function green(s) { return color("32", s) } +function magenta(s) { return color("35", s) } +function cyan(s) { return color("36", s) } +function bg_red(s) { return color("41", s) } +function hl_ws(s, pattern) { + gsub(pattern, bg_red("&"), s) + # convert tab characters to 8 spaces to allow coloring + gsub(/\t/, " ", s) + return s +} + +/ +\t+/ { + retcode = 1 + print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ + hl_ws($0, " +\\t+") red("<-- space(s) followed by tab(s)") +} + +/[ \t]+$/ { + retcode = 1 + print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ + hl_ws($0, "[ \\t]+$") red("<-- trailing whitespace") +} + +ENDFILE { + # will only match on GNU awk, ignored on non-GNU versions + if ($0 ~ /^[ \t]*$/) { + retcode = 1 + print magenta(FILENAME) cyan(": ") red("trailing new line(s)") + } else if (RT != "\n") { + retcode = 1 + print magenta(FILENAME) cyan(": ") red("no new line at end of file") + } +} + +END { + exit retcode +} |