Rockster160
Rockster160

Reputation: 1658

Rubocop: end alignment for begin blocks

I may be missing something obvious, but it seems that Rubocop skips the alignment of end when closing a begin block.

Given the case:

  begin
        # Foo
      end

I would expect the "end" to have a Layout error: Layout/EndAlignment end is not aligned with begin However, Rubocop seems to believe this is correct.

I would expect one of Layout/DefEndAlignment, Layout/EndAlignment, and Layout/BlockAlignment to manage this behavior, but no option in any of them seems to have any effect.

Any Rubocop experts know how to fix this configuration, or is this a potential bug?

Use case of using begin without a rescue is using memoization to set a calculated value

@ivar ||= begin
  # Do some stuff
  # Do more stuff
  # Then return the value
end

Upvotes: 1

Views: 642

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

Update: This is a known issue (https://github.com/rubocop-hq/rubocop/pull/7286)

Original answer:

Given the [input] ..

begin
      # Foo
    end

Testing with latest rubocop (0.77.0) ..

  • BlockAlignment is only concerned with do .. end blocks, per the docs.
  • BlockEndNewline only cares that end is on its own line, and does not seem to care about indentation
  • DefEndAlignment not applicable, I think, because it's not a method definition (a def)
  • EndAlignment seems the most relevant, but does not seem to support begin. It supports lots of other things (class, module, if, while, etc.) so perhaps this is an oversight.
  • IndentationConsistency seems to only be concerned with the contents of blocks, and seems to ignore the end
  • IndentationWidth is described as a "companion" to IndentationConsistency and accordingly also seems to only be concerned with the contents of blocks
  • RescueEnsureAlignment is relevant to blocks, but seems to be only concerned with the rescue and ensure keywords

In conclusion, this seems like an oversight in the implementation of EndAlignment in the latest rubocop (0.77.0). I'd suggest opening an issue.

Upvotes: 2

Related Questions