diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 54f4bf289..835b25e91 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -341,10 +341,11 @@ 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_before] <- 0L + pd_flat$spaces[dollar_after] <- 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..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$ - add(10) $ + 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..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$ - add(10)$ +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) +})