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/AccessorGrouping cop #8241

Merged
merged 2 commits into from Jul 6, 2020

Conversation

fatkodima
Copy link
Contributor

Closes #3983

# EnforcedStyle: grouped (default)

# bad
class Foo
  attr_reader :bar
  attr_reader :baz
end

# good
class Foo
  attr_reader :bar, :baz
end


# EnforcedStyle: separated

# bad
class Foo
  attr_reader :bar, :baz
end

# good
class Foo
  attr_reader :bar
  attr_reader :baz
end

@fatkodima
Copy link
Contributor Author

Updated.

@bbatsov bbatsov merged commit c16faa7 into rubocop:master Jul 6, 2020
@ghiculescu
Copy link
Contributor

ghiculescu commented Oct 1, 2020

FYI, this cop is not compatible with sorbet. See this example.

This code:

class Original
  extend T::Sig

  sig { returns(String) }
  attr_reader :foo
  
  sig { returns(Integer) }
  attr_reader :bar
end

Is auto corrected to this:

class AutoCorrected
  extend T::Sig

  sig { returns(String) }
  attr_reader :foo, :bar

  sig { returns(Integer) }
end

@marcandre
Copy link
Contributor

@ghiculescu I'm not super familiar with Sorbet yet, I imagine the "separated" style is fine. Is it mandatory, actually? I'm too lazy to look it up myself: if two readers are strings, can you write sig { returns(String) }; attr_reader :foo, :bar?

@ghiculescu
Copy link
Contributor

Yes, that works. The problems are

  1. that doesn't work if they return different types
  2. we are autocorrecting to invalid syntax

To be fair, I'm not sure if it's rubocop's responsibility to deal with that. i'm just commenting so other sorbet users are aware of this.

@marcandre
Copy link
Contributor

I believe it warrants at least a note in the doc: #8828

I believe the cop already considers comments, but maybe our notion of "comment" should be expanded for Sorbet.

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.

Add a cop that checks grouping of accessor methods
4 participants