Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(Lint)
export(Linter)
export(T_and_F_symbol_linter)
export(absolute_path_linter)
export(all_linters)
export(all_undesirable_functions)
export(all_undesirable_operators)
export(any_duplicated_linter)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

* Added `format()` functions for `lint` and `lints` (#1784, @AshesITR)

* `all_linters()` function provides an easy way to access all available linters (#1843, @IndrajeetPatil)

### New linters

* `unnecessary_lambda_linter()`: detect unnecessary lambdas (anonymous functions), e.g.
Expand Down
2 changes: 1 addition & 1 deletion R/use_lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use_lintr <- function(path = ".", type = c("tidyverse", "full")) {
encoding = '"UTF-8"'
),
full = list(
linters = 'linters_with_tags(tags = NULL, packages = "lintr") # see vignette("lintr")',
linters = 'all_linters(packages = "lintr") # see vignette("lintr")',
encoding = '"UTF-8"',
exclusions = 'list("renv", "packrat") # see ?lintr::exclude'
)
Expand Down
37 changes: 29 additions & 8 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
#' @param defaults named list of elements to modify.
#' @return A modified list of elements, sorted by name. To achieve this sort in a platform-independent way, two
#' transformations are applied to the names: (1) replace `_` with `0` and (2) convert [tolower()].
#' @seealso [linters_with_tags], [linters_with_defaults] for creating linter lists.
#' @seealso
#' - [linters_with_defaults] for basing off lintr's set of default linters.
#' - [all_linters] for basing off all available linters in lintr.
#' - [available_linters] to get a data frame of available linters.
#' - [linters] for a complete list of linters available in lintr.
#'
#' @examples
#' # custom list of undesirable functions:
#' # remove `sapply` (using `NULL`)
Expand Down Expand Up @@ -65,9 +70,11 @@ modify_defaults <- function(defaults, ...) {
#'
#' @return A modified list of linters.
#' @seealso
#' [linters_with_defaults] for basing off lintr's set of default linters.
#' [available_linters] to get a data frame of available linters.
#' [linters] for a complete list of linters available in lintr.
#' - [linters_with_defaults] for basing off lintr's set of default linters.
#' - [all_linters] for basing off all available linters in lintr.
#' - [available_linters] to get a data frame of available linters.
#' - [linters] for a complete list of linters available in lintr.
#'
#' @examples
#' # `linters_with_defaults()` and `linters_with_tags("default")` are the same:
#' all.equal(linters_with_defaults(), linters_with_tags("default"))
Expand All @@ -76,10 +83,6 @@ modify_defaults <- function(defaults, ...) {
#' linters <- linters_with_tags(tags = "package_development")
#' names(linters)
#'
#' # Get all linters provided by lintr
#' linters <- linters_with_tags(tags = NULL)
#' names(linters)
#'
#' # Get all linters tagged as "default" from lintr and mypkg
#' if (FALSE) {
#' linters_with_tags("default", packages = c("lintr", "mypkg"))
Expand Down Expand Up @@ -117,6 +120,23 @@ linters_with_tags <- function(tags, ..., packages = "lintr", exclude_tags = "dep
modify_defaults(..., defaults = tagged_linters)
}

#' Create a linter configuration based on all available linters
#'
#' @inheritParams linters_with_tags
#'
#' @examples
#' names(all_linters())
#'
#' @seealso
#' - [linters_with_defaults] for basing off lintr's set of default linters.
#' - [linters_with_tags] for basing off tags attached to linters, possibly across multiple packages.
#' - [available_linters] to get a data frame of available linters.
#' - [linters] for a complete list of linters available in lintr.
#' @export
all_linters <- function(packages = "lintr") {
linters_with_tags(tags = NULL, packages = packages)
}

#' Create a linter configuration based on defaults
#'
#' Make a new list based on \pkg{lintr}'s default linters.
Expand Down Expand Up @@ -148,6 +168,7 @@ linters_with_tags <- function(tags, ..., packages = "lintr", exclude_tags = "dep
#'
#' @seealso
#' - [linters_with_tags] for basing off tags attached to linters, possibly across multiple packages.
#' - [all_linters] for basing off all available linters in lintr.
#' - [available_linters] to get a data frame of available linters.
#' - [linters] for a complete list of linters available in lintr.
#' @export
Expand Down
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
[![codecov.io](https://codecov.io/github/r-lib/lintr/coverage.svg?branch=main)](https://codecov.io/github/r-lib/lintr?branch=main)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/lintr)](https://cran.r-project.org/package=lintr)

`lintr` provides [static code analysis for R](https://en.wikipedia.org/wiki/Static_program_analysis). It checks for adherence to a given style, identifying syntax errors and possible semantic issues, then reports them to you so you can take action. Watch lintr in action in the following animation:
`{lintr}` provides [static code analysis for R](https://en.wikipedia.org/wiki/Static_program_analysis). It checks for adherence to a given style, identifying syntax errors and possible semantic issues, then reports them to you so you can take action. Watch lintr in action in the following animation:

![](man/figures/demo.gif "lintr demo")

`lintr` is complementary to [the `styler` package](https://github.com/r-lib/styler) which automatically restyles code, eliminating some of the problems that `lintr` can detect.
`{lintr}` is complementary to [the `styler` package](https://github.com/r-lib/styler) which automatically restyles code, eliminating some of the problems that `{lintr}` can detect.

## Installation

Expand All @@ -26,26 +26,44 @@ remotes::install_github("r-lib/lintr")

## Usage

Load the library:

```R
library(lintr)
```

And then you can create a configuration file and run selected linters:

```R
use_lintr(type = "tidyverse")

# in a project:
lintr::use_lintr(type = "tidyverse")
usethis::use_github_action("lint-project")
lintr::lint_dir()
lint_dir()

# in a package:
lintr::use_lintr(type = "tidyverse")
usethis::use_github_action("lint")
lintr::lint_package()
lint_package()
```

To see a list of linters included for each configuration:

```R
# tidyverse (default)
names(lintr::linters_with_defaults())
names(linters_with_defaults())

# full
names(lintr::linters_with_tags(tags = NULL))
names(all_linters())
```

You can use helper functions from `{usethis}` package to generate GitHub action workflow files:

```R
library(usethis)

# in a project:
use_github_action("lint-project")

# in a package:
use_github_action("lint")
```

You can also run lintr during continuous integration or within your IDE or text editor. See `vignette("continuous-integration")` and `vignette("editors")` for more details.
Expand Down
26 changes: 26 additions & 0 deletions man/all_linters.Rd

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

1 change: 1 addition & 0 deletions man/linters_with_defaults.Rd

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

13 changes: 6 additions & 7 deletions man/linters_with_tags.Rd

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

7 changes: 6 additions & 1 deletion man/modify_defaults.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-with.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ test_that("linters_with_defaults(default = .) is supported with a deprecation wa
expect_silent(linters <- linters_with_defaults(defaults = list(), default = default))
expect_named(linters, "default")
})

test_that("all_linters contains all available linters", {
all_linters <- all_linters(packages = "lintr")

expect_identical(linters_with_tags(NULL, packages = "lintr"), all_linters)
expect_length(all_linters, nrow(available_linters()))
})
6 changes: 3 additions & 3 deletions vignettes/lintr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ knitr::kable(defaults_table)
The default lintr configuration includes only linters relevant to the tidyverse style guide, but there are many other linters available in `{lintr}`. You can see a list of all available linters using

```{r}
names(lintr::linters_with_tags(tags = NULL))
names(lintr::all_linters())
```

If you want to use all available linters, you can include this in your `.lintr` file:

```r
linters: linters_with_defaults(
defaults = linters_with_tags(tags = NULL)
defaults = all_linters()
)
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should also directly call all_linters().
Is there a section mentioning linters_with_tags()?


Expand All @@ -216,7 +216,7 @@ If you want to use all available linters *except* a few, you can exclude them us
linters: linters_with_defaults(
commented_code_linter = NULL,
implicit_integer_linter = NULL,
defaults = linters_with_tags(tags = NULL)
defaults = all_linters()
)
```

Expand Down