From e4da5ae78c242c643eb45a1ebc9f04d40f3279a4 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 7 Dec 2024 08:35:49 +0530 Subject: [PATCH 1/2] Remove spaces around dollar operator --- R/rules-spaces.R | 3 ++- R/style-guides.R | 4 ++-- tests/testthat/indention_operators/dollar_R6-in.R | 2 +- tests/testthat/indention_operators/dollar_R6-out.R | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 54f4bf289..6dad9d34e 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -341,9 +341,10 @@ set_space_after_bang_bang <- function(pd_flat) { pd_flat } -remove_space_before_dollar <- function(pd_flat) { +remove_space_around_dollar <- function(pd_flat) { dollar_after <- (pd_flat$token == "'$'") & (pd_flat$lag_newlines == 0L) dollar_before <- lead(dollar_after, default = FALSE) + pd_flat$spaces[dollar_after] <- 0L pd_flat$spaces[dollar_before] <- 0L pd_flat } diff --git a/R/style-guides.R b/R/style-guides.R index 93ea89dd2..51e758c2c 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -108,7 +108,7 @@ tidyverse_style <- function(scope = "tokens", remove_space_after_opening_paren = remove_space_after_opening_paren, remove_space_after_excl = remove_space_after_excl, set_space_after_bang_bang = set_space_after_bang_bang, - remove_space_before_dollar = remove_space_before_dollar, + remove_space_around_dollar = remove_space_around_dollar, remove_space_after_fun_dec = remove_space_after_fun_dec, remove_space_around_colons = remove_space_around_colons, start_comments_with_space = partial(start_comments_with_space, @@ -205,7 +205,7 @@ tidyverse_style <- function(scope = "tokens", # remove_space_after_opening_paren = c("'('", "'['", "LBB"), remove_space_after_excl = "'!'", set_space_after_bang_bang = "'!'", - remove_space_before_dollar = "'$'", + remove_space_around_dollar = "'$'", remove_space_after_fun_dec = "FUNCTION", remove_space_around_colons = c("':'", "NS_GET_INT", "NS_GET"), start_comments_with_space = "COMMENT", diff --git a/tests/testthat/indention_operators/dollar_R6-in.R b/tests/testthat/indention_operators/dollar_R6-in.R index de575d6a5..399ebe5ea 100644 --- a/tests/testthat/indention_operators/dollar_R6-in.R +++ b/tests/testthat/indention_operators/dollar_R6-in.R @@ -1,4 +1,4 @@ x$ - add(10) $ + add(10) $ add(10) $ add(10)$sum + 3 diff --git a/tests/testthat/indention_operators/dollar_R6-out.R b/tests/testthat/indention_operators/dollar_R6-out.R index e4cf03219..25744ef23 100644 --- a/tests/testthat/indention_operators/dollar_R6-out.R +++ b/tests/testthat/indention_operators/dollar_R6-out.R @@ -1,4 +1,4 @@ x$ - add(10)$ + add(10)$add(10)$ add(10)$sum + 3 From bf5c500423df894eba40bc37b048f76ce0c096c3 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 7 Dec 2024 08:56:47 +0530 Subject: [PATCH 2/2] fix --- R/rules-spaces.R | 2 +- tests/testthat/indention_operators/dollar_R6-in.R | 2 +- tests/testthat/indention_operators/dollar_R6-out.R | 2 +- tests/testthat/spacing/dollar-in.R | 5 +++++ tests/testthat/spacing/dollar-out.R | 5 +++++ tests/testthat/test-spacing.R | 7 +++++++ 6 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/testthat/spacing/dollar-in.R create mode 100644 tests/testthat/spacing/dollar-out.R diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 6dad9d34e..835b25e91 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -344,8 +344,8 @@ set_space_after_bang_bang <- function(pd_flat) { remove_space_around_dollar <- function(pd_flat) { dollar_after <- (pd_flat$token == "'$'") & (pd_flat$lag_newlines == 0L) dollar_before <- lead(dollar_after, default = FALSE) - pd_flat$spaces[dollar_after] <- 0L pd_flat$spaces[dollar_before] <- 0L + pd_flat$spaces[dollar_after] <- 0L pd_flat } diff --git a/tests/testthat/indention_operators/dollar_R6-in.R b/tests/testthat/indention_operators/dollar_R6-in.R index 399ebe5ea..a3bf2f591 100644 --- a/tests/testthat/indention_operators/dollar_R6-in.R +++ b/tests/testthat/indention_operators/dollar_R6-in.R @@ -1,4 +1,4 @@ - x$ + x$ add(10)$ add(10) $ add(10) $ add(10)$sum + 3 diff --git a/tests/testthat/indention_operators/dollar_R6-out.R b/tests/testthat/indention_operators/dollar_R6-out.R index 25744ef23..8111d3582 100644 --- a/tests/testthat/indention_operators/dollar_R6-out.R +++ b/tests/testthat/indention_operators/dollar_R6-out.R @@ -1,4 +1,4 @@ -x$ +x$add(10)$ add(10)$add(10)$ add(10)$sum + 3 diff --git a/tests/testthat/spacing/dollar-in.R b/tests/testthat/spacing/dollar-in.R new file mode 100644 index 000000000..3ac9a459a --- /dev/null +++ b/tests/testthat/spacing/dollar-in.R @@ -0,0 +1,5 @@ +mtcars $wt + +mtcars$ wt + +mtcars $ wt diff --git a/tests/testthat/spacing/dollar-out.R b/tests/testthat/spacing/dollar-out.R new file mode 100644 index 000000000..070ddc731 --- /dev/null +++ b/tests/testthat/spacing/dollar-out.R @@ -0,0 +1,5 @@ +mtcars$wt + +mtcars$wt + +mtcars$wt diff --git a/tests/testthat/test-spacing.R b/tests/testthat/test-spacing.R index 7fdb6e55a..20ae4c695 100644 --- a/tests/testthat/test-spacing.R +++ b/tests/testthat/test-spacing.R @@ -76,3 +76,10 @@ test_that("spacing around square brackets / braces", { transformer = style_text, strict = TRUE ), NA) }) + +test_that("spacing around dollar", { + expect_warning(test_collection( + "spacing", "dollar", + transformer = style_text, strict = TRUE + ), NA) +})