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

Generating an explicit .lintr.R file #2628

Open
TimTaylor opened this issue Jul 11, 2024 · 2 comments
Open

Generating an explicit .lintr.R file #2628

TimTaylor opened this issue Jul 11, 2024 · 2 comments

Comments

@TimTaylor
Copy link

TimTaylor commented Jul 11, 2024

Would there be interest in providing a function that generates an explicit (e.g. defaults explicitly listed and commented) .lintr.R configuration. These could be pre-populated templates within the package or generated by users at runtime.

The motivation for this is that an explicit format makes it clear to new (and probably old) users what lints are going to be run and, if commentary is also supplied, what those lints do. Of course there are drawbacks to the explicit approach (e.g. a need to regenerate the file with new releases) but I think the option could be useful.

Some code I've been playing with is shared below. At the moment it is purely set to print out and comment all linters but there is no reason it couldn't handle customisation and NULLing of linters as with the other functions.

generate_all_linters <- function(filename, ...) {
    # TODO - filename checks
    # TODO - changing lint defaults / excluding lints
    # TODO - alternative to tools:::.Rd_get_metadata that will convert description to nicely formatted text

    names <- .subset2(lintr::available_linters(), "linter")
    namespace <- loadNamespace("lintr")
    call <- lapply(names, function(x) c(as.symbol(x), formals(x, namespace)))
    call <- sapply(call, function(x) deparse1(as.call(x)))

    rddb <- tools::Rd_db("lintr")
    desc <- sapply(rddb, tools:::.Rd_get_metadata, "description")
    names(desc) <- sub(".Rd$", "", names(desc), perl = TRUE)
    desc <- desc[names]
    desc <- gsub(pattern = "\n", replacement = " ", x = desc, perl = TRUE)
    desc <- gsub(pattern = "[[:blank:]]{2,}", replacement = " ", x = desc, perl = TRUE)
    desc <- lapply(desc, strwrap, width = 80)
    desc <- lapply(desc, function(x) paste("    #", x, collapse = "\n"))

    cat("linters <- list(\n", file = filename)
    for (i in seq_along(call)) {
        cat(desc[[i]], file = filename, append = TRUE)
        txt <- sprintf("\n    %s%s", call[[i]], if (i == length(call)) "\n)" else ",\n\n")
        cat(txt, file = filename, append = TRUE)
    }
    invisible(filename)
}

I'm happy to pursue this further in a draft PR but no worries if not desirable / out of scope.

@TimTaylor TimTaylor changed the title Generating an explicity .lintr.R file Generating an explicit .lintr.R file Jul 11, 2024
@AshesITR
Copy link
Collaborator

This may be well integrated into use_lintr() once we're committed to this type of configuration.

@TimTaylor
Copy link
Author

Cool. FWIW - I've tidied up this function for myself (better formatting and accompanying lint documentation) over at https://github.com/TimTaylor/lintrall/blob/main/pkg/R/use_all_linters.R. I've also added a copy of what it currently generates at https://github.com/TimTaylor/lintrall/blob/main/pkg/inst/Example/.lintr.R in case you want to peruse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants