From 897185a4aa3dded15d11d1a83c3c0e48d9351bdd Mon Sep 17 00:00:00 2001 From: Jake Herbst Date: Wed, 10 Nov 2021 13:28:50 -0500 Subject: [PATCH] Adding scalafmt (#2053) * Adding scalafmt * add it Co-authored-by: Jake Herbst Co-authored-by: Lukas Gravley --- .automation/test/scalafmt/README.md | 19 +++++++++++ .../test/scalafmt/scalafmt_bad_1.scala | 31 ++++++++++++++++++ .../test/scalafmt/scalafmt_good_1.scala | 32 +++++++++++++++++++ Dockerfile | 6 ++++ Dockerfile-slim | 6 ++++ README.md | 3 ++ lib/functions/buildFileList.sh | 9 ++++++ lib/linter.sh | 10 ++++-- .../super-linter/controls/super_linter.rb | 2 ++ 9 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 .automation/test/scalafmt/README.md create mode 100644 .automation/test/scalafmt/scalafmt_bad_1.scala create mode 100644 .automation/test/scalafmt/scalafmt_good_1.scala diff --git a/.automation/test/scalafmt/README.md b/.automation/test/scalafmt/README.md new file mode 100644 index 00000000..3288feba --- /dev/null +++ b/.automation/test/scalafmt/README.md @@ -0,0 +1,19 @@ +# Scala Test Cases + +This folder holds the test cases for **Scala**. + +## Additional Docs + +No Additional information is needed for this test case. + +## Good Test Cases + +The test cases denoted: `LANGUAGE_good_FILE.EXTENSION` are all valid, and should pass successfully when linted. + +- **Note:** They are linted utilizing the default linter rules. + +## Bad Test Cases + +The test cases denoted: `LANGUAGE_bad_FILE.EXTENSION` are **NOT** valid, and should trigger errors when linted. + +- **Note:** They are linted utilizing the default linter rules. diff --git a/.automation/test/scalafmt/scalafmt_bad_1.scala b/.automation/test/scalafmt/scalafmt_bad_1.scala new file mode 100644 index 00000000..9343290f --- /dev/null +++ b/.automation/test/scalafmt/scalafmt_bad_1.scala @@ -0,0 +1,31 @@ +object a { + class a[ + t1, + t2 // comment + ](a1: Int, + a2: Int // comment + )( + b1: String, + b2: String // comment + )(implicit + c1: SomeType1, + c2: SomeType2 // comment + ) { + def this( + a1: Int, + a2: Int // comment + )( + b1: String, + b2: String // comment + )(implicit + c1: SomeType1, + c2: SomeType2 // comment + ) = this( + a1, + a2 // comment + ) ( + b1, + b2 // comment + ) + } +} diff --git a/.automation/test/scalafmt/scalafmt_good_1.scala b/.automation/test/scalafmt/scalafmt_good_1.scala new file mode 100644 index 00000000..40dcd4b1 --- /dev/null +++ b/.automation/test/scalafmt/scalafmt_good_1.scala @@ -0,0 +1,32 @@ +object a { + class a[ + t1, + t2 // comment + ]( + a1: Int, + a2: Int // comment + )( + b1: String, + b2: String // comment + )(implicit + c1: SomeType1, + c2: SomeType2 // comment + ) { + def this( + a1: Int, + a2: Int // comment + )( + b1: String, + b2: String // comment + )(implicit + c1: SomeType1, + c2: SomeType2 // comment + ) = this( + a1, + a2 // comment + )( + b1, + b2 // comment + ) + } +} diff --git a/Dockerfile b/Dockerfile index 411cd094..3d4d3981 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,7 @@ FROM zricethezav/gitleaks:v7.6.1 as gitleaks FROM garethr/kubeval:0.15.0 as kubeval FROM ghcr.io/assignuser/lintr-lib:0.3.0 as lintr-lib FROM ghcr.io/awkbar-devops/clang-format:v1.0.2 as clang-format +FROM scalameta/scalafmt:v2.7.5 as scalafmt ################## # Get base image # @@ -253,6 +254,11 @@ COPY --from=clang-format /usr/bin/clang-format /usr/bin/ #################### COPY --from=gitleaks /usr/bin/gitleaks /usr/bin/ +#################### +# Install scalafmt # +#################### +COPY --from=scalafmt /bin/scalafmt /usr/bin/ + ################# # Install Litnr # ################# diff --git a/Dockerfile-slim b/Dockerfile-slim index fa522c97..d9a3fee2 100644 --- a/Dockerfile-slim +++ b/Dockerfile-slim @@ -29,6 +29,7 @@ FROM zricethezav/gitleaks:v7.6.1 as gitleaks FROM garethr/kubeval:0.15.0 as kubeval FROM ghcr.io/assignuser/lintr-lib:0.3.0 as lintr-lib FROM ghcr.io/awkbar-devops/clang-format:v1.0.2 as clang-format +FROM scalameta/scalafmt:v2.7.5 as scalafmt ################## # Get base image # @@ -191,6 +192,11 @@ COPY --from=clang-format /usr/bin/clang-format /usr/bin/ #################### COPY --from=gitleaks /usr/bin/gitleaks /usr/bin/ +#################### +# Install scalafmt # +#################### +COPY --from=scalafmt /bin/scalafmt /usr/bin/ + ################# # Install Litnr # ################# diff --git a/README.md b/README.md index a3420f9c..32ddc59a 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their codebase w | **Raku** | [Raku](https://raku.org) | | **Ruby** | [RuboCop](https://github.com/rubocop-hq/rubocop) | | **Rust** | [Rustfmt](https://github.com/rust-lang/rustfmt) / [Clippy](https://github.com/rust-lang/rust-clippy) | +| **Scala** | [scalafmt](https://github.com/scalameta/scalafmt) | | **Secrets** | [GitLeaks](https://github.com/zricethezav/gitleaks) | | **Shell** | [Shellcheck](https://github.com/koalaman/shellcheck) / [executable bit check] / [shfmt](https://github.com/mvdan/sh) | | **Snakemake** | [snakefmt](https://github.com/snakemake/snakefmt/) / [snakemake --lint](https://snakemake.readthedocs.io/en/stable/snakefiles/writing_snakefiles.html#best-practices) | @@ -310,6 +311,7 @@ But if you wish to select or exclude specific linters, we give you full control | **RUBY_CONFIG_FILE** | `.ruby-lint.yml` | Filename for [rubocop configuration](https://docs.rubocop.org/rubocop/configuration.html) (ex: `.ruby-lint.yml`, `.rubocop.yml`) | | **SUPPRESS_FILE_TYPE_WARN** | `false` | If set to `true`, will hide warning messages about files without their proper extensions. Default is `false` | | **SUPPRESS_POSSUM** | `false` | If set to `true`, will hide the ASCII possum at top of log output. Default is `false` | +| **SCALAFMT_CONFIG_FILE** | `.scalafmt.conf` | Filename for [scalafmt configuration](https://scalameta.org/scalafmt/docs/configuration.html) (ex: `.scalafmt.conf`) | | **SNAKEMAKE_SNAKEFMT_CONFIG_FILE** | `.snakefmt.toml` | Filename for [Snakemake configuration](https://github.com/snakemake/snakefmt#configuration) (ex: `pyproject.toml`, `.snakefmt.toml`) | | **SSL_CERT_SECRET** | `none` | SSL cert to add to the **Super-Linter** trust store. This is needed for users on `self-hosted` runners or need to inject the cert for security standards (ex. ${{ secrets.SSL_CERT }}) | | **SQL_CONFIG_FILE** | `.sql-config.json` | Filename for [SQL-Lint configuration](https://sql-lint.readthedocs.io/en/latest/files/configuration.html) (ex: `sql-config.json` , `.config.json`) | @@ -375,6 +377,7 @@ But if you wish to select or exclude specific linters, we give you full control | **VALIDATE_RUST_2015** | `true` | Flag to enable or disable the linting process of the Rust language. (edition: 2015) | | **VALIDATE_RUST_2018** | `true` | Flag to enable or disable the linting process of Rust language. (edition: 2018) | | **VALIDATE_RUST_CLIPPY** | `true` | Flag to enable or disable the clippy linting process of Rust language. | +| **VALIDATE_SCALAFMT_LINT** | `true` | Flag to enable or disable the linting process of Scala language. (Utilizing: scalafmt --test) | | **VALIDATE_SHELL_SHFMT** | `true` | Flag to enable or disable the linting process of Shell scripts. (Utilizing: shfmt) | | **VALIDATE_SNAKEMAKE_LINT** | `true` | Flag to enable or disable the linting process of Snakefiles. (Utilizing: snakemake --lint) | | **VALIDATE_SNAKEMAKE_SNAKEFMT** | `true` | Flag to enable or disable the linting process of Snakefiles. (Utilizing: snakefmt) | diff --git a/lib/functions/buildFileList.sh b/lib/functions/buildFileList.sh index 07304179..f3c8bdf1 100755 --- a/lib/functions/buildFileList.sh +++ b/lib/functions/buildFileList.sh @@ -687,6 +687,15 @@ function BuildFileList() { ############################################### FILE_ARRAY_RUST_CLIPPY+=("${FILE}") + ########################### + # Get the SCALA files # + ########################### + elif [ "${FILE_TYPE}" == "scala" ] || [ "${BASE_FILE}" == "??????" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_SCALAFMT+=("${FILE}") + ########################### # Get the SNAKEMAKE files # ########################### diff --git a/lib/linter.sh b/lib/linter.sh index 1dc72e14..169a2c61 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -162,6 +162,8 @@ R_FILE_NAME=".lintr" # shellcheck disable=SC2034 # Variable is referenced indirectly RUBY_FILE_NAME="${RUBY_CONFIG_FILE:-.ruby-lint.yml}" # shellcheck disable=SC2034 # Variable is referenced indirectly +SCALAFMT_FILE_NAME="${SCALAFMT_CONFIG_FILE:-.scalafmt.conf}" +# shellcheck disable=SC2034 # Variable is referenced indirectly SNAKEMAKE_SNAKEFMT_FILE_NAME="${SNAKEMAKE_SNAKEFMT_CONFIG_FILE:-.snakefmt.toml}" # shellcheck disable=SC2034 # Variable is referenced indirectly SUPPRESS_FILE_TYPE_WARN="${SUPPRESS_FILE_TYPE_WARN:-false}" @@ -218,9 +220,9 @@ LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CLANG_FORMAT' 'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' 'PROTOBUF' 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'PYTHON_ISORT' 'PYTHON_MYPY' 'R' 'RAKU' 'RUBY' 'RUST_2015' 'RUST_2018' - 'RUST_CLIPPY' 'SHELL_SHFMT' 'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' 'STATES' - 'SQL' 'SQLFLUFF' 'TEKTON' 'TERRAFORM_TFLINT' 'TERRAFORM_TERRASCAN' 'TERRAGRUNT' - 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML') + 'RUST_CLIPPY' 'SCALAFMT' 'SHELL_SHFMT' 'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' + 'STATES' 'SQL' 'SQLFLUFF' 'TEKTON' 'TERRAFORM_TFLINT' 'TERRAFORM_TERRASCAN' + 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML') ############################## # Linter command names array # @@ -281,6 +283,7 @@ LINTER_NAMES_ARRAY['RUBY']="rubocop" LINTER_NAMES_ARRAY['RUST_2015']="rustfmt" LINTER_NAMES_ARRAY['RUST_2018']="rustfmt" LINTER_NAMES_ARRAY['RUST_CLIPPY']="clippy" +LINTER_NAMES_ARRAY['SCALAFMT']="scalafmt" LINTER_NAMES_ARRAY['SHELL_SHFMT']="shfmt" LINTER_NAMES_ARRAY['SNAKEMAKE_LINT']="snakemake" LINTER_NAMES_ARRAY['SNAKEMAKE_SNAKEFMT']="snakefmt" @@ -899,6 +902,7 @@ LINTER_COMMANDS_ARRAY['RUBY']="rubocop -c ${RUBY_LINTER_RULES} --force-exclusion LINTER_COMMANDS_ARRAY['RUST_2015']="rustfmt --check --edition 2015" LINTER_COMMANDS_ARRAY['RUST_2018']="rustfmt --check --edition 2018" LINTER_COMMANDS_ARRAY['RUST_CLIPPY']="clippy" +LINTER_COMMANDS_ARRAY['SCALAFMT']="scalafmt --config ${SCALAFMT_LINTER_RULES} --test" LINTER_COMMANDS_ARRAY['SHELL_SHFMT']="shfmt -d" LINTER_COMMANDS_ARRAY['SNAKEMAKE_LINT']="snakemake --lint -s" LINTER_COMMANDS_ARRAY['SNAKEMAKE_SNAKEFMT']="snakefmt --config ${SNAKEMAKE_SNAKEFMT_LINTER_RULES} --check --compact-diff" diff --git a/test/inspec/super-linter/controls/super_linter.rb b/test/inspec/super-linter/controls/super_linter.rb index 6b225feb..37586afb 100644 --- a/test/inspec/super-linter/controls/super_linter.rb +++ b/test/inspec/super-linter/controls/super_linter.rb @@ -137,6 +137,7 @@ control "super-linter-installed-commands" do { linter_name: "raku", version_command: "raku --version | strings -n 8"}, { linter_name: "rubocop"}, { linter_name: "rustfmt"}, + { linter_name: "scalafmt"}, { linter_name: "shellcheck"}, { linter_name: "shfmt"}, { linter_name: "snakefmt"}, @@ -424,6 +425,7 @@ control "super-linter-validate-files" do "/action/lib/.automation/.python-black", "/action/lib/.automation/.python-lint", "/action/lib/.automation/.ruby-lint.yml", + "/action/lib/.automation/.scalafmt.conf", "/action/lib/.automation/.snakefmt.toml", "/action/lib/.automation/.sql-config.json", "/action/lib/.automation/.stylelintrc.json",