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

Extract some shared logic to BlankLineSeparation #961

Merged
merged 4 commits into from Jul 14, 2020

Conversation

bquorning
Copy link
Collaborator

@bquorning bquorning commented Jul 10, 2020

In 0289cd8, a bunch of logic was extracted into BlankLineSeparation module. In this PR I extract another bit.

Please help me with the naming of the method. I am not sure if check_missing_separating_lines is the best name.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • [-] Added tests.
  • [-] Updated documentation.
  • [-] Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@bquorning bquorning marked this pull request as ready for review July 10, 2020 20:00
@Darhazer
Copy link
Member

How about missing_separating_line_offense
I also wonder if we can have a message method in order not to initialize a message without having an offense.

@Darhazer
Copy link
Member

Another option for a cleaner API would be

missing_separating_line_offsense(node) do |message|
  message << format(....)
end

this way it's obvious that the offense is something that could be registered. But I don't quite like providing the argument just for the sake of modifying it with the message, and yielding or returning the message out of the block is just non-intuitive API

Anyway, those are just a couple of suggestions

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Wondering if the message logic can be extracted somehow as well, at least the MSG itself.

lib/rubocop/rspec/blank_line_separation.rb Outdated Show resolved Hide resolved
lib/rubocop/rspec/blank_line_separation.rb Show resolved Hide resolved
@@ -31,14 +31,9 @@ class EmptyLineAfterExampleGroup < Cop

def on_block(node)
return unless example_group?(node)
Copy link
Member

Choose a reason for hiding this comment

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

Will it work as

example_group?(node) do |example_group|
  msg = format(MSG, example_group: example_group.method_name)
  check_missing_separating_lines(example_group, message: msg)
end

Copy link
Member

Choose a reason for hiding this comment

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

Same above for EmptyLineAfterExample and below for EmptyLineAfterHook.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be neat, but we’d have to add a capture in the right place in RuboCop::RSpec::Language::SelectorSet. Something to explore in a separate PR, maybe?

Copy link
Member

Choose a reason for hiding this comment

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

It still could be

example_group?(node) do
  msg = format(MSG, example_group: node.method_name)
  check_missing_separating_lines(node, message: msg)
end

but then I would remove the ? from the name. Still, something that could be explored in a separate PR

Copy link
Member

Choose a reason for hiding this comment

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

Up to you. Looks good as is.
There's always an indefinite space for improvement.

@bquorning
Copy link
Collaborator Author

How about missing_separating_line_offense

I like it.

I also wonder if we can have a message method in order not to initialize a message without having an offense.

https://github.com/rubocop-hq/rubocop/blob/v0.88.0/lib/rubocop/cop/base.rb#L86-L91 says “Cops are discouraged to override this (#message); instead pass your message directly”.

I changed the logic to yield the method name of the offending node, to let the calling code generate the error message. Please review again.

@bquorning bquorning force-pushed the extract-to-blank-line-separation branch from e872b3c to 8f50d89 Compare July 13, 2020 19:15
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

I hate this part of Ruby where an included module can't access including class's constants directly. Just can't figure out the reasoning behind this behaviour.

There's a way around, though. If you decide to further reduce the amount of code in cops by pushing it to the module, a self.class::MSG will have the access to cop's MSG rather than module's. I'm on the fence with this though, as constructs like that always wake alarm my active thinking and get me out of the flow. On the other hand, it would be possible to get rid of yield, and instead of passing a block with a single format call, it would be possible to pass the argument name directly, i.e.:

missing_separating_line_offense(node, :hook)

or even make an agreement to name the parameter in MSGs (or don't use a named param):

        MSG = 'Add an empty line after `%<name>s`.'
# or
        MSG = 'Add an empty line after `%s`.'

and in the cops then:

missing_separating_line_offense(node)

Still, with a self.class::MSG on the other side of the scales, it's a tradeoff. Pick a solution that appeals to you most.
I'm pretty happy with how it is now.

lib/rubocop/cop/rspec/empty_line_after_subject.rb Outdated Show resolved Hide resolved
@pirj
Copy link
Member

pirj commented Jul 14, 2020

Please merge at will 👍

Also, add "an" to error message.
Also, removed redundant "block" from the message.
For consistency, let's call it an "empty line" everywhere.
@bquorning bquorning force-pushed the extract-to-blank-line-separation branch from 8f50d89 to 51d593a Compare July 14, 2020 20:36
@bquorning bquorning merged commit 1be4c09 into master Jul 14, 2020
@bquorning bquorning deleted the extract-to-blank-line-separation branch July 14, 2020 20:52
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.

None yet

3 participants