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

Add new Style/EmptyLinesAroundBeginBody cop #3984

Merged
merged 4 commits into from Feb 4, 2017

Conversation

pocke
Copy link
Collaborator

@pocke pocke commented Jan 29, 2017

Example

 # good
begin
  foo
end

 # bad
begin

  foo

end

Note

This cop does not detect the following code.

begin
  foo
  # Empty line is here!

rescue
  bar
end

begin
  foo
rescue

  # Empty line is here!
  bar
end

begin
  foo
  # Empty line is here!

ensure
  bar
end

There are two reasons for this.

First, the feature can't be implemented by check method that is defined by EmptyLinesAroundBody mixin.

Second, I think the feature should be implemented as another cop(e.g. Style/EmptyLinesAroundBeginSection).
I would like to check empty lines around begin-end block body always.
However, I add an empty line around ensure, rescue, else keywords sometimes.

begin
  foo
  bar
  baz

rescue
  something
end

So, the features should be separateness to disable the one only.

I'll implement the cop for empty lines around the keywords with another PR.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@pocke pocke force-pushed the empty_lines_around_begin_block branch from 2b96b47 to e341e6c Compare January 29, 2017 12:26
Example
------

```ruby
 # good
begin
  foo
end

 # bad
begin

  foo

end
```

Note
--------

This cop does not detect the following code.

```ruby
begin
  foo
  # Empty line is here!

rescue
  bar
end

begin
  foo
rescue

  # Empty line is here!
  bar
end

begin
  foo
  # Empty line is here!

ensure
  bar
end
```

There are two reasons for this.

First, the feature can't be implemented by `check` method that is defined by `EmptyLinesAroundBody` mixin.

Second, I think the feature should be implemented as another cop(e.g. `Style/EmptyLinesAroundBeginSection`).
I would like to check empty lines around begin-end block body always.
However, I add an empty line around `ensure`, `rescue`, `else` keywords sometimes.

```ruby
begin
  foo
  bar
  baz

rescue
  something
end
```

So, the features should be separateness to disable the one only.

I'll implement the cop for empty lines around the keywords with another PR.
@@ -223,6 +223,10 @@ Style/EmptyLinesAroundAccessModifier:
Description: "Keep blank lines around access modifiers."
Enabled: true

Style/EmptyLinesAroundBeginBody:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't we have a style guide rule for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, we don't have the style guide. And we also don't have style guides for other EmptyLines* cops.
I'll add the documentation.

class EmptyLinesAroundBeginBody < Cop
include EmptyLinesAroundBody

KIND = 'begin'.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks a bit confusing. Maybe we should turn it into a method instead?

Copy link
Collaborator Author

@pocke pocke Feb 4, 2017

Choose a reason for hiding this comment

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

Why are you confusing? I think it is good to keep constant assignment.

Note: The begin keyword should be surrounded by backtick. :) I'll fix it.

shared_examples :offense do |name, code, correction|
it "registers an offense for #{name} with a blank" do
inspect_source(cop, code)
expect(cop.offenses.size).to eq(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see a check for the message generated by the cop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've made to check it 👍

end

include_examples :offense, 'begin body starting', <<-CODE, <<-CORRECTION
begin
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same remark as from your other PR - use strip_margin so we can pretty up the heredocs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably add some cop for newer Rubies and a rule in the style guide to use the new heredocs syntax - it's so much nicer...

@bbatsov bbatsov merged commit b898d40 into rubocop:master Feb 4, 2017
@pocke pocke deleted the empty_lines_around_begin_block branch February 5, 2017 05:07
pocke added a commit to pocke/rubocop that referenced this pull request Feb 5, 2017
```ruby
 # bad
begin
  foo

rescue

  bar
end

 # good
begin
  foo
rescue
  bar
end
```

See also rubocop#3984

Fix
bbatsov pushed a commit that referenced this pull request Feb 5, 2017
```ruby
 # bad
begin
  foo

rescue

  bar
end

 # good
begin
  foo
rescue
  bar
end
```

See also #3984

Fix
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

2 participants