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

Customize lint message for equals_na_linter() #2415

Merged
merged 1 commit into from
Dec 11, 2023
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
3 changes: 2 additions & 1 deletion R/equals_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ equals_na_linter <- function() {
xml <- source_expression$xml_parsed_content

bad_expr <- xml_find_all(xml, xpath)
op <- xml_find_first(bad_expr, "EQ | NE | SPECIAL")

xml_nodes_to_lints(
bad_expr,
source_expression,
lint_message = "Use is.na for comparisons to NA (not == or != or %in%)",
lint_message = sprintf("Use is.na() instead of x %s NA", xml_text(op)),
type = "warning"
)
})
Expand Down
28 changes: 22 additions & 6 deletions tests/testthat/test-equals_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ patrick::with_parameters_test_that(
"equals_na_linter blocks disallowed usages for all combinations of operators and types of NAs",
expect_lint(
paste("x", operation, type_na),
rex::rex("Use is.na for comparisons to NA (not == or != or %in%)"),
rex::rex("Use is.na() instead of x ", operation, " NA"),
equals_na_linter()
),
.cases = tibble::tribble(
Expand All @@ -52,15 +52,31 @@ patrick::with_parameters_test_that(

test_that("equals_na_linter blocks disallowed usages in edge cases", {
linter <- equals_na_linter()
lint_msg <- rex::rex("Use is.na for comparisons to NA (not == or != or %in%)")
lint_msg_part <- "Use is.na() instead of x "

# missing spaces around operators
expect_lint("x==NA", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)
expect_lint("x!=NA", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)
expect_lint(
"x==NA",
list(message = rex::rex(lint_msg_part, "== NA"), line_number = 1L, column_number = 1L),
linter
)
expect_lint(
"x!=NA",
list(message = rex::rex(lint_msg_part, "!= NA"), line_number = 1L, column_number = 1L),
linter
)

# order doesn't matter
expect_lint("NA == x", list(message = lint_msg, line_number = 1L, column_number = 1L), linter)
expect_lint(
"NA == x",
list(message = rex::rex(lint_msg_part, "== NA"), line_number = 1L, column_number = 1L),
linter
)

# correct line number for multiline code
expect_lint("x ==\nNA", list(line_number = 1L, column_number = 1L, ranges = list(c(1L, 4L))), linter)
expect_lint(
"x ==\nNA",
list(line_number = 1L, column_number = 1L, ranges = list(c(1L, 4L))),
linter
)
})
Loading