Title: | Find and Fix Lints in R Code |
---|---|
Description: | Lints are code patterns that are not optimal because they are inefficient, forget corner cases, or less readable. 'flint' provides a small set of functions to detect those lints and automatically fix them. It builds on 'astgrepr', which itself uses the Rust crate 'ast-grep' to parse and navigate R code. |
Authors: | Etienne Bacher [aut, cre, cph] |
Maintainer: | Etienne Bacher <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-11-14 05:31:30 UTC |
Source: | https://github.com/etiennebacher/flint |
anyDuplicated(x) > 0
over any(duplicated(x))
See https://lintr.r-lib.org/reference/any_duplicated_linter.
any_duplicated_linter
any_duplicated_linter
anyNA(x)
over any(is.na(x))
See https://lintr.r-lib.org/reference/any_is_na_linter.
any_is_na_linter
any_is_na_linter
==
See https://lintr.r-lib.org/reference/class_equals_linter.
class_equals_linter
class_equals_linter
paste()
and paste0()
with messaging functions using ...
See https://lintr.r-lib.org/reference/condition_message_linter.
condition_message_linter
condition_message_linter
double_assignment
double_assignment_linter
double_assignment_linter
See https://lintr.r-lib.org/reference/duplicate_argument_linter.
duplicate_argument_linter
duplicate_argument_linter
See https://lintr.r-lib.org/reference/equals_na_linter.
equals_na_linter
equals_na_linter
expect_gt(x, y)
over expect_true(x > y)
(and similar)See https://lintr.r-lib.org/reference/expect_comparison_linter.
expect_comparison_linter
expect_comparison_linter
expect_identical(x, y)
where appropriateSee https://lintr.r-lib.org/reference/expect_identical_linter.
expect_identical_linter
expect_identical_linter
expect_length(x, n)
over expect_equal(length(x), n)
See https://lintr.r-lib.org/reference/expect_length_linter.
expect_length_linter
expect_length_linter
expect_named(x, n)
over expect_equal(names(x), n)
See https://lintr.r-lib.org/reference/expect_named_linter.
expect_named_linter
expect_named_linter
expect_false(x)
over expect_true(!x)
See https://lintr.r-lib.org/reference/expect_not_linter.
expect_not_linter
expect_not_linter
expect_null
for checking NULL
See https://lintr.r-lib.org/reference/expect_null_linter.
expect_null_linter
expect_null_linter
expect_true(x)
over expect_equal(x, TRUE)
See https://lintr.r-lib.org/reference/expect_true_false_linter.
expect_true_false_linter
expect_true_false_linter
expect_type(x, type)
over expect_equal(typeof(x), type)
See https://lintr.r-lib.org/reference/expect_type_linter.
expect_type_linter
expect_type_linter
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 directories
fix_dir()
takes a path to one directory
fix_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.
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, force = FALSE, verbose = TRUE ) fix_package( path = ".", linters = NULL, exclude_path = NULL, exclude_linters = NULL, force = FALSE, verbose = TRUE ) fix_text(text, linters = NULL, exclude_linters = NULL)
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, force = FALSE, verbose = TRUE ) fix_package( path = ".", linters = NULL, exclude_path = NULL, exclude_linters = NULL, force = FALSE, verbose = TRUE ) fix_text(text, linters = NULL, exclude_linters = NULL)
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 |
exclude_path |
One or several paths that will be ignored from the |
exclude_linters |
One or several linters that will not be checked.
Values can be the names of linters (such as |
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). |
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
# `fix_text()` is convenient to explore with a small example fix_text("any(duplicated(rnorm(5)))") fix_text("any(duplicated(rnorm(5))) any(is.na(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) cat(paste(readLines(destfile), collapse = "\n"))
# `fix_text()` is convenient to explore with a small example fix_text("any(duplicated(rnorm(5)))") fix_text("any(duplicated(rnorm(5))) any(is.na(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) cat(paste(readLines(destfile), collapse = "\n"))
See https://lintr.r-lib.org/reference/for_loop_index_linter.
for_loop_index_linter
for_loop_index_linter
See https://lintr.r-lib.org/reference/function_return_linter.
function_return_linter
function_return_linter
implicit_assignment
implicit_assignment_linter
implicit_assignment_linter
is.numeric(x) || is.integer(x)
to just use is.numeric(x)
See https://lintr.r-lib.org/reference/is_numeric_linter.
is_numeric_linter
is_numeric_linter
See https://lintr.r-lib.org/reference/length_levels_linter.
length_levels_linter
length_levels_linter
See https://lintr.r-lib.org/reference/length_test_linter.
length_test_linter
length_test_linter
lengths()
where possibleSee https://lintr.r-lib.org/reference/lengths_linter.
lengths_linter
lengths_linter
See https://lintr.r-lib.org/reference/library_call_linter.
library_call_linter
library_call_linter
lint()
, lint_text()
, lint_package()
, and lint_dir()
all produce a
data.frame containing the lints, their location, and potential fixes. The
only difference is in the input they take:
lint()
takes path to files or directories
lint_text()
takes some text input
lint_dir()
takes a path to one directory
lint_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
.
lint( path = ".", linters = NULL, exclude_path = NULL, exclude_linters = NULL, open = TRUE, use_cache = TRUE, verbose = TRUE ) lint_dir( path = ".", linters = NULL, open = TRUE, exclude_path = NULL, exclude_linters = NULL, use_cache = TRUE, verbose = TRUE ) lint_package( path = ".", linters = NULL, open = TRUE, exclude_path = NULL, exclude_linters = NULL, use_cache = TRUE, verbose = TRUE ) lint_text(text, linters = NULL, exclude_linters = NULL)
lint( path = ".", linters = NULL, exclude_path = NULL, exclude_linters = NULL, open = TRUE, use_cache = TRUE, verbose = TRUE ) lint_dir( path = ".", linters = NULL, open = TRUE, exclude_path = NULL, exclude_linters = NULL, use_cache = TRUE, verbose = TRUE ) lint_package( path = ".", linters = NULL, open = TRUE, exclude_path = NULL, exclude_linters = NULL, use_cache = TRUE, verbose = TRUE ) lint_text(text, linters = NULL, exclude_linters = NULL)
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 |
exclude_path |
One or several paths that will be ignored from the |
exclude_linters |
One or several linters that will not be checked.
Values can be the names of linters (such as |
open |
If |
use_cache |
Do not re-parse files that haven't changed since the last time this function ran. |
verbose |
Show messages. |
text |
Text to analyze. |
A dataframe where each row is a lint. The columns show the text, its location (both the position in the text and the file in which it was found) and the severity.
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
# `lint_text()` is convenient to explore with a small example lint_text("any(duplicated(rnorm(5)))") lint_text("any(duplicated(rnorm(5))) any(is.na(x)) ") # Setup for the example with `lint()` 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) lint(destfile)
# `lint_text()` is convenient to explore with a small example lint_text("any(duplicated(rnorm(5)))") lint_text("any(duplicated(rnorm(5))) any(is.na(x)) ") # Setup for the example with `lint()` 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) lint(destfile)
flint
Get the list of linters in flint
list_linters()
list_linters()
A character vector
list_linters()
list_linters()
See https://lintr.r-lib.org/reference/literal_coercion_linter.
literal_coercion_linter
literal_coercion_linter
colSums(x)
or rowSums(x)
over apply(x, ., sum)
See https://lintr.r-lib.org/reference/matrix_apply_linter.
matrix_apply_linter
matrix_apply_linter
See https://lintr.r-lib.org/reference/missing_argument_linter.
missing_argument_linter
missing_argument_linter
ifelse()
callsSee https://lintr.r-lib.org/reference/nested_ifelse_linter.
nested_ifelse_linter
nested_ifelse_linter
See https://lintr.r-lib.org/reference/numeric_leading_zero_linter.
numeric_leading_zero_linter
numeric_leading_zero_linter
!any(x)
over all(!x)
, !all(x)
over any(!x)
See https://lintr.r-lib.org/reference/outer_negation_linter.
outer_negation_linter
outer_negation_linter
See https://lintr.r-lib.org/reference/package_hooks_linter.
package_hooks_linter
package_hooks_linter
paste()
See https://lintr.r-lib.org/reference/paste_linter.
paste_linter
paste_linter
==
, !=
on logical vectorsSee https://lintr.r-lib.org/reference/redundant_equals_linter.
redundant_equals_linter
redundant_equals_linter
ifelse()
from being used to produce TRUE
/FALSE
or 1
/0
See https://lintr.r-lib.org/reference/redundant_ifelse_linter.
redundant_ifelse_linter
redundant_ifelse_linter
See https://lintr.r-lib.org/reference/rep_len_linter.
rep_len_linter
rep_len_linter
See https://lintr.r-lib.org/reference/sample_int_linter.
sample_int_linter
sample_int_linter
See https://lintr.r-lib.org/reference/semicolon_linter.
semicolon_linter
semicolon_linter
See https://lintr.r-lib.org/reference/seq_linter.
seq_linter
seq_linter
This stores the default rules and internal files in inst/flint
. It also
imports sgconfig.yml
that is used by ast-grep
. This file must live at the
root of the project and cannot be renamed.
setup_flint(path = ".")
setup_flint(path = ".")
path |
Path to package or project root. |
Imports files necessary for flint
to work but doesn't return any
value in R.
flint
Create a Github Actions workflow for flint
setup_flint_gha(path = ".", overwrite = FALSE)
setup_flint_gha(path = ".", overwrite = FALSE)
path |
Root path to the package. |
overwrite |
Whether to overwrite |
Creates .github/workflows/flint.yaml
but doesn't return any value.
See https://lintr.r-lib.org/reference/sort_linter.
sort_linter
sort_linter
T
and F
symbol linterSee https://lintr.r-lib.org/reference/T_and_F_symbol_linter.
T_and_F_symbol_linter
T_and_F_symbol_linter
See https://lintr.r-lib.org/reference/todo_comment_linter.
todo_comment_linter
todo_comment_linter
See https://lintr.r-lib.org/reference/undesirable_function_linter.
undesirable_function_linter
undesirable_function_linter
See https://lintr.r-lib.org/reference/undesirable_operator_linter.
undesirable_operator_linter
undesirable_operator_linter
See https://lintr.r-lib.org/reference/unnecessary_nesting_linter.
unnecessary_nesting_linter
unnecessary_nesting_linter
See https://lintr.r-lib.org/reference/unreachable_code_linter.
unreachable_code_linter
unreachable_code_linter
flint
setupWhen flint
is updated, it can ship new built-in rules or update existing
ones. update_flint()
will automatically add those new rules to the
flint/rules/builtin
folder. Custom rules stored in flint/rules/custom
are not affected.
update_flint(path = ".")
update_flint(path = ".")
path |
Path to package or project root. |
Can add new files in the flint/rules
folder, doesn't return anything.
## Not run: update_flint() ## End(Not run)
## Not run: update_flint() ## End(Not run)
See https://lintr.r-lib.org/reference/which_grepl_linter.
which_grepl_linter
which_grepl_linter