This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
lint-r-code.R
118 lines (102 loc) · 3.97 KB
/
lint-r-code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
library(lintr)
args <- commandArgs(
trailingOnly = TRUE
)
SOURCE_DIR <- args[[1L]]
FILES_TO_LINT <- list.files(
path = SOURCE_DIR
, pattern = "\\.r$"
, all.files = TRUE
, ignore.case = TRUE
, full.names = TRUE
, recursive = TRUE
, include.dirs = FALSE
)
# 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."
)
LINTERS_TO_USE <- list(
"absolute_path" = lintr::absolute_path_linter
, "assignment" = lintr::assignment_linter
, "closed_curly" = lintr::closed_curly_linter
, "commas" = lintr::commas_linter
, "equals_na" = lintr::equals_na_linter
, "function_left" = lintr::function_left_parentheses_linter
, "implicit_integers" = lintr::implicit_integer_linter
, "infix_spaces" = lintr::infix_spaces_linter
, "long_lines" = lintr::line_length_linter(length = 120L)
, "no_tabs" = lintr::no_tab_linter
, "non_portable_path" = lintr::nonportable_path_linter
, "object_usage" = lintr::object_usage_linter
, "open_curly" = lintr::open_curly_linter
, "paren_brace_linter" = lintr::paren_brace_linter
, "semicolon" = lintr::semicolon_terminator_linter
, "seq" = lintr::seq_linter
, "single_quotes" = lintr::single_quotes_linter
, "spaces_inside" = lintr::spaces_inside_linter
, "spaces_left_parens" = lintr::spaces_left_parentheses_linter
, "todo_comments" = lintr::todo_comment_linter
, "trailing_blank" = lintr::trailing_blank_lines_linter
, "trailing_white" = lintr::trailing_whitespace_linter
, "true_false" = lintr::T_and_F_symbol_linter
, "undesirable_function" = lintr::undesirable_function_linter(
fun = c(
"cbind" = paste0(
"cbind is an unsafe way to build up a data frame. merge() or direct "
, "column assignment is preferred."
)
, "dyn.load" = "Directly loading/unloading .dll/.so files in package code should not be necessary."
, "dyn.unload" = "Directly loading/unloading .dll/.so files in package code should not be necessary."
, "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
, "?" = interactive_text
, "??" = interactive_text
)
)
, "unneeded_concatenation" = lintr::unneeded_concatenation_linter
)
cat(sprintf("Found %i R files to lint\n", length(FILES_TO_LINT)))
results <- NULL
for (r_file in FILES_TO_LINT) {
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)
if (issues_found > 0L) {
cat("\n")
print(results)
}
quit(save = "no", status = issues_found)