You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
if-else with '=' assignment, wrong curly placement #259
Thanks @lefec for reaching out. This is indeed very unfortunate. There are two aspects to it.
If you replace the assignment = with <-, you won't get invalid code back. The problem here is really an inconsistency in the R parser discussed in inconsistency in getParseData? #100.
The indention of the braces will still not be correct, i.e.
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-10-25
library(styler)
style_text("x = 5if(x >= 5)y <- TRUE else y <- FALSE")
#> x <- 5#> if (x >= 5) {#> y <- TRUE#> } else {#> y <- FALSE#> }
Which is an issue already reported #257 and which I hope to solve soon.
@krlmlr What do you suggest? Should we just disable adding braces if there is any = assignment in the code in order to prevent (1) from happening? Invalidating code is the worst styler can do anyways and I think it should be avoided at any cost. Eventually we need to fix the parsing inconsistency but I think that might take some time.
What does it take to put the EX_ASSIGN and the expr/NUM_CONST where it belongs? Can we compute the parent information ourselves from line + col data, to avoid relying on the (sometimes) faulty parent ID in the parse data?
I haven't looked into the solution Jim Hester provided in #100, but when mentioning #100, I just meant to "solve" the parsing problem, i.e. getting a consistent parse data (that is, the same parsing data as if <- was used instead of =, just with text being different for the assignment token) which would solve the problem. That involves getting the right parent from the parse data. I think this is not trivial. We can also parse, replace the tokens EQ_ASSIGN with arrow assignment, serialise, parse again and replace text from the second serialisation with the text from the first, which is not particularly elegant.
Seems that all we need to do here is to wrap EQ_ASSIGN sequences into a separate expression. Maybe this operator is special because it has lowest precedence?
Walk all nests
Look at sequences of EQ_ASSIGN
If there is one in the nest, wrap everything between the token before the first EQ_ASSIGN and the token after the last EQ_ASSIGN into a separate expression, very similarly to the addition of braces (Wrap expr in expr before enclosing with curly braces #263).
Ok, I see. Looks like that could be a valid approach. Need to look at more examples maybe. We could try to fix it in the nested structure or the flat structure. If we fix it in the flat structure (that is, reassigning parents and ids before nesting), this might have the advantage that we fix the problem early in the food chain, so other packages / tasks that do nesting in a different way than we do might benefit. However, that might be potentially more difficult.
I think you can't do it exactly that way because in the case below, two = appear on the same level but there is an else in between that you don't want to wrap.
Activity
lorenzwalthert commentedon Oct 25, 2017
Thanks @lefec for reaching out. This is indeed very unfortunate. There are two aspects to it.
=with<-, you won't get invalid code back. The problem here is really an inconsistency in the R parser discussed in inconsistency in getParseData? #100.Which is an issue already reported #257 and which I hope to solve soon.
@krlmlr What do you suggest? Should we just disable adding braces if there is any
=assignment in the code in order to prevent (1) from happening? Invalidating code is the worst styler can do anyways and I think it should be avoided at any cost. Eventually we need to fix the parsing inconsistency but I think that might take some time.krlmlr commentedon Oct 25, 2017
I understand that even if #100 is closed the underlying problems are not resolved completely:
What does it take to put the
EX_ASSIGNand theexpr/NUM_CONSTwhere it belongs? Can we compute the parent information ourselves from line + col data, to avoid relying on the (sometimes) faulty parent ID in the parse data?lorenzwalthert commentedon Oct 25, 2017
I haven't looked into the solution Jim Hester provided in #100, but when mentioning #100, I just meant to "solve" the parsing problem, i.e. getting a consistent parse data (that is, the same parsing data as if
<-was used instead of=, just withtextbeing different for the assignment token) which would solve the problem. That involves getting the right parent from the parse data. I think this is not trivial. We can also parse, replace the tokens EQ_ASSIGN with arrow assignment, serialise, parse again and replace text from the second serialisation with the text from the first, which is not particularly elegant.krlmlr commentedon Oct 26, 2017
Compare the parse tree between arrow assignment and equals assignment:
Seems that all we need to do here is to wrap
EQ_ASSIGNsequences into a separate expression. Maybe this operator is special because it has lowest precedence?EQ_ASSIGNEQ_ASSIGNand the token after the lastEQ_ASSIGNinto a separate expression, very similarly to the addition of braces (Wrap expr in expr before enclosing with curly braces #263).lorenzwalthert commentedon Oct 26, 2017
Ok, I see. Looks like that could be a valid approach. Need to look at more examples maybe. We could try to fix it in the nested structure or the flat structure. If we fix it in the flat structure (that is, reassigning parents and ids before nesting), this might have the advantage that we fix the problem early in the food chain, so other packages / tasks that do nesting in a different way than we do might benefit. However, that might be potentially more difficult.
lorenzwalthert commentedon Nov 2, 2017
Ok, I guess for the time being I just want to follow your suggestion and fix the problem in the nested parse data.
lorenzwalthert commentedon Nov 2, 2017
I think you can't do it exactly that way because in the case below, two
=appear on the same level but there is an else in between that you don't want to wrap.krlmlr commentedon Nov 2, 2017
Good point. I guess we need to look for sequences of
EQ_ASSIGNthat are interspersed with exactly one other token.lorenzwalthert commentedon Nov 2, 2017
Yes, I agree. So I am just trying to first split a nest into blocks, each containing one valid
=expression and then map over the blocks like this.Well not exactly, but somehow like this.
krlmlr commentedon Nov 2, 2017
Can you use
diff()?lorenzwalthert commentedon Nov 2, 2017
I modified
create_treee()so it can also return just the structure of an expression, i.eI think I managed to solve the problem, so we have
Will push later.
lorenzwalthert commentedon Nov 8, 2017
@lefec finally hoping to close this with #276.