Skip to content

Gaps in documentation for custom style guides #1170

@sorhawell

Description

@sorhawell

Hi stylers

Thank you for a very useful package. I think I have found 1 or perhaps 2 bugs:

  • It appears that style_text() ignores custom style unless cache is disabled.
  • It appears that style_file() w/o cache has unintended side effects such that it can only work once with a custom style. To work again the session must be restarted
# Prepare transformers
rpolars_style = function() {

  # derive from tidyverse
  transformers = styler::tidyverse_style()

  # reverse tranformer to make <- into =
  transformers$token$force_assignment_op = function (pd) {
    to_replace = pd$token == "LEFT_ASSIGN"
    pd$token[to_replace] = "EQ_ASSIGN"
    pd$text[to_replace] = "="
    pd
  }

  transformers
}


# CACHE does not take into account transformers
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # good
#> foo = 42
#> bar = TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = styler::tidyverse_style()) # bad
#> foo <- 42
#> bar = TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # bad
#> foo <- 42
#> bar = TRUE

styler::cache_clear(ask = FALSE)
styler::cache_deactivate()
#> Deactivated cache.
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # good
#> foo = 42
#> bar = TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = styler::tidyverse_style()) # good
#> foo <- 42
#> bar <- TRUE
styler::style_text("foo <- 42; bar = TRUE", transformers = rpolars_style()) # good
#> foo = 42
#> bar = TRUE



writeLines("foo <- 42; bar = TRUE", "deleteme.R")
readLines("deleteme.R")
#> [1] "foo <- 42; bar = TRUE"
styler::style_file("deleteme.R", transformers = rpolars_style()) # good
#> Styling  1  files:
#>  deleteme.R ℹ 
#> ────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    0   File unchanged.
#> ℹ    1   File changed.
#> ✖    0   Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
readLines("deleteme.R")
#> [1] "foo = 42"   "bar = TRUE"
styler::style_file("deleteme.R", transformers = styler::tidyverse_style()) # good
#> Styling  1  files:
#>  deleteme.R ℹ 
#> ────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    0   File unchanged.
#> ℹ    1   File changed.
#> ✖    0   Styling threw an error.
#> ────────────────────────────────────────
#> Please review the changes carefully!
readLines("deleteme.R")
#> [1] "foo <- 42"   "bar <- TRUE"
styler::style_file("deleteme.R", transformers = rpolars_style()) # bad
#> Styling  1  files:
#>  deleteme.R ✔ 
#> ────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    1   File unchanged.
#> ℹ    0   File changed.
#> ✖    0   Styling threw an error.
#> ────────────────────────────────────────
readLines("deleteme.R")
#> [1] "foo <- 42"   "bar <- TRUE"
styler::cache_clear(ask = FALSE)
styler::cache_deactivate()
#> Deactivated cache.
styler::style_file("deleteme.R", transformers = rpolars_style()) # bad
#> Styling  1  files:
#>  deleteme.R ✔ 
#> ────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    1   File unchanged.
#> ℹ    0   File changed.
#> ✖    0   Styling threw an error.
#> ────────────────────────────────────────
readLines("deleteme.R")
#> [1] "foo <- 42"   "bar <- TRUE"

Created on 2023-12-11 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions