Skip to content

New numeric_leading_zero_linter #992

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

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Collate:
'namespace.R'
'namespace_linter.R'
'no_tab_linter.R'
'numeric_leading_zero_linter.R'
'object_name_linters.R'
'object_usage_linter.R'
'open_curly_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export(missing_package_linter)
export(namespace_linter)
export(no_tab_linter)
export(nonportable_path_linter)
export(numeric_leading_zero_linter)
export(object_length_linter)
export(object_name_linter)
export(object_usage_linter)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function calls. (#850, #851, @renkun-ken)
* `vector_logic_linter()` Require use of scalar logical operators (`&&` and `||`) inside `if()` conditions and similar
* `any_is_na_linter()` Require usage of `anyNA(x)` over `any(is.na(x))`
* `outer_negation_linter()` Require usage of `!any(x)` over `all(!x)` and `!all(x)` over `any(!x)`
* `numeric_leading_zero_linter()` Require a leading `0` in fractional numeric constants, e.g. `0.1` instead of `.1`
* `assignment_linter()` now lints right assignment (`->` and `->>`) and gains two arguments. `allow_cascading_assign` (`TRUE` by default) toggles whether to lint `<<-` and `->>`; `allow_right_assign` toggles whether to lint `->` and `->>` (#915, @michaelchirico)
* `infix_spaces_linter()` gains argument `exclude_operators` to disable lints on selected infix operators. By default, all "low-precedence" operators throw lints; see `?infix_spaces_linter` for an enumeration of these. (#914 @michaelchirico)
* `infix_spaces_linter()` now throws a lint on `a~b` and `function(a=1) {}` (#930, @michaelchirico)
Expand Down
34 changes: 34 additions & 0 deletions R/numeric_leading_zero_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Require usage of a leading zero in all fractional numerics
#'
#' While .1 and 0.1 mean the same thing, the latter is easier to read due
#' to the small size of the '.' glyph.
#'
#' @evalRd rd_tags("numeric_leading_zero_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
numeric_leading_zero_linter <- function() {
Linter(function(source_file) {
if (length(source_file$parsed_content) == 0L) {
return(list())
}

xml <- source_file$xml_parsed_content

# NB:
# 1. negative constants are split to two components:
# OP-MINUS, NUM_CONST
# 2. complex constants are split to three components:
# NUM_CONST, OP-PLUS/OP-MINUS, NUM_CONST
xpath <- "//NUM_CONST[starts-with(text(), '.')]"

bad_expr <- xml2::xml_find_all(xml, xpath)

return(lapply(
bad_expr,
xml_nodes_to_lint,
source_file = source_file,
lint_message = "Include the leading zero for fractional numeric constants.",
type = "warning"
))
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ missing_package_linter,robustness common_mistakes
namespace_linter,correctness robustness configurable
no_tab_linter,style consistency default
nonportable_path_linter,robustness best_practices configurable
numeric_leading_zero_linter,style consistency readability
object_length_linter,style readability default configurable
object_name_linter,style consistency default configurable
object_usage_linter,style readability correctness default
Expand Down
1 change: 1 addition & 0 deletions man/consistency_linters.Rd

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

3 changes: 2 additions & 1 deletion man/linters.Rd

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

18 changes: 18 additions & 0 deletions man/numeric_leading_zero_linter.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/readability_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/style_linters.Rd

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

25 changes: 25 additions & 0 deletions tests/testthat/test-numeric_leading_zero_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that("numeric_leading_zero_linter skips allowed usages", {
expect_lint("a <- 0.1", NULL, numeric_leading_zero_linter())
expect_lint("b <- -0.2", NULL, numeric_leading_zero_linter())
expect_lint("c <- 3.0", NULL, numeric_leading_zero_linter())
expect_lint("d <- 4L", NULL, numeric_leading_zero_linter())
expect_lint("e <- TRUE", NULL, numeric_leading_zero_linter())
expect_lint("f <- 0.5e6", NULL, numeric_leading_zero_linter())
expect_lint("g <- 0x78", NULL, numeric_leading_zero_linter())
expect_lint("h <- 0.9 + 0.1i", NULL, numeric_leading_zero_linter())
expect_lint("h <- 0.9+0.1i", NULL, numeric_leading_zero_linter())
expect_lint("h <- 0.9 - 0.1i", NULL, numeric_leading_zero_linter())
expect_lint("i <- 2L + 3.4i", NULL, numeric_leading_zero_linter())
})

test_that("numeric_leading_zero_linter blocks simple disallowed usages", {
linter <- numeric_leading_zero_linter()
lint_message <- rex::rex("Include the leading zero for fractional numeric constants.")

expect_lint("a <- .1", lint_message, linter)
expect_lint("b <- -.2", lint_message, linter)
expect_lint("c <- .3 + 4.5i", lint_message, linter)
expect_lint("d <- 6.7 + .8i", lint_message, linter)
expect_lint("d <- 6.7+.8i", lint_message, linter)
expect_lint("e <- .9e10", lint_message, linter)
})