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 #9608] Fix a false positive for Layout/EmptyLineAfterGuardClause #9609

Conversation

koic
Copy link
Member

@koic koic commented Mar 17, 2021

Fixes #9608.

This PR fixes a false positive for Layout/EmptyLineAfterGuardClause when using guard clause is after rubocop:enable comment.


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 Mar 17, 2021

I'm assuming that's problem existing in other cops checking for a blank line after something.

@@ -41,20 +41,21 @@ class EmptyLineAfterGuardClause < Base

MSG = 'Add empty line after guard clause.'
END_OF_HEREDOC_LINE = 1
RUBOCOP_ENABLE_COMMENT_PATTERN = /#\s*rubocop\s*:\s*enable/.freeze

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use DirectiveComment here

@@ -85,8 +86,10 @@ def contains_guard_clause?(node)
node.if_branch&.guard_clause?
end

def next_line_empty?(line)
processed_source[line].blank?
def next_line_empty_or_rubocop_enable_comment?(line)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to still hold empty line after directive comment. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. There should still be an empty line, but it should come right after the enable directive.

@AndreiEres
Copy link

I'm assuming that's problem existing in other cops checking for a blank line after something.

If it is a general behaviour, maybe we can add an extra helper class to check it?

@@ -199,6 +199,28 @@ def foo
RUBY
end

it 'registers and corrects when using guard clause is after `rubocop:disable` comment' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theres should also be a test making sure an empty line is required after the enable directive.

@dvandersluis
Copy link
Member

Should this be specific to directive comments, or maybe generalized to any comment (or multiple lines of comment) after the clause?

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 17, 2021

@dvandersluis Only directives.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 24, 2021

@koic ping :-)

@koic koic force-pushed the fix_false_positive_for_layout_empty_line_after_guard_clause branch from f424686 to ccf2827 Compare March 25, 2021 05:47
…rdClause`

Fixes rubocop#9608.

This PR fixes a false positive for `Layout/EmptyLineAfterGuardClause`
when using guard clause is after `rubocop:enable` comment.
@koic koic force-pushed the fix_false_positive_for_layout_empty_line_after_guard_clause branch from ccf2827 to ccd2ddd Compare March 25, 2021 05:53
@koic
Copy link
Member Author

koic commented Mar 25, 2021

Thank you for reviews. I updated this PR and I will proceed separately for similar other cops.

@bbatsov bbatsov merged commit bf7ed9d into rubocop:master Mar 25, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 25, 2021

Thanks!

@koic koic deleted the fix_false_positive_for_layout_empty_line_after_guard_clause branch March 25, 2021 07:33
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.

Make Layout/EmptyLineAfterGuardClause accept RuboCop directive after guard clause
4 participants