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 idea: Style/AllowModifiersOnAttrs #12242

Open
ntkme opened this issue Oct 3, 2023 · 2 comments · May be fixed by #12385
Open

Cop idea: Style/AllowModifiersOnAttrs #12242

ntkme opened this issue Oct 3, 2023 · 2 comments · May be fixed by #12385

Comments

@ntkme
Copy link

ntkme commented Oct 3, 2023

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

In Ruby >=3.0, we can write the modifier and attr_* helpers inline like:

class Foo
  public attr_accessor :a
  protected attr_reader :b
  protected attr_writer :c
  private attr :d, true
end

Today, this is an offend on Style/AccessModifierDeclarations. However, just for attr* helpers this style looks much cleaner than the default group style.

Describe the solution you'd like

After all the default group style of Style/AccessModifierDeclarations works well for normal def methods, that I don't want to change that to inline style. Therefore I think the best approach is to add a new option called Style/AllowModifiersOnAttrs similar to Style/AllowModifiersOnSymbols to allow inlined modifier on attr* methods.

Describe alternatives you've considered

Use the group style everywhere - the problem is that I think most of the developers would prefer to put the attr declarations together, and then normal methods together, in that case we need to toggle public and private back and forth like below:

class Foo
  attr_accessor :a

  private

  attr_accessor :b

  public # <-- extra "public" to toggle group back and forth

  def c; end

  private

  def d; end
  def e; end
end

Use the inline style everywhere, the problem would be simply we don't want to add the modifier to each normal method.

class Foo
  attr_accessor :a
  private attr_accessor :b

  def c; end
  private def d; end 
  private def e; end
  private def f; end
end

With a Style/AllowModifiersOnAttrs rule similar to Style/AllowModifiersOnSymbols, we would be able to write:

class Foo
  attr_accessor :a
  private attr_accessor :b

  def c; end

  private

  def d; end
  def e; end
  def f; end
end

Additional context

@ntkme ntkme changed the title Cop idea: Style/AllowModifiersOnAttrs Cop idea: Style/AllowModifiersOnAttrs Oct 3, 2023
krororo added a commit to krororo/rubocop that referenced this issue Nov 12, 2023
krororo added a commit to krororo/rubocop that referenced this issue Dec 16, 2023
krororo added a commit to krororo/rubocop that referenced this issue Apr 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants
@ntkme and others