fix()
, fix_package()
, and fix_dir()
all replace lints in files. The
only difference is in the input they take:
fix()
takes path to files or directoriesfix_dir()
takes a path to one directoryfix_package()
takes a path to the root of a package and looks at the following list of folders:R
,tests
,inst
,vignettes
,data-raw
,demo
,exec
.
fix_text()
takes some text input. Its main interest is to be able to
quickly experiment with some lints and fixes.
Usage
fix(
path = ".",
linters = NULL,
exclude_path = NULL,
exclude_linters = NULL,
force = FALSE,
verbose = TRUE
)
fix_dir(
path = ".",
linters = NULL,
exclude_path = NULL,
exclude_linters = NULL
)
fix_package(
path = ".",
linters = NULL,
exclude_path = NULL,
exclude_linters = NULL
)
fix_text(text, linters = NULL, exclude_linters = NULL)
Arguments
- path
A valid path to a file or a directory. Relative paths are accepted.
- linters
A character vector with the names of the rules to apply. See the entire list of rules with
list_linters()
.- exclude_path
One or several paths that will be ignored from the
path
selection.- exclude_linters
One or several linters that will not be checked. Values can be the names of linters (such as
"any_is_na"
) or its associated function, such asany_is_na_linter()
(this is mostly for compatibility withlintr
).- force
Force the application of fixes on the files. This is used only in the case where Git is not detected, several files will be modified, and the code is run in a non-interactive setting.
- verbose
Show messages.
- text
Text to analyze (and to fix if necessary).
Ignoring lines
flint
supports ignoring single lines of code with # flint-ignore
. For
example, this will not warn:
# flint-ignore
any(duplicated(x))
However, this will warn for the second any(duplicated())
:
# flint-ignore
any(duplicated(x))
any(duplicated(y))
To ignore more than one line of code, use # flint-ignore-start
and
# flint-ignore-end
:
# flint-ignore-start
any(duplicated(x))
any(duplicated(y))
# flint-ignore-end
Examples
# `fix_text()` is convenient to explore with a small example
fix_text("any(duplicated(rnorm(5)))")
#> Old code: any(duplicated(rnorm(5)))
#> New code: anyDuplicated(rnorm(5)) > 0
fix_text("any(duplicated(rnorm(5)))
any(is.na(x))
")
#> Old code:
#> any(duplicated(rnorm(5)))
#> any(is.na(x))
#>
#> New code:
#> anyDuplicated(rnorm(5)) > 0
#> anyNA(x)
# Setup for the example with `fix()`
destfile <- tempfile()
cat("
x = c(1, 2, 3)
any(duplicated(x), na.rm = TRUE)
any(duplicated(x))
if (any(is.na(x))) {
TRUE
}
any(
duplicated(x)
)", file = destfile)
fix(destfile)
#> ℹ Going to check 1 file.
#> ✔ Fixed 3 lints in 1 file.
cat(paste(readLines(destfile), collapse = "\n"))
#>
#> x <- c(1, 2, 3)
#> anyDuplicated(x) > 0
#>
#> anyDuplicated(x) > 0
#>
#> if (anyNA(x)) {
#> TRUE
#> }
#>
#> anyDuplicated(x) > 0