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
Add new Layout/EmptyLineAroundAttributeAccessor
cop
#7922
Conversation
44113b6
to
11e3734
Compare
There was a problem hiding this 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.
11e3734
to
8831824
Compare
A legacy method |
I guess we're in business then! :-) Thanks! |
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 ```
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 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. |
I agree with @aried3r ; ideally the cop should be able to ignore lines like the |
I see. Perhaps customizable allow list such as |
This PR adds new
Layout/EmptyLineAroundAttributeAccessor
cop that checks for a newline after attribute accessor.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 thisEnforcedStyle: only_after
option could be users selectable option.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and RuboCop for itself, and generates the documentation.