Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to documentation: Part-6 (o-p) #1664

Merged
merged 10 commits into from
Oct 11, 2022
2 changes: 1 addition & 1 deletion R/function_return_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#' return(e$val)
#' }
#' "
#' cat(code_lines)
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = function_return_linter()
Expand Down
2 changes: 1 addition & 1 deletion R/infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ infix_overload <- data.frame(
#' ab <- 1L
#' abcdef <- 2L
#' "
#' cat(code_lines)
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = infix_spaces_linter(allow_multiple_spaces = TRUE)
Expand Down
6 changes: 3 additions & 3 deletions R/object_name_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object_name_xpath <- local({
#'
#' lint(
#' text = "xYz <- 1L",
#' linters = object_name_linter(styles = "UPPERCASE")
#' linters = object_name_linter(styles = c("UPPERCASE", "lowercase"))
#' )
#'
#' lint(
Expand All @@ -74,8 +74,8 @@ object_name_xpath <- local({
#' )
#'
#' lint(
#' text = "my.var <- 1L",
#' linters = object_name_linter(styles = "dotted.case")
#' text = "my.var <- 1L; myvar <- 2L",
#' linters = object_name_linter(styles = c("dotted.case", "lowercase"))
#' )
#'
#' @evalRd rd_tags("object_name_linter")
Expand Down
20 changes: 20 additions & 0 deletions R/paste_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
#' linters = paste_linter()
#' )
#'
#' lint(
#' text = "paste0(c('a', 'b'), sep = ' ')",
#' linters = paste_linter()
#' )
#'
#' lint(
#' text = "paste0(rep('*', 10L), collapse='')",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use collapse = '' to be closer to the style guide

(I guess that means maybe we should switch to using " in the code too? WDYT?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's a good point.

I have changed this particular example here, but I will make a separate PR to change all the existing examples to make sure that they all follow the style guide.

#' linters = paste_linter()
#' )
#'
#' # okay
#' lint(
#' text = "paste0('a', 'b')",
Expand All @@ -55,6 +65,16 @@
#' linters = paste_linter(allow_to_string = TRUE)
#' )
#'
#' lint(
#' text = "paste(c('a', 'b'))",
#' linters = paste_linter()
#' )
#'
#' lint(
#' text = "strrep('*', 10L)",
#' linters = paste_linter()
#' )
#'
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
paste_linter <- function(allow_empty_sep = FALSE, allow_to_string = FALSE) {
Expand Down
10 changes: 5 additions & 5 deletions R/pipe_continuation_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#' @examples
#' # will produce lints
#' code_lines <- "1:3 %>%\n mean() %>% as.character()"
#' cat(code_lines)
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = pipe_continuation_linter()
#' )
#'
#' code_lines <- "1:3 |> mean() |>\n as.character()"
#' cat(code_lines)
#' code_lines <- "1:3 |> mean() |>\n as.character()"
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = pipe_continuation_linter()
Expand All @@ -27,7 +27,7 @@
#' )
#'
#' code_lines <- "1:3 %>%\n mean() %>%\n as.character()"
#' cat(code_lines)
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = pipe_continuation_linter()
Expand All @@ -39,7 +39,7 @@
#' )
#'
#' code_lines <- "1:3 |>\n mean() |>\n as.character()"
#' cat(code_lines)
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = pipe_continuation_linter()
Expand Down
2 changes: 1 addition & 1 deletion man/function_return_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/infix_spaces_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/object_name_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/paste_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/pipe_continuation_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions tests/testthat/test-paste_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ test_that("paste_linter catches use of paste0 with sep=", {
})

test_that("paste_linter skips allowed usages for strrep()", {
expect_lint("paste(x, collapse = '')", NULL, paste_linter())
expect_lint("paste(rep('*', 10), collapse = '+')", NULL, paste_linter())
expect_lint("paste(rep(c('a', 'b'), 2), collapse = '')", NULL, paste_linter())
expect_lint("paste0(rep('a', 2), 'b', collapse = '')", NULL, paste_linter())
linter <- paste_linter()

expect_lint("paste(x, collapse = '')", NULL, linter)
expect_lint("paste(rep('*', 10), collapse = '+')", NULL, linter)
expect_lint("paste(rep(c('a', 'b'), 2), collapse = '')", NULL, linter)
expect_lint("paste0(rep('a', 2), 'b', collapse = '')", NULL, linter)
# no collapse
expect_lint("paste(rep('*', 10))", NULL, paste_linter())
expect_lint("paste(rep('*', 10))", NULL, linter)
# combined before aggregating
expect_lint("paste(rep('*', 10), rep('x', 10), collapse = '')", NULL, paste_linter())
expect_lint("paste(rep('*', 10), rep('x', 10), collapse = '')", NULL, linter)
})

test_that("paste_linter blocks simple disallowed usages", {
Expand Down