KEMBAR78
`Layout/EmptyLinesAroundClassBody` unaware of `class < Struct.new(<multiline>)` pattern · Issue #14411 · rubocop/rubocop · GitHub
Skip to content

Layout/EmptyLinesAroundClassBody unaware of class < Struct.new(<multiline>) pattern #14411

@emmahyde

Description

@emmahyde

Expected behavior

Expected the line after the closing parenthesis of Struct.new(...) to be checked for Layout/EmptyLinesAroundClassBody, in the event that the class inherits from Struct.new and is declared in a multiline format.

Actual behavior

Keeps revolving between

Layout/EmptyLinesAroundClassBody: Empty line missing at class body beginning,
Layout/EmptyLinesAroundArguments: Empty line detected around arguments.

in an infinite loop.

screenshots of the two formats:

Image Image

Steps to reproduce the problem

This seems to get applied once I extend the class to have a def initialize method... it must at that point get registered as a "block" class instead of a class that is defined "inline":

class Event < Struct.new(
  :message, # it checks this line for the space
  :severity,
  keyword_init: true,
)
  # this is the space I expected it to look at for Layout/EmptyLinesAroundClassBody
  def initialize(severity: :warning, **args)
    super(severity: severity, **args)
  end
end

I tried to create a regex pattern that would work with AllowedPatterns, but the issue is that RuboCop compares the regex 1 line in the source code at a time - so I'm unable to create a multiline regex pattern that captures \sclass.<\sStruct.new( and then disables this for the next line (or do ^sclass.<\sStruct.new(\n.*:[a-z_]+,).

RuboCop version

❯ rubocop -v
1.79.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions