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

use_lintr adds .lintr to .Rbuildignore #2396

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* `return_linter()` also has an argument `return_style` (`"implicit"` by default) which checks that all functions confirm to the specified return style of `"implicit"` or `"explicit"` (part of #884, @MichaelChirico, @AshesITR and @MEO265).
* `unnecessary_lambda_linter` is extended to encourage vectorized comparisons where possible, e.g. `sapply(x, sum) > 0` instead of `sapply(x, function(x) sum(x) > 0)` (part of #884, @MichaelChirico). Toggle this behavior with argument `allow_comparison`.
* `Linter()` has a new argument `linter_level` (default `NA`). This is used by `lint()` to more efficiently check for expression levels than the idiom `if (!is_lint_level(...)) { return(list()) }` (#2351, @AshesITR).
* `use_lintr()` adds the created `.lintr` file to the `.Rbuildignore` if run in a package. (#1805, @MEO265)

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

Expand Down
24 changes: 22 additions & 2 deletions 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 All @@ -25,7 +25,7 @@
#' lintr::lint_dir()
#' }
use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = FALSE)
config_file <- normalizePath(file.path(path, lintr_option("linter_file")), mustWork = FALSE, winslash = "/")
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
if (file.exists(config_file)) {
stop("Found an existing configuration file at '", config_file, "'.")
}
Expand All @@ -43,5 +43,25 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
)
)
write.dcf(the_config, config_file, width = Inf)

# Check if config_file is in package i.e. lintr_option("linter_file") != "../.lintr"
pkg_path <- normalizePath(path, mustWork = FALSE, winslash = "/")
if (file.exists("DESCRIPTION") && 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))
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
ignore_path <- file.path(pkg_path, ".Rbuildignore")
if (!file.exists(ignore_path)) file.create(ignore_path)
MEO265 marked this conversation as resolved.
Show resolved Hide resolved
# Follow the same procedure as base R to see if the file is already ignored
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?

ignore <- trimws(readLines(ignore_path, warn = FALSE))
}, warning = function(e) cat(file = ignore_path, "\n", append = TRUE)
)
ignore <- ignore[nzchar(ignore)]
if (!any(vapply(ignore, function(x) grepl(rel_path, pattern = x, perl = TRUE, ignore.case = TRUE), logical(1L)))) {
MEO265 marked this conversation as resolved.
Show resolved Hide resolved
cat(file = ignore_path, rex::rex(start, rel_path, end), sep = "\n", append = TRUE)
message("Adding ", rel_path, " to .Rbuildignore")
}
}

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.