lint-r-code.R 6.31 KB
Newer Older
1
loadNamespace("lintr")
2
3
4
5

args <- commandArgs(
    trailingOnly = TRUE
)
6
SOURCE_DIR <- args[[1L]]
7
8
9

FILES_TO_LINT <- list.files(
    path = SOURCE_DIR
10
    , pattern = "\\.r$|\\.rmd$"
11
12
13
14
15
16
17
    , all.files = TRUE
    , ignore.case = TRUE
    , full.names = TRUE
    , recursive = TRUE
    , include.dirs = FALSE
)

18
19
20
21
22
23
24
25
26
27
28
29
# text to use for pipe operators from packages like 'magrittr'
pipe_text <- paste0(
    "For consistency and the sake of being explicit, this project's code "
    , "does not use the pipe operator."
)

# text to use for functions that should only be called interactively
interactive_text <- paste0(
    "Functions like '?', 'help', and 'install.packages()' should only be used "
    , "interactively, not in package code."
)

30
LINTERS_TO_USE <- list(
31
    "absolute_path"          = lintr::absolute_path_linter()
32
33
    , "any_duplicated"       = lintr::any_duplicated_linter()
    , "any_is_na"            = lintr::any_is_na_linter()
34
    , "assignment"           = lintr::assignment_linter()
35
    , "backport"             = lintr::backport_linter()
36
    , "boolean_arithmetic"   = lintr::boolean_arithmetic_linter()
37
    , "braces"               = lintr::brace_linter()
38
    , "class_equals"         = lintr::class_equals_linter()
39
    , "commas"               = lintr::commas_linter()
40
    , "conjunct_test"        = lintr::conjunct_test_linter()
41
    , "duplicate_argument"   = lintr::duplicate_argument_linter()
42
    , "empty_assignment"     = lintr::empty_assignment_linter()
43
    , "equals_na"            = lintr::equals_na_linter()
44
    , "fixed_regex"          = lintr::fixed_regex_linter()
45
    , "for_loop_index"       = lintr::for_loop_index_linter()
46
    , "function_left"        = lintr::function_left_parentheses_linter()
47
48
    , "function_return"      = lintr::function_return_linter()
    , "implicit_assignment"  = lintr::implicit_assignment_linter()
49
50
    , "implicit_integers"    = lintr::implicit_integer_linter()
    , "infix_spaces"         = lintr::infix_spaces_linter()
51
    , "inner_combine"        = lintr::inner_combine_linter()
52
53
    , "is_numeric"           = lintr::is_numeric_linter()
    , "lengths"              = lintr::lengths_linter()
54
55
    , "length_levels"        = lintr::length_levels_linter()
    , "length_test"          = lintr::length_test_linter()
56
    , "line_length"          = lintr::line_length_linter(length = 120L)
57
    , "literal_coercion"     = lintr::literal_coercion_linter()
58
    , "matrix"               = lintr::matrix_apply_linter()
59
    , "missing_argument"     = lintr::missing_argument_linter()
60
    , "non_portable_path"    = lintr::nonportable_path_linter()
61
62
63
    , "numeric_leading_zero" = lintr::numeric_leading_zero_linter()
    , "outer_negation"       = lintr::outer_negation_linter()
    , "package_hooks"        = lintr::package_hooks_linter()
64
    , "paren_body"           = lintr::paren_body_linter()
65
    , "paste"                = lintr::paste_linter()
66
67
    , "quotes"               = lintr::quotes_linter()
    , "redundant_equals"     = lintr::redundant_equals_linter()
68
    , "regex_subset"         = lintr::regex_subset_linter()
69
    , "routine_registration" = lintr::routine_registration_linter()
70
    , "scalar_in"            = lintr::scalar_in_linter()
71
72
73
74
    , "semicolon"            = lintr::semicolon_linter()
    , "seq"                  = lintr::seq_linter()
    , "spaces_inside"        = lintr::spaces_inside_linter()
    , "spaces_left_parens"   = lintr::spaces_left_parentheses_linter()
75
    , "sprintf"              = lintr::sprintf_linter()
76
    , "string_boundary"      = lintr::string_boundary_linter()
77
    , "todo_comments"        = lintr::todo_comment_linter(c("todo", "fixme", "to-do"))
78
79
80
    , "trailing_blank"       = lintr::trailing_blank_lines_linter()
    , "trailing_white"       = lintr::trailing_whitespace_linter()
    , "true_false"           = lintr::T_and_F_symbol_linter()
81
82
    , "undesirable_function" = lintr::undesirable_function_linter(
        fun = c(
83
            "cbind" = paste0(
84
85
86
                "cbind is an unsafe way to build up a data frame. merge() or direct "
                , "column assignment is preferred."
            )
87
88
            , "dyn.load" = "Directly loading or unloading .dll or .so files in package code should not be necessary."
            , "dyn.unload" = "Directly loading or unloading .dll or .so files in package code should not be necessary."
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
            , "help" = interactive_text
            , "ifelse" = "The use of ifelse() is dangerous because it will silently allow mixing types."
            , "install.packages" = interactive_text
            , "is.list" = paste0(
                "This project uses data.table, and is.list(x) is TRUE for a data.table. "
                , "identical(class(x), 'list') is a safer way to check that something is an R list object."
            )
            , "rbind" = "data.table::rbindlist() is faster and safer than rbind(), and is preferred in this project."
            , "require" = paste0(
                "library() is preferred to require() because it will raise an error immediately "
                , "if a package is missing."
            )
        )
    )
    , "undesirable_operator" = lintr::undesirable_operator_linter(
        op = c(
            "%>%" = pipe_text
            , "%.%" = pipe_text
            , "%..%" = pipe_text
108
            , "|>" = pipe_text
109
110
111
112
            , "?" = interactive_text
            , "??" = interactive_text
        )
    )
113
114
115
    , "unnecessary_concatenation" = lintr::unnecessary_concatenation_linter()
    , "unnecessary_lambda"        = lintr::unnecessary_lambda_linter()
    , "unreachable_code"          = lintr::unreachable_code_linter()
116
    , "unused_import"             = lintr::unused_import_linter()
117
118
    , "vector_logic"              = lintr::vector_logic_linter()
    , "whitespace"                = lintr::whitespace_linter()
119
120
)

121
noquote(paste0(length(FILES_TO_LINT), " R files need linting"))
122

123
results <- NULL
124

125
for (r_file in FILES_TO_LINT) {
126
127
128
129
130
131
132

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

133
134
135
136
137
138
139
140
    print(
        sprintf(
            "Found %i linting errors in %s"
            , length(this_result)
            , r_file
        )
        , quote = FALSE
    )
141
142
143
144
145
146
147

    results <- c(results, this_result)

}

issues_found <- length(results)

148
149
noquote(paste0("Total linting issues found: ", issues_found))

150
if (issues_found > 0L) {
151
152
153
154
    print(results)
}

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