diff --git a/.automation/test/r/README.md b/.automation/test/r/README.md new file mode 100644 index 00000000..19e0042f --- /dev/null +++ b/.automation/test/r/README.md @@ -0,0 +1,13 @@ +# R Test Cases +This folder holds the test cases for **R**. + +## 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. \ No newline at end of file diff --git a/.automation/test/r/r_bad_1.r b/.automation/test/r/r_bad_1.r new file mode 100644 index 00000000..480eacd1 --- /dev/null +++ b/.automation/test/r/r_bad_1.r @@ -0,0 +1,43 @@ +# Each of the default linters should throw at least one lint on this file + +# assignment +# function_left_parentheses +# closed_curly +# commas +# paren_brace +f = function (x,y = 1){} + +# commented_code +# some <- commented("out code") + +# cyclocomp +# equals_na +# infix_spaces +# line_length +# object_length +# object_name +# object_usage +# open_curly +someComplicatedFunctionWithALongCameCaseName <- function(x) +{ + y <- 1 + if (1 > 2 && 2 > 3 && 3 > 4 && 4 > 5 && 5*10 > 6 && x == NA) {TRUE} else {FALSE} +} + +# pipe_continuation +# seq_linter +# spaces_inside +x <- 1:10 +x[ 2] +1:length(x) %>% lapply(function(x) x*2) %>% + head() + +# single_quotes +message('single_quotes') + +# spaces_left_parentheses +# trailing_whitespace +y <- 2 +(1:10) + +# trailing_blank_lines + diff --git a/.automation/test/r/r_good_1.r b/.automation/test/r/r_good_1.r new file mode 100644 index 00000000..e6c5c41d --- /dev/null +++ b/.automation/test/r/r_good_1.r @@ -0,0 +1,48 @@ +# Each of the default linters should throw at least one lint on this file + +# assignment +# function_left_parentheses +# closed_curly +# commas +# paren_brace +f <- function(x, y = 1) { + +} + +# commented_code + +# cyclocomp +# equals_na +# infix_spaces +# line_length +# object_length +# object_name +# object_usage +# open_curly +short_snake <- function(x) { + y <- 1 + y <- y^2 + if (1 > 2 && 5 * 10 > 6 && is.na(x)) { + TRUE + } else { + FALSE + } +} + +# pipe_continuation +# seq_linter +# spaces_inside +x <- 1:10 +x[2] +seq_len(x) %>% + lapply(function(x) x * 2) %>% + head() + +# single_quotes +message("single_quotes") + +# spaces_left_parentheses +# trailing_whitespace +y <- 2 + (1:10) + +# trailing_blank_lines \ No newline at end of file diff --git a/.github/linters/.lintr b/.github/linters/.lintr new file mode 100644 index 00000000..aac8e22e --- /dev/null +++ b/.github/linters/.lintr @@ -0,0 +1 @@ +linters: with_defaults(object_usage_linter = NULL) diff --git a/Dockerfile b/Dockerfile index 2018c288..ece69ab4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,7 @@ FROM yoheimuta/protolint:v0.26.0 as protolint FROM koalaman/shellcheck:v0.7.1 as shellcheck FROM wata727/tflint:0.19.0 as tflint FROM hadolint/hadolint:latest-alpine as dockerfile-lint +FROM assignuser/lintr-lib:latest as lintr-lib ################## # Get base image # @@ -60,7 +61,7 @@ RUN apk add --update --no-cache \ go \ icu-libs \ jq \ - libc-dev libxml2-utils \ + libc-dev libxml2-dev libxml2-utils \ make \ musl-dev \ npm nodejs-current \ @@ -69,9 +70,10 @@ RUN apk add --update --no-cache \ php7 php7-phar php7-json php7-mbstring php-xmlwriter \ php7-tokenizer php7-ctype php7-curl php7-dom php7-simplexml \ py3-setuptools \ + R \ readline-dev \ ruby ruby-dev ruby-bundler ruby-rdoc \ - gnupg + gnupg ######################################## # Copy dependencies files to container # @@ -240,6 +242,12 @@ RUN wget --tries=5 https://github.com/cvega/luarocks/archive/v3.3.1-super-linter RUN luarocks install luacheck +################# +# Install lintr # +################# +COPY --from=lintr-lib /usr/lib/R/library/ /home/r-library +RUN R -e "install.packages(list.dirs('/home/r-library',recursive = FALSE), repos = NULL, type = 'source')" + ########################################### # Load GitHub Env Vars for GitHub Actions # ########################################### @@ -294,6 +302,7 @@ ENV ACTIONS_RUNNER_DEBUG=${ACTIONS_RUNNER_DEBUG} \ VALIDATE_PYTHON=${VALIDATE_PYTHON} \ VALIDATE_PYTHON_PYLINT=${VALIDATE_PYTHON_PYLINT} \ VALIDATE_PYTHON_FLAKE8=${VALIDATE_PYTHON_FLAKE8} \ + VALIDATE_R=${VALIDATE_R} \ VALIDATE_RAKU=${VALIDATE_RAKU} \ VALIDATE_RUBY=${VALIDATE_RUBY} \ VALIDATE_STATES=${VALIDATE_STATES} \ diff --git a/README.md b/README.md index b2c4d095..d7fa9e22 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base | **PowerShell** | [PSScriptAnalyzer](https://github.com/PowerShell/Psscriptanalyzer) | | **Protocol Buffers** | [protolint](https://github.com/yoheimuta/protolint) | | **Python3** | [pylint](https://www.pylint.org/) [flake8](https://flake8.pycqa.org/en/latest/) | +| **R** | [lintr](https://github.com/jimhester/lintr) | | **Raku** | [raku](https://raku.org) | | **Ruby** | [RuboCop](https://github.com/rubocop-hq/rubocop) | | **Shell** | [Shellcheck](https://github.com/koalaman/shellcheck) | @@ -224,6 +225,7 @@ But if you wish to select or exclude specific linters, we give you full control | **VALIDATE_PYTHON_PYLINT** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: pylint) | | **VALIDATE_PYTHON_FLAKE8** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: flake8) | | **VALIDATE_POWERSHELL** | `true` | Flag to enable or disable the linting process of the Powershell language. | +| **VALIDATE_R** | `true` | Flag to enable or disable the linting process of the R language. | | **VALIDATE_RAKU** | `true` | Flag to enable or disable the linting process of the Raku language. | | **VALIDATE_RUBY** | `true` | Flag to enable or disable the linting process of the Ruby language. | | **VALIDATE_STATES** | `true` | Flag to enable or disable the linting process for AWS States Language. | diff --git a/TEMPLATES/.lintr b/TEMPLATES/.lintr new file mode 100644 index 00000000..aac8e22e --- /dev/null +++ b/TEMPLATES/.lintr @@ -0,0 +1 @@ +linters: with_defaults(object_usage_linter = NULL) diff --git a/docs/disabling-linters.md b/docs/disabling-linters.md index 13db1ee2..ec6b81b6 100644 --- a/docs/disabling-linters.md +++ b/docs/disabling-linters.md @@ -42,6 +42,7 @@ For some linters it is also possible to override rules on a case by case level w - [Protocol Buffers](#protocol-buffers) - [Python3 pylint](#python3-pylint) - [Python3 flake8](#python3-flake8) + - [R](#r) - [Raku](#raku) - [Ruby](#ruby) - [Shell](#shell) @@ -896,7 +897,40 @@ var = "terrible code down here..." ``` --- +## R +- [lintr](https://github.com/jimhester/lintr) + +### lintr Config file + +- `.github/linters/.lintr` +- You can pass multiple rules and overwrite default rules +- You can use either one `.lintr` file in the root of your repository and/or additonal `.lintr` files in subdirectories. When linting a file lintr will look for config files from the file location upwards and will use the closest one. +- Absolute paths for exclusions will not work due to the code being linted within the docker environment. Use paths relative to the `.lintr` file in which youare adding them. +- **Note:** The defaults adhere to the [tidyverse styleguide](https://style.tidyverse.org/) + +### lintr disable single line + +```r +1++1/3+2 # nolint +``` + +### lintr disable code block + +```r + # nolint start + hotGarbage = 1++1/3+2 + #a very long comment line + # nolint end +``` +### lintr disable entire file + +Add files to exclude into the config file as a list of filenames to exclude from linting. You can use a named item to exclude only certain lines from a file. Use paths relative to the location of the `.lintr` file. + +```r +exclusions: list("inst/doc/creating_linters.R" = 1, "inst/example/bad.R", "tests/testthat/exclusions-test") +``` +--- ## Raku - [raku](https://raku.org) diff --git a/lib/buildFileList.sh b/lib/buildFileList.sh index e565868d..760c0eec 100755 --- a/lib/buildFileList.sh +++ b/lib/buildFileList.sh @@ -355,6 +355,21 @@ function BuildFileList() { # Set the READ_ONLY_CHANGE_FLAG since this could be exec # ########################################################## READ_ONLY_CHANGE_FLAG=1 + #################### + # Get the R files # + #################### + elif [ "${FILE_TYPE}" == "r" ] || [ "${FILE_TYPE}" == "rmd" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_R+=("${FILE}") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 + ########################### + # Get the Terraform files # + ########################### ########################### # Get the Terraform files # ########################### diff --git a/lib/linter.sh b/lib/linter.sh index 09ae346c..36b27abd 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -107,6 +107,9 @@ PYTHON_PYLINT_FILE_NAME="${PYTHON_PYLINT_CONFIG_FILE:-.python-lint}" PYTHON_PYLINT_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_PYLINT_FILE_NAME}" # Path to the python lint rules PYTHON_FLAKE8_FILE_NAME="${PYTHON_FLAKE8_CONFIG_FILE:-.flake8}" # Name of the file PYTHON_FLAKE8_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PYTHON_FLAKE8_FILE_NAME}" # Path to the python lint rules +# R Vars +R_FILE_NAME='.lintr' # Name of the file +R_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${R_FILE_NAME}" # Path to the R lint rules # Ruby Vars RUBY_FILE_NAME="${RUBY_CONFIG_FILE:-.ruby-lint.yml}" # Name of the file RUBY_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${RUBY_FILE_NAME}" # Path to the ruby lint rules @@ -129,7 +132,7 @@ YAML_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${YAML_FILE_NAME}" # Path to the ya ####################################### LINTER_ARRAY=('ansible-lint' 'arm-ttk' 'asl-validator' 'cfn-lint' 'checkstyle' 'clj-kondo' 'coffeelint' 'dart' 'dockerfilelint' 'dotenv-linter' 'editorconfig-checker' 'eslint' 'flake8' 'golangci-lint' - 'hadolint' 'htmlhint' 'jsonlint' 'ktlint' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint' + 'hadolint' 'htmlhint' 'jsonlint' 'ktlint' 'lintr' 'lua' 'markdownlint' 'npm-groovy-lint' 'perl' 'protolint' 'pwsh' 'pylint' 'raku' 'rubocop' 'shellcheck' 'spectral' 'standard' 'stylelint' 'sql-lint' 'terrascan' 'tflint' 'xmllint' 'yamllint') @@ -140,7 +143,7 @@ LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'DART' 'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GO' 'GROOVY' 'HTML' 'JAVA' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' 'JSON' 'JSX' 'KOTLIN' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' - 'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'RAKU' 'RUBY' 'STATES' 'SQL' 'TERRAFORM' + 'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'R' 'RAKU' 'RUBY' 'STATES' 'SQL' 'TERRAFORM' 'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YAML') ############################################ @@ -197,6 +200,7 @@ VALIDATE_PHP_PSALM="${VALIDATE_PHP_PSALM}" # Boolean t VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language VALIDATE_PYTHON_PYLINT="${VALIDATE_PYTHON:-$VALIDATE_PYTHON_PYLINT}" # Boolean to validate language VALIDATE_PYTHON_FLAKE8="${VALIDATE_PYTHON_FLAKE8}" # Boolean to validate language +VALIDATE_R="${VALIDATE_R}" # Boolean to validate language VALIDATE_RAKU="${VALIDATE_RAKU}" # Boolean to validate language VALIDATE_RUBY="${VALIDATE_RUBY}" # Boolean to validate language VALIDATE_STATES="${VALIDATE_STATES}" # Boolean to validate language @@ -295,6 +299,7 @@ FILE_ARRAY_POWERSHELL=() # Array of files to check FILE_ARRAY_PROTOBUF=() # Array of files to check FILE_ARRAY_PYTHON_PYLINT=() # Array of files to check FILE_ARRAY_PYTHON_FLAKE8=() # Array of files to check +FILE_ARRAY_R=() # Array of files to check FILE_ARRAY_RAKU=() # Array of files to check FILE_ARRAY_RUBY=() # Array of files to check FILE_ARRAY_STATES=() # Array of files to check @@ -375,6 +380,8 @@ ERRORS_FOUND_PYTHON_PYLINT=0 # Count of errors found export ERRORS_FOUND_PYTHON_PYLINT # Workaround SC2034 ERRORS_FOUND_PYTHON_FLAKE8=0 # Count of errors found export ERRORS_FOUND_PYTHON_FLAKE8 # Workaround SC2034 +ERRORS_FOUND_R=0 # Count of errors found +export ERRORS_FOUND_R # Workaround SC2034 ERRORS_FOUND_RAKU=0 # Count of errors found export ERRORS_FOUND_RAKU # Workaround SC2034 ERRORS_FOUND_RUBY=0 # Count of errors found @@ -442,6 +449,11 @@ GetLinterVersions() { elif [[ ${LINTER} == "protolint" ]] || [[ ${LINTER} == "editorconfig-checker" ]]; then # Need specific command for Protolint and editorconfig-checker mapfile -t GET_VERSION_CMD < <(echo "--version not supported") + elif [[ ${LINTER} == "lintr" ]]; then + # Need specific command for lintr (--slave is deprecated in R 4.0 and replaced by --no-echo) + mapfile -t GET_VERSION_CMD < <(R --slave -e "r_ver <- R.Version()\$version.string; \ + lintr_ver <- packageVersion('lintr'); \ + glue::glue('lintr { lintr_ver } on { r_ver }')") else # Standard version command mapfile -t GET_VERSION_CMD < <("${LINTER}" --version 2>&1) @@ -1210,6 +1222,8 @@ GetLinterRules "POWERSHELL" GetLinterRules "PYTHON_PYLINT" # Get Python flake8 rules GetLinterRules "PYTHON_FLAKE8" +# Get R rules +GetLinterRules "R" # Get Ruby rules GetLinterRules "RUBY" # Get SQL rules @@ -1688,6 +1702,25 @@ if [ "${VALIDATE_PYTHON_FLAKE8}" == "true" ]; then LintCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config=${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "${FILE_ARRAY_PYTHON_FLAKE8[@]}" fi +################ +# R LINTING # +################ +if [ "${VALIDATE_R}" == "true" ]; then + ########################## + # Check for local config # + ########################## + if [ ! -f "${GITHUB_WORKSPACE}/.lintr" ]; then + info " " + info "No .lintr configuration file found, using defaults." + cp $R_LINTER_RULES "$GITHUB_WORKSPACE" + fi + + ###################### + # Lint the R files # + ###################### + LintCodebase "R" "lintr" "lintr::lint(File)" ".*\.\(r\|R\|Rmd\|rmd\)\$" "${FILE_ARRAY_R[@]}" +fi + ################ # RAKU LINTING # ################ diff --git a/lib/worker.sh b/lib/worker.sh index e94d9a06..52490f44 100755 --- a/lib/worker.sh +++ b/lib/worker.sh @@ -37,8 +37,12 @@ function LintCodebase() { ##################################### # Validate we have linter installed # ##################################### - VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) - + # Edgecase for Lintr as it is a Package for R + if [[ ${LINTER_NAME} == *"lintr"* ]]; then + VALIDATE_INSTALL_CMD=$(command -v "R" 2>&1) + else + VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) + fi ####################### # Load the error code # ####################### @@ -202,6 +206,23 @@ function LintCodebase() { cd "${GITHUB_WORKSPACE}" || exit ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 ) + ############################################################################### + # Corner case for R as we have to pass it to R # + ############################################################################### + elif [[ ${FILE_TYPE} == "R" ]]; then + ####################################### + # Lint the file with the updated path # + ####################################### + if [ ! -f "${DIR_NAME}/.lintr" ]; then + r_dir="${GITHUB_WORKSPACE}" + else + r_dir="${DIR_NAME}" + fi + LINT_CMD=$( + cd "$r_dir" || exit + R --slave -e "errors <- lintr::lint('$FILE');print(errors);quit(save = 'no', status = if (length(errors) > 0) 1 else 0)" 2>&1 + ) + #LINTER_COMMAND="lintr::lint('${FILE}')" else ################################ # Lint the file with the rules # @@ -285,7 +306,12 @@ function TestCodebase() { ##################################### # Validate we have linter installed # ##################################### - VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) + # Edgecase for Lintr as it is a Package for R + if [[ ${LINTER_NAME} == *"lintr"* ]]; then + VALIDATE_INSTALL_CMD=$(command -v "R" 2>&1) + else + VALIDATE_INSTALL_CMD=$(command -v "${LINTER_NAME}" 2>&1) + fi ####################### # Load the error code # @@ -417,6 +443,17 @@ function TestCodebase() { cd "${GITHUB_WORKSPACE}" || exit ${LINTER_COMMAND} --path "${DIR_NAME}" --files "$FILE_NAME" 2>&1 ) + ############################################################################### + # Corner case for R as we have to pass it to R # + ############################################################################### + elif [[ ${FILE_TYPE} == "R" ]]; then + ####################################### + # Lint the file with the updated path # + ####################################### + LINT_CMD=$( + cd "${GITHUB_WORKSPACE}" || exit + R --slave -e "errors <- lintr::lint('$FILE');print(errors);quit(save = 'no', status = if (length(errors) > 0) 1 else 0)" 2>&1 + ) else ################################ # Lint the file with the rules # @@ -600,6 +637,7 @@ function RunTestCases() { TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf" TestCodebase "PYTHON_PYLINT" "pylint" "pylint --rcfile ${PYTHON_PYLINT_LINTER_RULES}" ".*\.\(py\)\$" "python" TestCodebase "PYTHON_FLAKE8" "flake8" "flake8 --config ${PYTHON_FLAKE8_LINTER_RULES}" ".*\.\(py\)\$" "python" + TestCodebase "R" "lintr" "lintr::lint()" ".*\.\(r\|rmd\)\$" "r" TestCodebase "RAKU" "raku" "raku -c" ".*\.\(raku\|rakumod\|rakutest\|pm6\|pl6\|p6\)\$" "raku" TestCodebase "RUBY" "rubocop" "rubocop -c ${RUBY_LINTER_RULES}" ".*\.\(rb\)\$" "ruby" TestCodebase "STATES" "asl-validator" "asl-validator --json-path" ".*\.\(json\)\$" "states"