Skip to content

Commit a5fdb4c

Browse files
Copilotm7pr
andcommitted
Move function calls to right side instead of filtering them out completely
Co-authored-by: m7pr <[email protected]>
1 parent 8208b4e commit a5fdb4c

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

R/utils-get_code_dependency.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,25 +301,30 @@ extract_occurrence <- function(pd) {
301301
sym_cond <- rev(sym_cond)
302302
}
303303

304-
after <- match(min(x$id[assign_cond]), sort(x$id[c(min(assign_cond), sym_cond)])) - 1
305-
306304
# Separate symbols before and after assignment
307305
symbols_before_assign <- sym_cond[x$id[sym_cond] < min(x$id[assign_cond])]
308306
symbols_after_assign <- sym_cond[x$id[sym_cond] > min(x$id[assign_cond])]
309307

310-
# Filter out function calls from left side of assignment (before assignment operator)
308+
# Move function calls from left side to right side of assignment
311309
# Function calls should only appear as dependencies (right side), not as assignment targets
310+
function_calls_on_left <- c()
312311
if (length(symbols_before_assign) > 0) {
313312
is_function_call_before <- x[symbols_before_assign, "token"] == "SYMBOL_FUNCTION_CALL"
313+
function_calls_on_left <- symbols_before_assign[is_function_call_before]
314314
symbols_before_assign <- symbols_before_assign[!is_function_call_before]
315315
}
316316

317-
# Combine all symbols (filtered left side + all right side)
318-
filtered_sym_cond <- c(symbols_before_assign, symbols_after_assign)
317+
# Combine symbols: filtered left side + all right side + moved function calls
318+
filtered_sym_cond <- c(symbols_before_assign, symbols_after_assign, function_calls_on_left)
319+
320+
# Update the after position based on filtered symbols (only non-function symbols on left)
321+
if (length(symbols_before_assign) > 0) {
322+
after <- match(min(x$id[assign_cond]), sort(x$id[c(min(assign_cond), symbols_before_assign)])) - 1
323+
} else {
324+
after <- 0
325+
}
319326

320-
# Update the after position based on filtered symbols
321327
if (length(filtered_sym_cond) > 0) {
322-
after <- match(min(x$id[assign_cond]), sort(x$id[c(min(assign_cond), filtered_sym_cond)])) - 1
323328
ans <- append(x[filtered_sym_cond, "text"], "<-", after = max(1, after))
324329
} else {
325330
ans <- "<-"

tests/testthat/test-fix-function-call-dependencies.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ testthat::test_that("function calls should not appear on left side of assignment
2323
# In this case, only 'ADMH' should be on the left side
2424
testthat::expect_true("ADMH" %in% left_side1,
2525
info = "Variable 'ADMH' should appear on left side as it's being assigned to")
26+
27+
# Function calls should still appear as dependencies on the right side
28+
right_side1 <- if(length(assign_pos1) > 0) deps1[seq(assign_pos1[1] + 1, length(deps1))] else character(0)
29+
testthat::expect_true("c" %in% right_side1,
30+
info = "Function 'c' should appear on right side as a dependency")
2631
})
2732

2833
testthat::test_that("complex assignment with function calls handles dependencies correctly", {
@@ -48,6 +53,8 @@ testthat::test_that("complex assignment with function calls handles dependencies
4853
# Functions can be on right side as dependencies
4954
testthat::expect_true("c" %in% right_side_admh,
5055
info = "Function 'c' can appear on right side as a dependency")
56+
testthat::expect_true("col_labels" %in% right_side_admh,
57+
info = "Function 'col_labels' should be moved to right side as a dependency")
5158

5259
# Variables being modified should be on left side
5360
testthat::expect_true("ADMH" %in% left_side_admh,

0 commit comments

Comments
 (0)