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
[Fix #9429] Fix Style/NegatedIfElseCondition
autocorrect to keep comments in correct branch
#9436
Conversation
@tejasbubane ping :-) |
ac91593
to
63fd2d7
Compare
…keep comments in correct branch Closes rubocop#9429
end | ||
RUBY | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fixing. There's one more thing. Can you add and pass the following test case ending in comments? (I think it's an edge case 😅 )
it 'registers and corrects an offense when using negated condition and the end of branches are comment' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
# part B
# and foo is 39
foo = 39
# end of `then` body comment
else
# part A
# and foo is 42
foo = 42
# end of `else` body comment
end
RUBY
expect_correction(<<~RUBY)
if condition.nil?
# part A
# and foo is 42
foo = 42
# end of `else` body comment
else
# part B
# and foo is 39
foo = 39
# end of `then` body comment
end
RUBY
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tejasbubane Can you fix this issue with this PR? I think it's an edge case, so I think it can be merged and improved even at present if you don't have time for this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koic I tried handling this case but seems like this needs to be fixed at parser level. Below is output of processed_source.ast_with_comments
. Note the comment just above else if taken as part of else statement. Is there any other rubocop API that I can use and I am unaware of?
irb(#<RuboCop::Cop::Style::NegatedIfElseCondition:0x00005588841deb30>):007:0> pp processed_source.ast_with_comments
{s(:lvasgn, :foo,
s(:int, 39))=>
[#<Parser::Source::Comment (string):2:3 "# part B">,
#<Parser::Source::Comment (string):3:3 "# and foo is 39">],
s(:lvasgn, :foo,
s(:int, 42))=>
[#<Parser::Source::Comment (string):5:3 "# end of `then` body comment">,
#<Parser::Source::Comment (string):7:3 "# part A">,
#<Parser::Source::Comment (string):8:3 "# and foo is 42">],
s(:if,
s(:send,
s(:send,
s(:send, nil, :condition), :nil?), :!),
s(:lvasgn, :foo,
s(:int, 39)),
s(:lvasgn, :foo,
s(:int, 42)))=>
[#<Parser::Source::Comment (string):10:3 "# end of `else` body comment">]}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for investigating. This issue may require deeper investigation to be resolved. And this PR has improved auto-correction, so let's merge it with the current status. Thank you again!
Closes #9429
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.