lint_r_code.R 1.92 KB
Newer Older
1
2
3
4
5
6

library(lintr)

args <- commandArgs(
    trailingOnly = TRUE
)
7
SOURCE_DIR <- args[[1L]]
8
9
10
11
12
13
14
15
16
17
18
19

FILES_TO_LINT <- list.files(
    path = SOURCE_DIR
    , pattern = "\\.r$"
    , all.files = TRUE
    , ignore.case = TRUE
    , full.names = TRUE
    , recursive = TRUE
    , include.dirs = FALSE
)

LINTERS_TO_USE <- list(
20
21
22
23
24
25
26
    "assignment" = lintr::assignment_linter
    , "closed_curly" = lintr::closed_curly_linter
    , "equals_na" = lintr::equals_na_linter
    , "function_left" = lintr::function_left_parentheses_linter
    , "commas" = lintr::commas_linter
    , "concatenation" = lintr::unneeded_concatenation_linter
    , "implicit_integers" = lintr::implicit_integer_linter
27
    , "infix_spaces" = lintr::infix_spaces_linter
28
    , "long_lines" = lintr::line_length_linter(length = 120L)
29
30
    , "tabs" = lintr::no_tab_linter
    , "open_curly" = lintr::open_curly_linter
31
32
33
34
    , "paren_brace_linter" = lintr::paren_brace_linter
    , "semicolon" = lintr::semicolon_terminator_linter
    , "seq" = lintr::seq_linter
    , "single_quotes" = lintr::single_quotes_linter
35
36
    , "spaces_inside" = lintr::spaces_inside_linter
    , "spaces_left_parens" = lintr::spaces_left_parentheses_linter
37
    , "todo_comments" = lintr::todo_comment_linter(c("todo", "fixme", "to-do"))
38
39
    , "trailing_blank" = lintr::trailing_blank_lines_linter
    , "trailing_white" = lintr::trailing_whitespace_linter
40
    , "true_false" = lintr::T_and_F_symbol_linter
41
42
43
44
)

cat(sprintf("Found %i R files to lint\n", length(FILES_TO_LINT)))

45
results <- NULL
46

47
for (r_file in FILES_TO_LINT) {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    this_result <- lintr::lint(
        filename = r_file
        , linters = LINTERS_TO_USE
        , cache = FALSE
    )

    cat(sprintf(
        "Found %i linting errors in %s\n"
        , length(this_result)
        , r_file
    ))

    results <- c(results, this_result)

}

issues_found <- length(results)

67
if (issues_found > 0L) {
68
69
70
71
72
    cat("\n")
    print(results)
}

quit(save = "no", status = issues_found)