Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
171 commits
Select commit Hold shift + click to select a range
3882350
use maybe_write_content for easier 'mocking'
MichaelChirico Mar 5, 2025
c392c53
initial progress
MichaelChirico Mar 6, 2025
5cef281
getting very close i think...
MichaelChirico Mar 6, 2025
a4e4a66
skip Rmd files
MichaelChirico Mar 6, 2025
0b1eaf5
caught a live one!
MichaelChirico Mar 6, 2025
868ad30
need to match original file extension?
MichaelChirico Mar 6, 2025
0ed5cc0
caught another one!
MichaelChirico Mar 6, 2025
99d00a3
simpler approach, avoid rex() due to bug
MichaelChirico Mar 6, 2025
d3cca7a
also ignore warnings
MichaelChirico Mar 6, 2025
59dc1b0
finally getting somewhere...
MichaelChirico Mar 6, 2025
a25065f
progressively more complicated :(
MichaelChirico Mar 6, 2025
491a340
round of fixes & first working nofuzz
MichaelChirico Mar 6, 2025
92f0628
looks like we got another live one... break time
MichaelChirico Mar 6, 2025
d387a71
another true positive
MichaelChirico Mar 6, 2025
e150ffe
more ignores, need '.' in file extension, restore test
MichaelChirico Mar 6, 2025
3d1fc0e
wrapping up
MichaelChirico Mar 6, 2025
b69b7cd
Write up the GHA config
MichaelChirico Mar 6, 2025
b8a06e3
annotation
MichaelChirico Mar 6, 2025
a3dbf27
comment for future work
MichaelChirico Mar 6, 2025
5a22050
vestigial
MichaelChirico Mar 6, 2025
76b869f
skips on old R
MichaelChirico Mar 6, 2025
afec743
expect_no_lint
MichaelChirico Mar 6, 2025
51593e4
new tests
MichaelChirico Mar 6, 2025
f4b9481
NEWS
MichaelChirico Mar 6, 2025
6389d55
bad copy-paste
MichaelChirico Mar 6, 2025
1550ead
need stop_on_failure for batch?
MichaelChirico Mar 6, 2025
bbdac43
delint, fix last skip for R<4.1.0
MichaelChirico Mar 6, 2025
523c218
more extensible structure
MichaelChirico Mar 7, 2025
852d0ea
expect_no_lint
MichaelChirico Mar 7, 2025
3eb21ca
progress, incl. many 'nofuzz' & 'no_lint'
MichaelChirico Mar 7, 2025
8059091
another round of nofuzz
MichaelChirico Mar 7, 2025
df8cccc
another batch
MichaelChirico Mar 7, 2025
138e9cc
tweak
MichaelChirico Mar 7, 2025
683c461
another nofuzz case, attempting to reduce nofuzz requirements
MichaelChirico Mar 7, 2025
98086d4
fix; scale back nofuzz for an attempt
MichaelChirico Mar 7, 2025
fe88c59
reinstate more legit nofuzz
MichaelChirico Mar 7, 2025
1928831
general fix for issue of S4 method calls under @
MichaelChirico Mar 7, 2025
168ee65
fix missed S4 extractions looking for preamble
MichaelChirico Mar 7, 2025
413e029
expect_no_lint
MichaelChirico Mar 7, 2025
c7cc8ac
handle @ equivalency
MichaelChirico Mar 7, 2025
4d3ff4f
fix an equivalency issue in indentation_linter
MichaelChirico Mar 7, 2025
1d8869f
new simple swap fuzzer, some nofuzz
MichaelChirico Mar 7, 2025
3d106d7
add some vectorization to make debugging easier
MichaelChirico Mar 7, 2025
1d19687
another indentation inconsistency
MichaelChirico Mar 7, 2025
dbfaf5c
nofuzz'ing
MichaelChirico Mar 7, 2025
88117cb
complete NEWS
MichaelChirico Mar 7, 2025
ee611cb
more expect_no_lint
MichaelChirico Mar 7, 2025
aa756e6
more expect_no_lint
MichaelChirico Mar 7, 2025
c3a99ce
add tests of include_s4_slots
MichaelChirico Mar 7, 2025
5893b8c
initial try, let's see
MichaelChirico Mar 7, 2025
8a83c01
adversarial comment protection
MichaelChirico Mar 7, 2025
836bf31
expect_no_lint
MichaelChirico Mar 7, 2025
f0974d9
caught true false positive
MichaelChirico Mar 7, 2025
a0dc171
some nofuzz
MichaelChirico Mar 7, 2025
4fc370d
don't break up calls; report the actual content
MichaelChirico Mar 7, 2025
c130bf4
nofuzz
MichaelChirico Mar 7, 2025
fc097ba
fix another one
MichaelChirico Mar 7, 2025
092f98e
fix another one
MichaelChirico Mar 8, 2025
4c08d49
another real fix
MichaelChirico Mar 8, 2025
7b2a16d
nofuzz
MichaelChirico Mar 8, 2025
78bdc12
no hope for brace_linter; use a space before comment for infix_spaces
MichaelChirico Mar 8, 2025
6446bbb
another real fix
MichaelChirico Mar 8, 2025
d256ccb
expect_no_lint
MichaelChirico Mar 8, 2025
f4e53cb
another real fix
MichaelChirico Mar 8, 2025
f817c77
expect_no_lint
MichaelChirico Mar 8, 2025
3f817a7
expect_no_lint, stylistic touch-up
MichaelChirico Mar 8, 2025
0fa5647
style touch-up (no message=)
MichaelChirico Mar 8, 2025
c2b18ca
nofuzz
MichaelChirico Mar 8, 2025
24f32a4
expect_no_lint
MichaelChirico Mar 8, 2025
c7530e6
more true fixes
MichaelChirico Mar 8, 2025
0ed031d
tidy
MichaelChirico Mar 8, 2025
ccc0f89
tidy2
MichaelChirico Mar 8, 2025
6c090b7
nofuzz, expect_no_lint
MichaelChirico Mar 8, 2025
cd7052b
a real bear here, fixed
MichaelChirico Mar 8, 2025
4326789
another true fix
MichaelChirico Mar 8, 2025
35e7708
bug fix
MichaelChirico Mar 8, 2025
218e656
more nofuzz, another true fix
MichaelChirico Mar 8, 2025
29a6232
another nofuzz, another true fix
MichaelChirico Mar 8, 2025
85fd53e
another case requiring stripping comments, more nofuzz
MichaelChirico Mar 8, 2025
7ae6552
fix yet another, nofuzz
MichaelChirico Mar 8, 2025
c750079
another fix
MichaelChirico Mar 8, 2025
7cede8e
closer & closer: another
MichaelChirico Mar 8, 2025
bacce08
found the MRE, not fixed yet
MichaelChirico Mar 8, 2025
edfe6d3
what eldritch horrors...
MichaelChirico Mar 8, 2025
06b2047
the easier fixes continue
MichaelChirico Mar 8, 2025
de0bf4a
modernize the test file first to apply nofuzz next
MichaelChirico Mar 8, 2025
15d3a50
further tweak, nofuzz
MichaelChirico Mar 8, 2025
7699a7c
ban the '*'
MichaelChirico Mar 8, 2025
4c38b90
node equality tests are going to be a pain...
MichaelChirico Mar 8, 2025
68ab08d
another one requiring a tree copy
MichaelChirico Mar 8, 2025
aad82de
nofuzz
MichaelChirico Mar 8, 2025
9a508b1
*[2]
MichaelChirico Mar 8, 2025
0ad134c
skip another file
MichaelChirico Mar 8, 2025
3929a43
preceding-sibling::*
MichaelChirico Mar 9, 2025
e57707e
improve handling of ifelse_censor, add a new test
MichaelChirico Mar 9, 2025
72164ee
more comments interfering with node=node tests
MichaelChirico Mar 9, 2025
ac774a3
simpler preceding-sibling::* fix
MichaelChirico Mar 9, 2025
173e932
more nofuzz, more preceding-sibling::*
MichaelChirico Mar 9, 2025
fcf7cf7
tricky tricky
MichaelChirico Mar 9, 2025
025537b
hit the dj khaled
MichaelChirico Mar 9, 2025
f0c8c9a
another tricky one
MichaelChirico Mar 9, 2025
ebe7936
kitchen sink
MichaelChirico Mar 9, 2025
04d4e1b
new one, old rule
MichaelChirico Mar 9, 2025
a7c9768
just drop formulas
MichaelChirico Mar 9, 2025
e1a497c
edge case in seq_linter
MichaelChirico Mar 9, 2025
3d11316
another sprintf case
MichaelChirico Mar 9, 2025
a9dca0c
re-fixed unreachable code thing?
MichaelChirico Mar 9, 2025
2a0831d
more nofuzz, and finally fixed the conjunct_test issue
MichaelChirico Mar 9, 2025
9344004
unbelievably tricky
MichaelChirico Mar 10, 2025
f53ec50
need one further level up here too
MichaelChirico Mar 10, 2025
5d61675
one more easy one
MichaelChirico Mar 10, 2025
dd08aa2
another tricky one
MichaelChirico Mar 10, 2025
b96f2b1
trickier handling needed
MichaelChirico Mar 10, 2025
ff083c2
for future reference
MichaelChirico Mar 10, 2025
88f998c
blessed oversight
MichaelChirico Mar 10, 2025
5606eac
vestigial
MichaelChirico Mar 10, 2025
49872bf
expect_no_lint
MichaelChirico Mar 10, 2025
3494010
leftovers
MichaelChirico Mar 10, 2025
0f02cc5
expect_no_lint in all touched files
MichaelChirico Mar 10, 2025
aa3e930
start the NEWS
MichaelChirico Mar 10, 2025
18f32d3
cite all changed linters
MichaelChirico Mar 10, 2025
2907c86
annotate reference issues where noteworthy
MichaelChirico Mar 10, 2025
a42bda5
cleanup
MichaelChirico Mar 10, 2025
82268e6
implement as simple swap
MichaelChirico Mar 10, 2025
b349f7f
fix ancestor::expr
MichaelChirico Mar 10, 2025
24f1545
nofuzz valid usages
MichaelChirico Mar 10, 2025
de97a23
count(expr) misses '=' assignments
MichaelChirico Mar 10, 2025
5a75c73
more nofuzz, and a real ancestor::* fix
MichaelChirico Mar 10, 2025
c785c0c
more nofuzz, another expr->* case
MichaelChirico Mar 10, 2025
87cdf1b
repeat_linter bug
MichaelChirico Mar 10, 2025
7dffd10
another count(expr) case
MichaelChirico Mar 10, 2025
db63bc0
3 straight clean runs
MichaelChirico Mar 10, 2025
1e4a68f
NEWS
MichaelChirico Mar 10, 2025
e7d0860
expect_no_lint in touched files
MichaelChirico Mar 10, 2025
f1c288a
quieter by using ListReporter
MichaelChirico Mar 10, 2025
3b76536
tidy+comment
MichaelChirico Mar 10, 2025
b53d934
better tracing
MichaelChirico Mar 11, 2025
ad85636
missing ','
MichaelChirico Mar 11, 2025
fb7866f
vastly improve the system for excluding tests from fuzzing
MichaelChirico Mar 11, 2025
fd5dfbd
caught a legit omission
MichaelChirico Mar 11, 2025
c20a891
another legit omission
MichaelChirico Mar 11, 2025
12d465b
more nofuzz
MichaelChirico Mar 11, 2025
0d45ec6
typo
MichaelChirico Mar 11, 2025
36f645e
more robustness
MichaelChirico Mar 11, 2025
732d1ff
more nofuzz progress
MichaelChirico Mar 11, 2025
1b3b41d
more nofuzz, some tidying, comments
MichaelChirico Mar 12, 2025
436da46
live one on the line!
MichaelChirico Mar 12, 2025
87e8588
more tidying & nofuzz stress testing
MichaelChirico Mar 12, 2025
357f56f
missed nofuzz in new test
MichaelChirico Mar 12, 2025
81b3849
Merge branch 'main' into nofuzz
MichaelChirico Jan 18, 2026
e22411c
first pass at merge fix-up
MichaelChirico Jan 18, 2026
003b01e
Merge remote-tracking branch 'origin/nofuzz' into nofuzz
MichaelChirico Jan 18, 2026
e856236
more merge fixup
MichaelChirico Jan 18, 2026
01a60c7
more merge fix-up
MichaelChirico Jan 18, 2026
b91f891
more
MichaelChirico Jan 18, 2026
6fd67d1
more
MichaelChirico Jan 18, 2026
fa08d64
need keep.source=TRUE
MichaelChirico Jan 18, 2026
76dca4a
new nofuzz
MichaelChirico Jan 18, 2026
12e4755
mistake
MichaelChirico Jan 18, 2026
30d7bcd
one more tweak
MichaelChirico Jan 18, 2026
193a20d
caught a live one!
MichaelChirico Jan 18, 2026
243f77b
a nofuzz
MichaelChirico Jan 18, 2026
06b8f57
need to strip comments
MichaelChirico Jan 18, 2026
3f69fcd
expect_lint thing
MichaelChirico Jan 18, 2026
de93bf8
another nofuzz for comments
MichaelChirico Jan 18, 2026
1b88a68
another false positive
MichaelChirico Jan 18, 2026
19a64bd
fix xml_find_first robustness issue
MichaelChirico Jan 19, 2026
532e4e4
one level further up needed for three parent::expr in XPath
MichaelChirico Jan 20, 2026
8f512b4
finish comment
MichaelChirico Jan 20, 2026
e77163d
Merge branch 'main' into nofuzz
MichaelChirico Jan 22, 2026
10909f5
Merge branch 'main' into nofuzz
MichaelChirico Jan 22, 2026
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
117 changes: 68 additions & 49 deletions .dev/ast_fuzz_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (
}

contents[wrong_number_def_idx] <-
'wrong_number_fmt <- "got %d lints instead of %d%s\\nFile contents:\\n%s"'
' wrong_number_fmt <- "got %d lints instead of %d%s\\nFile contents:\\n%s"'
contents[wrong_number_use_idx] <-
gsub("\\)$", ", readChar(file, file.size(file)))", contents[wrong_number_use_idx])
writeLines(contents, expect_lint_file)
Expand All @@ -66,61 +66,81 @@ withr::defer({

suppressMessages(pkgload::load_all())

can_parse <- \(lines) !inherits(tryCatch(parse(text = lines), error = identity), "error")
get_str <- \(x) tail(unlist(strsplit(x, ": ", fixed = TRUE)), 1L)

# beware lazy eval: originally tried adding a withr::defer() in each iteration, but
# this effectively only runs the last 'defer' expression as the names are only
# evaluated at run-time. So instead keep track of all edits in this object.
# this approach to implementing 'nofuzz' feels painfully manual, but I couldn't
# figure out how else to get 'testthat' to give us what we need -- the failures
# object in the reporter is frustratingly inconsistent in whether the trace
# exists, and even if it does, we'd have to text-mangle to get the corresponding
# file names out. Also, the trace 'srcref' happens under keep.source=FALSE,
# so we lose any associated comments anyway. even that would not solve the issue
# of getting top-level exclusions done for 'nofuzz start|end' ranges, except
# maybe if it enabled us to reuse lintr's own exclude() system.
# therefore we take this approach: pass over the test suite first and comment out
# any tests/units that have been marked 'nofuzz'. restore later. one consequence
# is there's no support for fuzzer-specific exclusion, e.g. we fully disable
# the unnecessary_placeholder_linter() tests because |> and _ placeholders differ.
# these have to be enabled/disabled at runtime as it's not possible to disentagle which
# fuzzer caused the error ex-post (and it might be the interaction of >1 at issue).
# an earlier approach was like the current 'nofuzz' -- just comment out the troublesome
# tests from being run at all. But that led to a very quickly growing set of tests being
# skipped totally, which also hid some issues that are surfaced by the current approach.
# Another idea would be to just leave the enable/disable calls as code in the test suite,
# but I prefer the current approach of leaving them as comments: (1) it's more consistent
# with the 'nolint' exclusion system and (2) it doesn't distract the casual reader as much.
test_restorations <- list()
for (test_file in list.files("tests/testthat", pattern = "^test-", full.names = TRUE)) {
xml <- read_xml(xmlparsedata::xml_parse_data(parse(test_file, keep.source = TRUE)))
# parent::* to catch top-level comments (exprlist). matches one-line nofuzz and start/end ranges.
nofuzz_lines <- xml_find_all(xml, "//COMMENT[contains(text(), 'nofuzz')]/parent::*")
if (length(nofuzz_lines) == 0L) next

test_original <- test_lines <- readLines(test_file)

for (nofuzz_line in nofuzz_lines) {
comments <- xml_find_all(nofuzz_line, "COMMENT[contains(text(), 'nofuzz')]")
comment_text <- xml_text(comments)
# handle start/end ranges first.
start_idx <- grep("nofuzz start", comment_text, fixed = TRUE)
end_idx <- grep("nofuzz end", comment_text, fixed = TRUE)
if (length(start_idx) != length(end_idx) || any(end_idx < start_idx)) {
stop(sprintf(
"Mismatched '# nofuzz start' (%s), '# nofuzz end' (%s) in %s",
toString(start_idx), toString(end_idx), test_file
))
test_lines <- readLines(test_file)
one_expr_idx <- grep("# nofuzz", test_lines, fixed = TRUE)
range_start_idx <- grep("^\\s*# fuzzer disable:", test_lines)
if (length(one_expr_idx) == 0L && length(range_start_idx) == 0L) next

test_original <- test_lines
pd <- getParseData(parse(test_file, keep.source = TRUE))

for (start_line in rev(one_expr_idx)) {
end_line <- start_line
while (end_line <= length(test_lines) && !can_parse(test_lines[start_line:end_line])) {
end_line <- end_line + 1L
}

comment_ranges <- Map(`:`,
as.integer(xml_attr(comments[start_idx], "line1")),
as.integer(xml_attr(comments[end_idx], "line1"))
)
for (comment_range in comment_ranges) {
test_lines[comment_range] <- paste("#", test_lines[comment_range])
if (end_line > length(test_lines)) {
stop("Unable to parse any expression starting from line ", start_line)
}
comment_txt <- subset(pd, line1 == start_line & token == "COMMENT", select = "text", drop = TRUE)
# blanket disable means the test cannot be run. this happens e.g. for tests of encoding
# that are too complicated to deal with in this GHA.
if (comment_txt == "# nofuzz") {
test_lines[start_line:end_line] <- ""
} else {
deactivated <- get_str(comment_txt)
test_lines <- c(
head(test_lines, start_line - 1L),
sprintf("deactivate_fuzzers('%s')", deactivated),
test_lines[start_line:end_line],
sprintf("activate_fuzzers('%s')", deactivated),
tail(test_lines, -end_line)
)
}
}

if (length(start_idx) > 0L && !any(!start_idx & !end_idx)) next
if (length(one_expr_idx)) {
writeLines(test_lines, test_file)
pd <- getParseData(parse(test_file, keep.source = TRUE))
range_start_idx <- grep("^\\s*# fuzzer disable:", test_lines)
}

# NB: one-line tests line expect_lint(...) # nofuzz are not supported,
# since the comment will attach to the parent test_that() & thus comment
# out the whole unit. Easiest solution is just to spread out those few tests for now.
comment_range <- as.integer(xml_attr(nofuzz_line, "line1")):as.integer(xml_attr(nofuzz_line, "line2"))
test_lines[comment_range] <- paste("#", test_lines[comment_range])
range_end_idx <- grep("^\\s*# fuzzer enable:", test_lines)

if (length(range_start_idx) != length(range_end_idx) || any(range_end_idx < range_start_idx)) {
stop(sprintf(
"Mismatched '# fuzzer disable' (%s), '# fuzzer enable' (%s) in %s",
toString(range_start_idx), toString(range_end_idx), test_file
))
}

for (ii in seq_along(range_start_idx)) {
start_line <- test_lines[range_start_idx[ii]]
test_lines[range_start_idx[ii]] <-
gsub("#.*", sprintf("deactivate_fuzzers('%s')", get_str(start_line)), start_line)
end_line <- test_lines[range_end_idx[ii]]
test_lines[range_end_idx[ii]] <-
gsub("#.*", sprintf("activate_fuzzers('%s')", get_str(end_line)), end_line)
}

writeLines(test_lines, test_file)
if (length(range_start_idx)) writeLines(test_lines, test_file)

test_restorations <- c(test_restorations, list(list(file = test_file, lines = test_original)))
}
withr::defer(for (restoration in test_restorations) writeLines(restoration$lines, restoration$file))
Expand All @@ -134,8 +154,7 @@ all_classes <- unlist(lapply(
reporter$get_results(),
\(test) lapply(test$results, \(x) class(x)[1L])
))
cat("Summary of test statuses:\n")
print(table(all_classes))
print(table(`Summary of test statuses:` = all_classes))

# ignore any test that failed for expected reasons, e.g. some known lint metadata changes
# about line numbers or the contents of the line. this saves us having to pepper tons of
Expand All @@ -160,7 +179,7 @@ if (length(invalid_failures) > 0L) {
\(x) sprintf("%s:%s", x$file, x$test),
character(1L)
)
cat("Some fuzzed tests failed unexpectedly!\n")
cat(sprintf("%d fuzzed tests failed unexpectedly!\n", length(invalid_failures)))
print(invalid_failures)
stop("Use # nofuzz [start|end] to mark false positives or fix any bugs.")
stop("Fix any bugs, or use '# nofuzz'/'# fuzzer [dis|en]able' to mark false positives.")
}
54 changes: 42 additions & 12 deletions .dev/maybe_fuzz_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ maybe_fuzz_content <- function(file, lines) {
file.copy(file, new_file, copy.mode = FALSE)
}

apply_fuzzers(new_file, fuzzers = list(
function_lambda_fuzzer,
pipe_fuzzer,
dollar_at_fuzzer,
comment_injection_fuzzer,
assignment_fuzzer
))
apply_fuzzers(new_file, fuzzers = .fuzzers$active)

new_file
}
Expand Down Expand Up @@ -106,19 +100,55 @@ apply_fuzzers <- function(f, fuzzers) {
return(invisible())
}

unedited <- lines <- readLines(f)
unedited <- lines <- readLines(f, warn = FALSE)
for (fuzzer in fuzzers) {
updated_lines <- fuzzer(pd, lines)
if (is.null(updated_lines) || identical(unedited, updated_lines)) next # skip some I/O if we can
if (is.null(updated_lines)) next # skip some I/O if we can
writeLines(updated_lines, f)
# check if our attempted edit introduced some error; skip applying this fuzzer only if so
# check if our attempted edit introduced some error
pd <- error_or_parse_data(f)
if (inherits(pd, "error")) {
writeLines(lines, f)
next
writeLines(unedited, f)
return(invisible())
}
lines <- readLines(f)
}

invisible()
}

.fuzzers <- new.env()
.fuzzers$active <- list(
assignment = assignment_fuzzer,
comment_injection = comment_injection_fuzzer,
dollar_at = dollar_at_fuzzer,
function_lambda = function_lambda_fuzzer,
pipe = pipe_fuzzer
)
.fuzzers$inactive <- list()

deactivate_fuzzers <- function(names_str) {
req <- unlist(strsplit(names_str, " ", fixed = TRUE))
if (!all(req %in% names(.fuzzers$active))) {
stop(sprintf(
"Invalid attempt to deactivate fuzzers: '%s'\n Currently active fuzzers: %s\n Currently inactive fuzzers: %s",
names_str, toString(names(.fuzzers$active)), toString(names(.fuzzers$inactive))
))
}
.fuzzers$inactive[req] <- .fuzzers$active[req]
.fuzzers$active[req] <- NULL
invisible()
}

activate_fuzzers <- function(names_str) {
req <- unlist(strsplit(names_str, " ", fixed = TRUE))
if (!all(req %in% names(.fuzzers$inactive))) {
stop(sprintf(
"Invalid attempt to activate fuzzers: '%s'\n Currently active fuzzers: %s\n Currently inactive fuzzers: %s",
names_str, toString(names(.fuzzers$active)), toString(names(.fuzzers$inactive))
))
}
.fuzzers$active[req] <- .fuzzers$inactive[req]
.fuzzers$inactive[req] <- NULL
invisible()
}
110 changes: 56 additions & 54 deletions R/any_duplicated_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,76 +63,78 @@ any_duplicated_linter <- function() {
# this lets us match on either side of EQ, where following-sibling
# assumes we are before EQ, preceding-sibling assumes we are after EQ.
length_unique_xpath_parts <- glue("
//{ c('EQ', 'NE', 'GT', 'LT') }
/parent::expr
/expr[
expr[1]/SYMBOL_FUNCTION_CALL[text() = 'length']
and expr/expr[1][
SYMBOL_FUNCTION_CALL[text() = 'unique']
and (
following-sibling::expr =
parent::expr
/parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'length']]
/following-sibling::expr
or following-sibling::expr[OP-DOLLAR or LBB]/expr[1] =
parent::expr
/parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'nrow']]
/following-sibling::expr
or parent::expr
expr/expr[
expr[1]/SYMBOL_FUNCTION_CALL[text() = 'length']
and expr/expr[1][
SYMBOL_FUNCTION_CALL[text() = 'unique']
and (
following-sibling::expr =
parent::expr
/parent::expr
/parent::expr
/expr[
SYMBOL[text() = '.N']
or (expr/SYMBOL_FUNCTION_CALL[text() = 'n'] and count(expr) = 1)
]
)
]
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'length']]
/following-sibling::expr
or following-sibling::expr[OP-DOLLAR or LBB]/expr[1] =
parent::expr
/parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'nrow']]
/following-sibling::expr
or parent::expr
/parent::expr
/parent::expr
/expr[
SYMBOL[text() = '.N']
or (expr/SYMBOL_FUNCTION_CALL[text() = 'n'] and count(expr) = 1)
]
)
]
]
")
length_unique_xpath <- paste(length_unique_xpath_parts, collapse = " | ")

distinct_xpath <- glue("
//{ c('EQ', 'NE', 'GT', 'LT') }
/parent::expr
/expr[
expr[1][
SYMBOL_FUNCTION_CALL[text() = 'uniqueN' or text() = 'n_distinct']
and (
following-sibling::expr =
parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'length' or text() = 'nrow']]
/following-sibling::expr
or following-sibling::expr[OP-DOLLAR or LBB]/expr[1] =
parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'nrow']]
/following-sibling::expr
or parent::expr
expr/expr[
expr[1][
SYMBOL_FUNCTION_CALL[text() = 'uniqueN' or text() = 'n_distinct']
and (
following-sibling::expr =
parent::expr
/parent::expr
/expr[
SYMBOL[text() = '.N']
or (expr/SYMBOL_FUNCTION_CALL[text() = 'n'] and count(expr) = 1)
]
)
]
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'length' or text() = 'nrow']]
/following-sibling::expr
or following-sibling::expr[OP-DOLLAR or LBB]/expr[1] =
parent::expr
/parent::expr
/expr
/expr[1][SYMBOL_FUNCTION_CALL[text() = 'nrow']]
/following-sibling::expr
or parent::expr
/parent::expr
/expr[
SYMBOL[text() = '.N']
or (expr/SYMBOL_FUNCTION_CALL[text() = 'n'] and count(expr) = 1)
]
)
]
]
")

uses_nrow_xpath <- "./parent::expr/expr/expr[1]/SYMBOL_FUNCTION_CALL[text() = 'nrow']"
uses_dtn_xpath <- "./parent::expr/expr/SYMBOL[text() = '.N']"
uses_dplyr_xpath <- "./parent::expr/expr/expr[1]/SYMBOL_FUNCTION_CALL[text() = 'n']"

Linter(linter_level = "expression", function(source_expression) {
xml <- source_expression$xml_parsed_content
# NB: need two parents given three parent::expr in XPath and stripped comments.
xml <- source_expression$xml_parsed_content |>
xml_find_all("//EQ | //NE | //GT | //LT") |>
xml_parent() |>
xml_parent() |>
strip_comments_from_subtree()

xml_calls <- source_expression$xml_find_function_calls("any")

any_duplicated_expr <- xml_find_all(xml_calls, any_duplicated_xpath)
Expand Down
22 changes: 12 additions & 10 deletions R/is_numeric_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ is_numeric_linter <- function() {

# testing things like is.numeric(x) || is.integer(x)
or_xpath <- glue("
//OR2
/parent::expr[
expr/{is_numeric_expr}
and expr/{is_integer_expr}
and
expr/{is_numeric_expr}/following-sibling::expr[1]
= expr/{is_integer_expr}/following-sibling::expr[1]
]
")
self::*[
expr/{is_numeric_expr}
and expr/{is_integer_expr}
and
expr/{is_numeric_expr}/following-sibling::expr[1]
= expr/{is_integer_expr}/following-sibling::expr[1]
]")

# testing class(x) %in% c("numeric", "integer")
class_xpath <- "
Expand All @@ -69,7 +67,11 @@ is_numeric_linter <- function() {
Linter(linter_level = "expression", function(source_expression) {
xml <- source_expression$xml_parsed_content

or_expr <- xml_find_all(xml, or_xpath)
or_expr <- xml |>
xml_find_all("//OR2") |>
xml_parent() |>
strip_comments_from_subtree() |>
xml_find_all(or_xpath)
or_lints <- xml_nodes_to_lints(
or_expr,
source_expression = source_expression,
Expand Down
Loading
Loading