1

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
Rockster160
  • 1,579
  • 1
  • 15
  • 30
  • _I've tried changing the settings for Layout/DefEndAlignment, Layout/EndAlignment, Layout/BlockAlignment and a few others_ Post what you tried and the outcomes of trying them. Don't make everyone reproduce work that you've already tried and determined not to be the solution. – anothermh Dec 11 '19 at 22:34
  • @anothermh - Sorry I wasn't clear. I meant changing all/any of those settings has 0 effect on the layout of any sort. I'll update the post for clarity. – Rockster160 Dec 11 '19 at 22:36
  • You're still haven't said what you've tried. You're essentially saying "I know a bunch of things that don't work but I won't tell you what they are. I want you to spend time figuring out what I already know doesn't work." Think about your audience. If you've tried x and got outcome y but desire outcome z, explain what x, y and z are. Repeat for every iteration you've tried. – anothermh Dec 11 '19 at 22:42
  • 1
    I'm still not sure how exactly you expect the question to be phrased? Rubocop has thousands of different cops, and I didn't keep a running tab of every single one I tried. I listed the relevant ones because those are the ones that SHOULD matter, the rest of them are irrelevant. – Rockster160 Dec 12 '19 at 00:47
  • That's my point: there are **tons** of things that can be configured. You've already spent time trying things and none of them worked. _You should post in your [mre] the things you tried that did not work_. Instead, you've decided it's better to say "there are lots of options, I didn't keep track of my work, so please go replicate all the work I've already done until you find the answer for me." Imagine how much time you could save by sharing the work you've done! Imagine how polite it would be to make the smallest effort to help the people you're asking for help! – anothermh Dec 12 '19 at 02:18
  • `EndAlignment` has three options, `BlockAlignment` has three options, and `DefEndAlignment` has two options. You're basically asking us to try all the combinations of these options -- and any others that may be relevant -- even though you've already tested some number of these combinations and found they didn't work. Why would you not include that information in your post so that people don't have to replicate a bunch of steps that you already know won't work? If the answer truly is "I didn't keep track," then go do the work of keeping track and post it here. – anothermh Dec 12 '19 at 02:25
  • 1
    Not trying to argue- but clearly we're not understanding each other. The point of that line was, "I tried irrelevant things and they obviously didn't work." I ate a banana while trying it- should I include that in the steps as well? The other work I did was not relevant, therefore, not mentioned explicitly outside of "I'm frustrated and just trying rubbish at this point." I could have been more clear saying that I've tried each of the available options on the 3 relevant to no avail, you are correct. – Rockster160 Dec 13 '19 at 20:25

1 Answers1

2

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.

Jared Beck
  • 16,796
  • 9
  • 72
  • 97