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
I think Style/IfUnlessModifier doesn't correctly handle the case when there is a first-line comment (right after the if keyword) AND some code right after the end keyword. If you correct this code with rubocop -a it would put the comment right after the end keyword, before the code that was there previously. This basically makes the code invalid after correction.
This probably can be better seen in the example below.
I think in such a case we should stop auto-correction because it's pretty non-deterministic: where would that comment should go? Did it belong to the if-end condition? In this case it should go right after it, before the next code. But that's impossible in a single-line modifier form, unless we use these /* comments */ but we probably wouldn't want to do this, so better not to auto-correct.
I can provide a PR if it seems right.
Steps to reproduce the problem
We have file test.rb with the following code:
[
1, if foo # this is due to ...
2
end, 3
]
We run rubocop test.rb --only Style/IfUnlessModifier -a.
Actual behavior
[
1, (2 if foo) # this is due to ..., 3
]
Expected behavior
Everything stays the same and no offense is given (I think it's the right way):
[
1, if foo # this is due to ...
2
end, 3
]
or somehow it's fixed to
[
1, (2 if foo), 3 # this is due to ...
]
but in this case the comment is in the wrong place, or it's fixed to
[
1, (2 if foo) /* # this is due to ... */, 3
]
but we probably don't want to use this syntax for comments.
I think
Style/IfUnlessModifier
doesn't correctly handle the case when there is a first-line comment (right after theif
keyword) AND some code right after theend
keyword. If you correct this code withrubocop -a
it would put the comment right after theend
keyword, before the code that was there previously. This basically makes the code invalid after correction.This probably can be better seen in the example below.
I think in such a case we should stop auto-correction because it's pretty non-deterministic: where would that comment should go? Did it belong to the if-end condition? In this case it should go right after it, before the next code. But that's impossible in a single-line modifier form, unless we use these
/* comments */
but we probably wouldn't want to do this, so better not to auto-correct.I can provide a PR if it seems right.
Steps to reproduce the problem
We have file
test.rb
with the following code:We run
rubocop test.rb --only Style/IfUnlessModifier -a
.Actual behavior
Expected behavior
Everything stays the same and no offense is given (I think it's the right way):
or somehow it's fixed to
but in this case the comment is in the wrong place, or it's fixed to
but we probably don't want to use this syntax for comments.
RuboCop version
0.89.0 (using Parser 2.7.1.4, rubocop-ast 0.2.0, running on ruby 2.5.1 x86_64-darwin18)
The text was updated successfully, but these errors were encountered: