Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* `object_usage_linter()` lints missing packages that may cause false positives (#2872, @AshesITR)
* New argument `include_s4_slots` for the `xml_find_function_calls()` entry in the `get_source_expressions()` to govern whether calls of the form `s4Obj@fun()` are included in the result (#2820, @MichaelChirico).
* `sprintf_linter()` lints `sprintf()` and `gettextf()` calls when a constant string is passed to `fmt` (#2894, @Bisaloo).
* `use_lintr()` adds the created `.lintr` file to the `.Rbuildignore` if run in a package (#1805, @MEO265).

### New linters

Expand Down Expand Up @@ -162,6 +163,7 @@ files in Windows (#2882, @Bisaloo).
* `lint()` and friends emit a message if no lints are found (#2643, @IndrajeetPatil).
* `commented_code_linter()` can detect commented code that ends with a pipe (#2671, @jcken95)


Copy link
Collaborator

Choose a reason for hiding this comment

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

Superfluous change

### New linters

* `condition_call_linter()` for ensuring consistent use of `call.` in `warning()` and `stop()`. The default `call. = FALSE` follows the tidyverse guidance of not displaying the call (#2226, @Bisaloo)
Expand Down
34 changes: 33 additions & 1 deletion R/use_lintr.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Use lintr in your project
#'
#' Create a minimal lintr config file as a starting point for customization
#' Create a minimal lintr config file as a starting point for customization and add it to the .Rbuildignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be add it to .Rbuildignore, without a "the". Same in other places.

#'
#' @param path Path to project root, where a `.lintr` file should be created.
#' If the `.lintr` file already exists, an error will be thrown.
Expand Down Expand Up @@ -42,5 +42,37 @@
)
)
write.dcf(the_config, config_file, width = Inf)

if (file.exists(file.path(path, "DESCRIPTION"))) {
# Some OS can only normalize a path if the associated file or folder exists, so the path needs to be re-normalized
tryCatch({
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand this part.
What do these systems return with normalizePath("foo", mustWork = FALSE) if foo doesn't exist?

pkg_path <- normalizePath(path, mustWork = TRUE, winslash = "/")

Check warning on line 49 in R/use_lintr.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_lintr.R,line=49,col=19,[undesirable_function_name_linter] Avoid undesirable function "normalizePath". As an alternative, use normalize_path().
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = TRUE, winslash = "/")

Check warning on line 50 in R/use_lintr.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_lintr.R,line=50,col=22,[undesirable_function_name_linter] Avoid undesirable function "normalizePath". As an alternative, use normalize_path().
}, error = function(e) {
stop("No entry could be added to the .Rbuildignore.", call. = FALSE)

Check warning on line 52 in R/use_lintr.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_lintr.R,line=52,col=7,[undesirable_function_call_linter] Avoid undesirable function "stop". As an alternative, use cli::cli_abort().
Copy link
Collaborator

Choose a reason for hiding this comment

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

This sounds like a warning is in order. The main side effect was caused at this point anyway.

})
# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
if (startsWith(config_file, prefix = pkg_path)) {
# Skip a extra character for the leading `/`
rel_path <- substring(config_file, first = nchar(pkg_path) + 2L, last = nchar(config_file))
ignore_path <- file.path(pkg_path, ".Rbuildignore")
if (!file.exists(ignore_path)) file.create(ignore_path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This branch can be a writeLines(..., ignore_path) with immediate return.

# Follow the same procedure as base R to see if the file is already ignored
ignore <- tryCatch({
trimws(readLines(ignore_path))
}, warning = function(e) {
cat(file = ignore_path, "\n", append = TRUE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you silently fixing the missing terminal newline here?

trimws(readLines(ignore_path))
})
ignore <- ignore[nzchar(ignore)]
already_ignored <-
any(vapply(ignore, FUN = grepl, x = rel_path, perl = TRUE, ignore.case = TRUE, FUN.VALUE = logical(1L)))
if (!already_ignored) {
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IINM the sep= is ignored because you only pass one entry into cat.

message("Adding ", rel_path, " to .Rbuildignore")

Check warning on line 72 in R/use_lintr.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_lintr.R,line=72,col=9,[undesirable_function_call_linter] Avoid undesirable function "message". As an alternative, use cli::cli_inform().
}
}
}

invisible(config_file)
}
2 changes: 1 addition & 1 deletion man/use_lintr.Rd

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

53 changes: 53 additions & 0 deletions tests/testthat/test-use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,56 @@ test_that("use_lintr with type = full also works", {
lints <- lint_dir(tmp)
expect_length(lints, 0L)
})

test_that("No .Rbuildignore is created of packages", {
tmp <- withr::local_tempdir()

lintr_file <- use_lintr(path = tmp, type = "full")
expect_false(file.exists(file.path(tmp, ".Rbuildignore")))
})

test_that("No .Rbuildignore is filled outside of packages", {
tmp <- withr::local_tempdir()
ignore <- file.path(tmp, ".Rbuildignore")
file.create(ignore)

lintr_file <- use_lintr(path = tmp, type = "full")
expect_identical(readLines(ignore), character())
})

test_that("No .Rbuildignore is filled if matching regex exists", {
tmp <- withr::local_tempdir()
file.create(file.path(tmp, "DESCRIPTION"))
ignore <- file.path(tmp, ".Rbuildignore")
file.create(ignore)
cat(file = ignore, ".*", sep = "\n")

lintr_file <- use_lintr(path = tmp, type = "full")
expect_identical(readLines(ignore), ".*")
})

test_that("use_lintr creates the correct regex", {
tmp <- withr::local_tempdir()
file.create(file.path(tmp, "DESCRIPTION"))
ignore <- file.path(tmp, ".Rbuildignore")
file.create(ignore)
cat(file = ignore, "^fu$", "^bar$", sep = "\n")

expect_message({
lintr_file <- use_lintr(path = tmp, type = "full")
}, regexp = "Adding .* to .Rbuildignore")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: the . in front of Rbuildignore matches any character.

expect_identical(readLines(ignore), c("^fu$", "^bar$", "^\\.lintr$"))
})

test_that("use_lintr handles missing final new line", {
tmp <- withr::local_tempdir()
file.create(file.path(tmp, "DESCRIPTION"))
ignore <- file.path(tmp, ".Rbuildignore")
file.create(ignore)
cat(file = ignore, "^fu$\n^bar$")

expect_message({
lintr_file <- use_lintr(path = tmp, type = "full")
}, regexp = "Adding .* to .Rbuildignore")
expect_identical(readLines(ignore), c("^fu$", "^bar$", "^\\.lintr$"))
})
Loading