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

Cop request: private delegates #368

Open
Drenmi opened this issue Oct 9, 2020 · 4 comments
Open

Cop request: private delegates #368

Drenmi opened this issue Oct 9, 2020 · 4 comments
Assignees
Labels
feature request Request for new functionality

Comments

@Drenmi
Copy link
Contributor

Drenmi commented Oct 9, 2020

Is your feature request related to a problem? Please describe.

When using #delegate, ActiveSupport creates a new method on the class, but this method does not respect the visibility scope of said class.

For example:

class Foo
  def initialize(bar)
    @bar = bar
  end

  attr_reader :bar

  private

  delegate :baz, to: :bar
end

The generated method #baz will not be private. The #delegate method does, however, provide an option to create a private method like so:

delegate :baz, to: :bar, private: true

Describe the solution you'd like

I would like a cop that detects calls to #delegate that:

  1. Are in a private scope.
  2. Does not pass the private: true option.
@Drenmi Drenmi added the feature request Request for new functionality label Oct 9, 2020
@andyw8
Copy link
Contributor

andyw8 commented Oct 10, 2020

Sidenote: The private option was added in Rails 6:
https://blog.bigbinary.com/2019/06/10/rails-6-adds-private-option-to-delegate-method.html

@ABaldwinHunter
Copy link

I've had the same desire and would like to build this feature!

is it okay if I go ahead and start working on it following the contribution guidelines? or is there any other protocol to follow up on?

@andyw8
Copy link
Contributor

andyw8 commented May 6, 2022

@ABaldwinHunter go ahead! See https://github.com/rubocop/rubocop-rails/blob/master/CONTRIBUTING.md

@Darhazer
Copy link
Member

I started working on this. But what to do (in terms of auto-correction) when private option is passed, but there is mismatch in the scope? E.g.

class A
  private

  delegate :bar, to: :foo, private: false
class B
  public

  delegate :bar, to: :foo, private: true

Should we instead of changing the private option, move the node? Or register an offense with a different message (than in the missing private option) and do not attempt to auto-correct?

@Darhazer Darhazer self-assigned this Jul 14, 2022
povilasjurcys added a commit to povilasjurcys/rubocop-rails that referenced this issue Aug 2, 2023
povilasjurcys added a commit to povilasjurcys/rubocop-rails that referenced this issue Aug 2, 2023
povilasjurcys added a commit to povilasjurcys/rubocop-rails that referenced this issue Aug 2, 2023
povilasjurcys added a commit to povilasjurcys/rubocop-rails that referenced this issue Aug 2, 2023
povilasjurcys added a commit to povilasjurcys/rubocop-rails that referenced this issue Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for new functionality
Projects
None yet
Development

No branches or pull requests

4 participants