Skip to content

Commit

Permalink
replace internal 'trailing_semicolon_linter' with lintr version for #171
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Sep 25, 2024
1 parent b215780 commit a8349ae
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 45 deletions.
4 changes: 2 additions & 2 deletions R/chk_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CHECKS$lintr_line_length_linter <- make_check(
}
)

CHECKS$lintr_trailing_semicolon_linter <- make_check(
CHECKS$lintr_semicolon_linter <- make_check(

description = "No trailing semicolons",
tags = c("style", "lintr"),
Expand All @@ -63,7 +63,7 @@ CHECKS$lintr_trailing_semicolon_linter <- make_check(
forbid them",

check = function(state) {
get_lintr_state(state, "trailing_semicolon_linter")
get_lintr_state(state, "semicolon_linter")
}
)

Expand Down
37 changes: 0 additions & 37 deletions R/my_linters.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,6 @@

#' @importFrom lintr Lint

trailing_semicolon_linter <- function() lintr::Linter(function(source_file) {

allsc <- which(source_file$parsed_content$token == "';'")

if (!length(allsc)) return(list())

## Check that it is the last token in the line
## Note that we need to restrict the search to terminal
## nodes, because parse is buggy and sometimes reports false,
## too large end positions for non-terminal nodes.
badsc <- Filter(
x = allsc,
f = function(line) {
with(
source_file$parsed_content,
all(! terminal | line1 != line1[line] | col2 <= col2[line])
)
}
)

lapply(
badsc,
function(line) {
parsed <- source_file$parsed_content[line, ]
Lint(
filename = source_file$filename,
line_number = parsed$line1,
column_number = parsed$col1,
type = "style",
message = "Avoid trailing semicolons, they are not needed.",
line = source_file$lines[as.character(parsed$line1)],
ranges = list(c(parsed$col1, parsed$col2))
)
}
)
})

#' Find dangerous 1:x expressions
#'
#' Find occurrences of \code{1:length(x)}, \code{1:nrow(x)},
Expand Down
2 changes: 1 addition & 1 deletion R/prep_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
linters_to_lint <- list(
assignment_linter = lintr::assignment_linter(),
line_length_linter = lintr::line_length_linter(80),
trailing_semicolon_linter = trailing_semicolon_linter(),
semicolon_linter = lintr::semicolon_linter(allow_compound = TRUE),
attach_detach_linter = attach_detach_linter(),
setwd_linter = setwd_linter(),
sapply_linter = sapply_linter(),
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-lintr.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# "lintr_trailing_semicolon_linter" fails in bad1
# "lintr_semicolon_linter" fails in bad1
# "lintr_attach_detach_linter" fails in bad2
# "lintr_setwd_linter" fails in bad2
# "lintr_sapply_linter" fails in bad2
Expand All @@ -10,7 +10,7 @@
# "lintr_line_length_linter"

gp_lintrs <- c("lintr_assignment_linter", "lintr_line_length_linter",
"lintr_trailing_semicolon_linter",
"lintr_semicolon_linter",
"lintr_attach_detach_linter", "lintr_setwd_linter",
"lintr_sapply_linter", "lintr_library_require_linter",
"lintr_seq_linter")
Expand Down Expand Up @@ -41,10 +41,10 @@ test_that("lintr_line_length_linter", {
# TODO expectation/example where the check fails

})
test_that("lintr_trailing_semicolon_linter", {
test_that("lintr_semicolon_linter", {

expect_true(get_result(res_bad2, "lintr_trailing_semicolon_linter"))
expect_false(get_result(res_bad1, "lintr_trailing_semicolon_linter"))
expect_true(get_result(res_bad2, "lintr_semicolon_linter"))
expect_false(get_result(res_bad1, "lintr_semicolon_linter"))

})
test_that("lintr_attach_detach_linter", {
Expand Down

0 comments on commit a8349ae

Please sign in to comment.