Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styling resulted in code that isn't parsable. #1070

Closed
balthasars opened this issue Nov 24, 2022 · 4 comments · Fixed by #1088
Closed

Styling resulted in code that isn't parsable. #1070

balthasars opened this issue Nov 24, 2022 · 4 comments · Fixed by #1088

Comments

@balthasars
Copy link

Hi there!

Thank you for your ongoing work on this great package!

Dealing with legacy code, I encountered code like this:

styler::style_text(
'whatever <- function(x) {
    if (is.vector(x)){
        {if(is.Date(x)){ mbspalte <- 1}
            }  #Label "seit dd.mm.yyyy"
    }
}
'
)
#> Error in `value[[3L]]()`:
#> ! Styling resulted in code that isn't parsable. This should not happen. Please file a bug report on GitHub (https://github.com/r-lib/styler/issues) using a reprex.

#> Backtrace:
#>      ▆
#>   1. ├─styler::style_text("whatever <- function(x) {\n    if (is.vector(x)){\n        {if(is.Date(x)){ mbspalte <- 1}\n            }  #Label \"seit dd.mm.yyyy\"\n    }\n}\n")
#>   2. │ └─styler (local) transformer(text)
#>   3. │   └─... %>% ...
#>   4. ├─purrr::when(...)
#>   5. └─styler:::parse_transform_serialize_r(...)
#>   6.   └─styler:::verify_roundtrip(text, text_out, parsable_only = !parse_tree_must_be_identical(transformers))
#>   7.     └─rlang::with_handlers(...)
#>   8.       └─base::tryCatch(.expr, error = `<fn>`)
#>   9.         └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  10.           └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  11.             └─value[[3L]](cond)
#>  12.               └─rlang::abort(...)

Created on 2022-11-24 with reprex v2.0.2

Interestingly enough, removing the comment #Label "seit dd.mm.yyyy" resolves this issue.

styler::style_text(
  "whatever <- function(x) {
    if (is.vector(x)){
        {if(is.Date(x)){ mbspalte <- 1}
            }
    }
}
"
)
#> Warning: Could not use colored = TRUE, as the package prettycode is not
#> installed. Please install it if you want to see colored output or see `?
#> print.vertical` for more information.
#> whatever <- function(x) {
#>   if (is.vector(x)) {{ if (is.Date(x)) {
#>     mbspalte <- 1
#>   } }}
#> }

Created on 2022-11-24 with reprex v2.0.2

Thought this might be useful, cheers!

@IndrajeetPatil
Copy link
Collaborator

IndrajeetPatil commented Nov 24, 2022

Thanks for reporting, and also illustrating the workaround.

Definitely a bug.

@lorenzwalthert
Copy link
Collaborator

I supspect that it has to do with how new lines are added or removed around curly braces. I note that the R parser does not tolerate all ways you can potentially handle these. In addition, it would be useful if the output would also include the code that can't be parsed, instead of just this message.

@IndrajeetPatil
Copy link
Collaborator

IndrajeetPatil commented Nov 24, 2022

Slightly more minimal reprex (suitable for testing):

code <- "{{ 
# x 
}}"

styler::style_text(code)
#> Error in `value[[3L]]()`:
#> ! Styling resulted in code that isn't parsable. This should not happen. Please file a bug report on GitHub (https://github.com/r-lib/styler/issues) using a reprex.

Created on 2022-11-24 with reprex v2.0.2

This code is styled to {{ # x }}, which is not valid:

parse(text = "{{ # x }}")
#> Error in parse(text = "{{ # x }}"): <text>:2:0: unexpected end of input
#> 1: {{ # x }}
#>    ^

Created on 2022-11-24 with reprex v2.0.2

@lorenzwalthert
Copy link
Collaborator

Thanks guys. Of course, I should have known that line breaks can never be removed when they move code behind a comment...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants