From f65c92ea59ff7ba9f06bd992c9a55b23071d6fb9 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 11 Dec 2023 11:02:02 +0000 Subject: [PATCH] Customize lint message --- R/equals_na_linter.R | 3 ++- tests/testthat/test-equals_na_linter.R | 28 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/R/equals_na_linter.R b/R/equals_na_linter.R index 8a1ceabdc..2961ac984 100644 --- a/R/equals_na_linter.R +++ b/R/equals_na_linter.R @@ -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" ) }) diff --git a/tests/testthat/test-equals_na_linter.R b/tests/testthat/test-equals_na_linter.R index 34f9e6f9c..26109e30b 100644 --- a/tests/testthat/test-equals_na_linter.R +++ b/tests/testthat/test-equals_na_linter.R @@ -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( @@ -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 + ) })