From 6cf762624d06a419e6ab8beee1f7044090234f37 Mon Sep 17 00:00:00 2001 From: Eduard Staniloiu Date: Thu, 3 Feb 2022 10:02:22 +0200 Subject: [PATCH] Add style checker (#9) --- .github/workflows/build.yml | 6 +++++- makefile | 39 +++++++++++++++++++++++++++++++++++++ src/dscanner/imports.d | 2 +- src/dscanner/main.d | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f23461a..7999630 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,8 +17,12 @@ jobs: #- name: Setup upterm session #uses: lhotari/action-upterm@v1 + - name: Run style checks + run: | + source ~/dlang/*/activate + make style + - name: Run tests run: | source ~/dlang/*/activate make test - diff --git a/makefile b/makefile index 2aca819..c09542f 100644 --- a/makefile +++ b/makefile @@ -175,3 +175,42 @@ report: all release: ./release.sh + +# Add source files here as we transition to DMD-as-a-library +STYLE_CHECKED_SRC := \ + src/dscanner/imports.d \ + src/dscanner/main.d + +style: + @echo "Check for trailing whitespace" + grep -nr '[[:blank:]]$$' ${STYLE_CHECKED_SRC}; test $$? -eq 1 + + @echo "Enforce whitespace before opening parenthesis" + grep -nrE "\<(for|foreach|foreach_reverse|if|while|switch|catch|version)\(" ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce no whitespace after opening parenthesis" + grep -nrE "\<(version) \( " ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce whitespace between colon(:) for import statements (doesn't catch everything)" + grep -nr 'import [^/,=]*:.*;' ${STYLE_CHECKED_SRC} | grep -vE "import ([^ ]+) :\s"; test $$? -eq 1 + + @echo "Check for package wide std.algorithm imports" + grep -nr 'import std.algorithm : ' ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce Allman style" + grep -nrE '(if|for|foreach|foreach_reverse|while|unittest|switch|else|version) .*{$$' ${STYLE_CHECKED_SRC}; test $$? -eq 1 + + @echo "Enforce do { to be in Allman style" + grep -nr 'do *{$$' ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce no space between assert and the opening brace, i.e. assert(" + grep -nrE 'assert +\(' ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce space after cast(...)" + grep -nrE '[^"]cast\([^)]*?\)[[:alnum:]]' ${STYLE_CHECKED_SRC} ; test $$? -eq 1 + + @echo "Enforce space between a .. b" + grep -nrE '[[:alnum:]][.][.][[:alnum:]]|[[:alnum:]] [.][.][[:alnum:]]|[[:alnum:]][.][.] [[:alnum:]]' ${STYLE_CHECKED_SRC}; test $$? -eq 1 + + @echo "Enforce space between binary operators" + grep -nrE "[[:alnum:]](==|!=|<=|<<|>>|>>>|^^)[[:alnum:]]|[[:alnum:]] (==|!=|<=|<<|>>|>>>|^^)[[:alnum:]]|[[:alnum:]](==|!=|<=|<<|>>|>>>|^^) [[:alnum:]]" ${STYLE_CHECKED_SRC}; test $$? -eq 1 diff --git a/src/dscanner/imports.d b/src/dscanner/imports.d index 6fc4d87..f4202a6 100644 --- a/src/dscanner/imports.d +++ b/src/dscanner/imports.d @@ -36,7 +36,7 @@ extern(C++) class ImportVisitor(AST) : ParseTimeTransitiveVisitor!AST { import std.conv; string s; - + foreach (const pid; imp.packages) s = s ~ to!string(pid.toChars()) ~ "."; diff --git a/src/dscanner/main.d b/src/dscanner/main.d index d833737..adee4a4 100644 --- a/src/dscanner/main.d +++ b/src/dscanner/main.d @@ -599,7 +599,7 @@ string getDefaultConfigurationLocation() configDir = buildPath(configDir, "dscanner", CONFIG_FILE_NAME); return configDir; } - else version(Windows) + else version (Windows) { string configDir = environment.get("APPDATA", null); enforce(configDir !is null, "%APPDATA% is unset");