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

Naming/AccessorMethodName not flagging all functions in some ruby files, even when explicitly included #7801

Closed
rnickmccandless opened this issue Mar 17, 2020 · 3 comments

Comments

@rnickmccandless
Copy link

The cop "Naming/AccessorMethodName" is not flagging all functions that start with get_ and set_ in some ruby files, even when explicitly including the files in the rubocop config.

We want to discourage our coders from using any function starting with get_ and set_ by including all files within the ruby app.

Naming/AccessorMethodName:
  Include:
    - 'app/**/*.rb'

Expected behavior

Rubocop flagging all functions that start with starting with get_ and set_ in all ruby files that are added to the Include in the "Naming/AccessorMethodName" rubocop config.

Actual behavior

Rubocop flags functions that start with get_ and set_ only in certain ruby files such as controllers and modes while ignoring files added to the Include.

Steps to reproduce the problem

Add the following rule to the .rubocop.yml

Naming/AccessorMethodName:
  Include:
    - 'app/**/*.rb'

Create a function starting with get_something_from(id) in app/services/external_source.rb

Run rubocop

bundle exec rubocop

RuboCop version

$ [bundle exec] rubocop -V
0.80.1 (using Parser 2.7.0.4, running on ruby 2.6.2 x86_64-darwin19)
@Kache
Copy link

Kache commented Jun 8, 2020

I ran into something like this. After testing, I believe the cop does not treat singleton accessor methods (module and class methods) as offenses, since singleton methods are (in principle) free functions and not bound to an object instance.

For example:

module Foo
  def get_this_gets_caught
    'foo'
  end

  def self.get_this_does_not
    'foo'
  end
end

@stale
Copy link

stale bot commented Dec 6, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Dec 6, 2020
@dvandersluis
Copy link
Member

This cop is working as expected. Both instance and class methods are considered, and it is not limited to certain paths.

test.rb:2:7: C: Naming/AccessorMethodName: Do not prefix reader method names with get_.
  def get_this_gets_caught
      ^^^^^^^^^^^^^^^^^^^^
test.rb:6:12: C: Naming/AccessorMethodName: Do not prefix reader method names with get_.
  def self.get_this_does_not
           ^^^^^^^^^^^^^^^^^

However, the cop only checks for methods with the correct arity: getters must have 0 arguments and setters must have 1. I am opening a PR to update the documentation in that regard.

dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Dec 8, 2020
@stale stale bot removed the stale Issues that haven't been active in a while label Dec 8, 2020
@bbatsov bbatsov closed this as completed in 0146f4f Dec 8, 2020
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

No branches or pull requests

3 participants