Skip to content

Cop idea: Style/AllowModifiersOnAttrs #12242

@ntkme

Description

@ntkme

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions