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 Layout/EmptyLineAroundAttributeAccessor cop #7922

Conversation

koic
Copy link
Member

@koic koic commented Apr 30, 2020

This PR adds new Layout/EmptyLineAroundAttributeAccessor cop that checks for a newline after attribute accessor.

# bad
attr_accessor :foo
def do_something
end

# good
attr_accessor :foo

def do_something
end

# good
attr_accessor :foo
attr_reader :bar
attr_writer :baz
attr :qux

def do_something
end

This PR first checks an empty line after an attribute accessor to implement the example shown in The Ruby Style Guide's rule.
rubocop/ruby-style-guide#817

In the future this cop will provide an EnforcedStyle: around that checks empty lines before and after an attribute accessor, and this EnforcedStyle: only_after option could be users selectable option.


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).
  • 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.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@koic koic force-pushed the add_new_layout_empty_line_after_attribute_accessor branch 3 times, most recently from 44113b6 to 11e3734 Compare May 1, 2020 13:52
Copy link
Collaborator

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

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

Looks good, but you should also add some coverage of attr - the ancient macro that probably nobody uses, but still exists. :-)

This PR adds new `Layout/EmptyLineAroundAttributeAccessor` cop
that checks for a newline after attribute accessor.

```ruby
# bad
attr_accessor :foo
def do_something
end

# good
attr_accessor :foo

def do_something
end

# good
attr_accessor :foo
attr_reader :bar
attr_writer :baz
attr :qux

def do_something
end
```

This PR first checks an empty line after an attribute accessor to
implement the example shown in The Ruby Style Guide's rule.
rubocop/ruby-style-guide#817

In the future this cop will provide an `EnforcedStyle: around` that
checks empty lines before and after an attribute accessor, and this
`EnforcedStyle: only_after` option could be users selectable option.
@koic koic force-pushed the add_new_layout_empty_line_after_attribute_accessor branch from 11e3734 to 8831824 Compare May 10, 2020 13:25
@koic
Copy link
Member Author

koic commented May 10, 2020

A legacy method attr was not included in the documentation. However, this cop's actual behavior also detects attr, so I updated the documentation to mention attr as well.

@bbatsov bbatsov merged commit 865930c into rubocop:master May 10, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented May 10, 2020

I guess we're in business then! :-) Thanks!

@koic koic deleted the add_new_layout_empty_line_after_attribute_accessor branch May 10, 2020 16:38
koic added a commit to rubocop/rubocop-rails that referenced this pull request May 11, 2020
Follow up to rubocop/rubocop#7922.

This commit suppresses the following offense.

```console
% bundle exec rake
(snip)

Offenses:

lib/rubocop/cop/rails/http_status.rb:85:11: C:
Layout/EmptyLinesAroundAttributeAccessor: Add an empty line after
attribute accessor.
          attr_reader :node
          ^^^^^^^^^^^^^^^^^
lib/rubocop/cop/rails/http_status.rb:126:11: C:
Layout/EmptyLinesAroundAttributeAccessor: Add an empty line after
attribute accessor.
          attr_reader :node
          ^^^^^^^^^^^^^^^^^

145 files inspected, 2 offenses detected
```
@aried3r
Copy link

aried3r commented May 13, 2020

Hey! I just updated to rubocop 0.83 and tried this new cop. I get the intention behind it when reading the examples, but how does the style guide suggest handling situations like these:

attr_accessor :validate_vat
alias validate_vat? validate_vat

# potentially more code

validates :vat_number, presence: true, if: :validate_vat?

# more code 

def do_something
  do_the_thing if validate_vat?
end

Personally I like to keep aliasing to add ? close to attr_accessor instead of separating it.

I know I can just disable this cop, but I was wondering if that's a common pattern the cop should be able to handle or if that's very specific to our code base at work.

@marcandre
Copy link
Contributor

I agree with @aried3r ; ideally the cop should be able to ignore lines like the alias, as well as any send call with an argument matching any of the argument of attr_accessor (e.g. alias_method :anything, :validate_vat, protected :validate_vat, etc.)

@koic
Copy link
Member Author

koic commented May 13, 2020

I see. Perhaps customizable allow list such as IgnoredMethods option can be created. I think it can have some allowed method names (e.g. alias) by default.

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

4 participants