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

[Fix #9429] Fix Style/NegatedIfElseCondition autocorrect to keep comments in correct branch #9436

Merged
merged 1 commit into from Mar 7, 2021

Conversation

tejasbubane
Copy link
Contributor

Closes #9429


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2021

@tejasbubane ping :-)

@tejasbubane
Copy link
Contributor Author

@koic @bbatsov Fixed.

end
RUBY
end

Copy link
Member

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

Copy link
Member

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.

Copy link
Contributor Author

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">]}

Copy link
Member

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!

@koic koic merged commit 7addc3d into rubocop:master Mar 7, 2021
@tejasbubane tejasbubane deleted the fix-9429 branch May 1, 2021 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NegatedIfElseCondition autocorrect leaves comments behind
3 participants