r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
725 stars 71 forks source link

Does styler include a transformer that can replace T with TRUE and F with FALSE in R code? #1156

Closed vertesy closed 10 months ago

vertesy commented 10 months ago
  1. Does styler include a transformer that can replace T with TRUE and F with FALSE in R code?
  2. Ideally it would be generic to so that I could also use it to replace shorthand e.g. p0 > paste0
  3. If none exists, is there simple transformer example that I could use to model on?

Thank you!

lorenzwalthert commented 10 months ago

it's generally unsafe to do that since you could have

T <- 'hi there'

You would need advanced static code analysis tools to ensure you only convert a true TRUE. That's out of scope for {styler}. But {lintr} offers checks for that so you can fix it manually.

Having said that, there are no generic transformers that replace arbitrary text and I would strongly advise against such replacements since they make styling unpredictable. Of course, you could write your own, there is documentation on that too.

vertesy commented 10 months ago

It looks a bit complicated to me, so I wont implement it via styler, but thank you for the explanation and warning. (Since the file is under git, I can see if something odd is replaced)

vertesy commented 10 months ago

Solved in PackageTools's replace_tf_with_true_false(). Works completely safely with strict = TRUE (default).