Skip to content

Commit 60ab27f

Browse files
Merge branch 'master' into ns-hooks-redux
2 parents b9587dd + e983233 commit 60ab27f

21 files changed

+224
-250
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
.idea
66
bad.R
77
script.R
8+
9+
*.Rcheck
10+
lintr_*.tar.gz

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* `object_name_linter()` now excludes special R hook functions such as `.onLoad` (#500, #614, @AshesITR and @michaelchirico)
2020
* `equals_na_linter()` now lints `x != NA` and `NA == x`, and skips usages in comments (#545, @michaelchirico)
2121
* Malformed Rmd files now cause a lint instead of an error (#571, #575, @AshesITR)
22+
* `object_name_linter()` gains a new default style, `"symbols"`, which won't lint all-symbol object names (in particular, that means operator names like `%+%` are skipped; #615, @michaelchirico)
2223

2324
# lintr 2.0.1
2425

R/actions.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ in_github_actions <- function() {
55
# Output logging commands for any lints found
66
github_actions_log_lints <- function(lints) {
77
for (x in lints) {
8-
cat(
9-
sprintf("::warning file=%s,line=%s,col=%s::%s\n", x$filename, x$line_number, x$column_number, x$message),
10-
sep = ""
8+
file_line_col <- sprintf(
9+
"file=%s,line=%s,col=%s", x$filename, x$line_number, x$column_number
1110
)
11+
cat(sprintf(
12+
"::warning %s::%s,%s\n",
13+
file_line_col, file_line_col, x$message
14+
), sep = "")
1215
}
1316
}

R/declared_functions.R

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
# Find all normal function declarations
2-
# TODO: setMethod() calls not included
3-
declared_functions <- function(x) {
4-
5-
xpath <- paste0(
6-
# Top level expression which
7-
"/exprlist/expr",
8-
9-
# Assigns to a symbol
10-
"[./LEFT_ASSIGN|EQ_ASSIGN]",
11-
"[./expr[FUNCTION]]",
12-
"[./expr/SYMBOL]",
13-
14-
# Retrieve assigned name of the function
15-
"/expr/SYMBOL/text()")
16-
17-
as.character(xml2::xml_find_all(x, xpath))
18-
}
19-
201
declared_s3_generics <- function(x) {
212
xpath <- paste0(
223
# Top level expression which

R/expect_lint.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
4545

4646
if (is.null(file)) {
4747
file <- tempfile()
48-
on.exit(unlink(file))
48+
on.exit(unlink(file), add = TRUE)
4949
writeLines(content, con = file, sep = "\n")
5050
}
5151

R/lint.R

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ lint <- function(filename, linters = NULL, cache = FALSE, ..., parse_settings =
2828
if (inline_data) {
2929
content <- gsub("\n$", "", filename)
3030
filename <- tempfile()
31-
on.exit(unlink(filename))
31+
on.exit(unlink(filename), add = TRUE)
3232
writeLines(text = content, con = filename, sep = "\n")
3333
}
3434

@@ -378,11 +378,23 @@ rstudio_source_markers <- function(lints) {
378378
})
379379

380380
# request source markers
381-
rstudioapi::callFun("sourceMarkers",
382-
name = "lintr",
383-
markers = markers,
384-
basePath = package_path,
385-
autoSelect = "first")
381+
out <- rstudioapi::callFun(
382+
"sourceMarkers",
383+
name = "lintr",
384+
markers = markers,
385+
basePath = package_path,
386+
autoSelect = "first"
387+
)
388+
389+
# workaround to avoid focusing an empty Markers pane
390+
# when possible, better solution is to delete the "lintr" source marker list
391+
# https://github.com/rstudio/rstudioapi/issues/209
392+
if (length(lints) == 0) {
393+
Sys.sleep(0.1)
394+
rstudioapi::executeCommand("activateConsole")
395+
}
396+
397+
out
386398
}
387399

388400
#' Checkstyle Report for lint results

R/methods.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ markdown <- function(x, info, ...) {
5252

5353
#' @export
5454
print.lints <- function(x, ...) {
55+
rstudio_source_markers <- getOption("lintr.rstudio_source_markers", TRUE) &&
56+
rstudioapi::hasFun("sourceMarkers")
57+
5558
if (length(x)) {
56-
if (getOption("lintr.rstudio_source_markers", TRUE) &&
57-
rstudioapi::hasFun("sourceMarkers")) {
59+
inline_data <- x[[1]][["filename"]] == "<text>"
60+
if (!inline_data && rstudio_source_markers) {
5861
rstudio_source_markers(x)
5962
} else if (in_github_actions()) {
6063
github_actions_log_lints(x)
@@ -78,8 +81,7 @@ print.lints <- function(x, ...) {
7881
if (isTRUE(settings$error_on_lint)) {
7982
quit("no", 31, FALSE)
8083
}
81-
} else if (getOption("lintr.rstudio_source_markers", TRUE) &&
82-
rstudioapi::hasFun("sourceMarkers")) {
84+
} else if (rstudio_source_markers) {
8385
# Empty lints: clear RStudio source markers
8486
rstudio_source_markers(x)
8587
}

R/object_name_linters.R

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' \Sexpr[stage=render, results=rd]{lintr:::regexes_rd}. A name should
44
#' match at least one of these styles.
55
#' @export
6-
object_name_linter <- function(styles = "snake_case") {
6+
object_name_linter <- function(styles = c("snake_case", "symbols")) {
77

88
.or_string <- function(xs) {
99
# returns "<S> or <T>"
@@ -13,7 +13,7 @@ object_name_linter <- function(styles = "snake_case") {
1313
if (len <= 1) {
1414
return(xs)
1515
}
16-
comma_sepd_prefix <- paste(xs[-len], collapse = ", ")
16+
comma_sepd_prefix <- toString(xs[-len])
1717
paste(comma_sepd_prefix, "or", xs[len])
1818
}
1919

@@ -24,10 +24,9 @@ object_name_linter <- function(styles = "snake_case") {
2424
)
2525

2626
function(source_file) {
27-
x <- global_xml_parsed_content(source_file)
28-
if (is.null(x)) {
29-
return()
30-
}
27+
if (is.null(source_file$full_xml_parsed_content)) return(list())
28+
29+
xml <- source_file$full_xml_parsed_content
3130

3231
xpath <- paste0(
3332
# Left hand assignments
@@ -46,14 +45,14 @@ object_name_linter <- function(styles = "snake_case") {
4645
"//SYMBOL_FORMALS"
4746
)
4847

49-
assignments <- xml2::xml_find_all(x, xpath)
48+
assignments <- xml2::xml_find_all(xml, xpath)
5049

5150
# Retrieve assigned name
5251
nms <- strip_names(
5352
as.character(xml2::xml_find_first(assignments, "./text()")))
5453

5554
generics <- strip_names(c(
56-
declared_s3_generics(x),
55+
declared_s3_generics(xml),
5756
namespace_imports()$fun,
5857
names(.knownS3Generics),
5958
.S3PrimitiveGenerics, ls(baseenv())))
@@ -105,7 +104,6 @@ strip_names <- function(x) {
105104
x
106105
}
107106

108-
109107
object_lint2 <- function(expr, source_file, message, type) {
110108
symbol <- xml2::as_list(expr)
111109
Lint(
@@ -281,53 +279,21 @@ object_lint <- function(source_file, token, message, type) {
281279
}
282280

283281

284-
object_name_linter_old <- function(style = "snake_case") {
285-
make_object_linter(
286-
function(source_file, token) {
287-
name <- unquote(token[["text"]])
288-
if (!any(matches_styles(name, style))) {
289-
object_lint(
290-
source_file,
291-
token,
292-
sprintf("Variable and function name style should be %s.", paste(style, collapse = " or ")),
293-
"object_name_linter"
294-
)
295-
}
296-
}
297-
)
298-
}
299-
300-
301282
loweralnum <- rex(one_of(lower, digit))
302283
upperalnum <- rex(one_of(upper, digit))
303284

304285
style_regexes <- list(
305-
"CamelCase" = rex(start, maybe("."), upper, zero_or_more(alnum), end),
306-
"camelCase" = rex(start, maybe("."), lower, zero_or_more(alnum), end),
307-
"snake_case" = rex(start, maybe("."), some_of(lower, digit), any_of("_", lower, digit), end),
308-
"SNAKE_CASE" = rex(start, maybe("."), some_of(upper, digit), any_of("_", upper, digit), end),
309-
"dotted.case" = rex(start, maybe("."), one_or_more(loweralnum), zero_or_more(dot, one_or_more(loweralnum)), end),
286+
"symbols" = rex(start, maybe("."), zero_or_more(none_of(alnum)), end),
287+
"CamelCase" = rex(start, maybe("."), upper, zero_or_more(alnum), end),
288+
"camelCase" = rex(start, maybe("."), lower, zero_or_more(alnum), end),
289+
"snake_case" = rex(start, maybe("."), some_of(lower, digit), any_of("_", lower, digit), end),
290+
"SNAKE_CASE" = rex(start, maybe("."), some_of(upper, digit), any_of("_", upper, digit), end),
291+
"dotted.case" = rex(start, maybe("."), one_or_more(loweralnum), zero_or_more(dot, one_or_more(loweralnum)), end),
310292
"lowercase" = rex(start, maybe("."), one_or_more(loweralnum), end),
311293
"UPPERCASE" = rex(start, maybe("."), one_or_more(upperalnum), end)
312294
)
313295

314-
regexes_rd <- paste0(collapse = ", ", paste0("\\sQuote{", names(style_regexes), "}"))
315-
316-
matches_styles <- function(name, styles=names(style_regexes)) {
317-
invalids <- paste(styles[!styles %in% names(style_regexes)], collapse=", ")
318-
if (nzchar(invalids)) {
319-
valids <- paste(names(style_regexes), collapse=", ")
320-
stop(sprintf("Invalid style(s) requested: %s\nValid styles are: %s\n", invalids, valids))
321-
}
322-
name <- re_substitutes(name, rex(start, one_or_more(dot)), "") # remove leading dots
323-
vapply(
324-
style_regexes[styles],
325-
re_matches,
326-
logical(1L),
327-
data=name
328-
)
329-
}
330-
296+
regexes_rd <- toString(paste0("\\sQuote{", names(style_regexes), "}"))
331297

332298
#' @describeIn linters check that object names are not too long.
333299
#' @export

R/tree-utils.R

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,6 @@ parents <- function(data, id, levels = Inf, simplify = TRUE) {
7373
}
7474
}
7575

76-
family <- function(data, id, parent_levels = 1L, child_levels = Inf) {
77-
parents <- parents(data, id, parent_levels)
78-
c(parents,
79-
unlist(lapply(
80-
parents,
81-
children,
82-
data = data,
83-
levels = child_levels)
84-
)
85-
)
86-
}
87-
8876
siblings <- function(data, id, child_levels = Inf) {
8977
parents <- parents(data, id, 1L)
9078
res <- unlist(lapply(

R/utils.R

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ auto_names <- function(x) {
7474
nms
7575
}
7676

77-
blank_text <- function(s, re, shift_start = 0, shift_end = 0) {
78-
m <- gregexpr(re, s, perl = TRUE)
79-
regmatches(s, m) <- lapply(regmatches(s, m),
80-
quoted_blanks,
81-
shift_start = shift_start,
82-
shift_end = shift_end)
83-
84-
s
85-
}
86-
8777
quoted_blanks <- function(matches, shift_start = 0, shift_end = 0) {
8878
lengths <- nchar(matches)
8979
blanks <- vapply(Map(rep.int,
@@ -101,15 +91,6 @@ names2 <- function(x) {
10191
names(x) %||% rep("", length(x))
10292
}
10393

104-
recursive_ls <- function(env) {
105-
if (parent.env(env) %!=% emptyenv()) {
106-
c(ls(envir = env), recursive_ls(parent.env(env)))
107-
}
108-
else {
109-
ls(envir = env)
110-
}
111-
}
112-
11394
safe_parse_to_xml <- function(parsed_content) {
11495
if (is.null(parsed_content)) return(NULL)
11596
tryCatch(xml2::read_xml(xmlparsedata::xml_parse_data(parsed_content)), error = function(e) NULL)
@@ -172,7 +153,6 @@ try_silently <- function(expr) {
172153
}
173154

174155
viapply <- function(x, ...) vapply(x, ..., FUN.VALUE = integer(1))
175-
vcapply <- function(x, ...) vapply(x, ..., FUN.VALUE = character(1))
176156

177157
# imitate sQuote(x, q) [requires R>=3.6]
178158
quote_wrap <- function(x, q) paste0(q, x, q)

0 commit comments

Comments
 (0)