rubocop / rubocop-rspec

Code style checking for RSpec files.
https://docs.rubocop.org/rubocop-rspec
MIT License
805 stars 277 forks source link

Wrong autocorrect for `RSpec/ScatteredSetup` when block contains heredoc #1913

Closed Earlopain closed 3 months ago

Earlopain commented 3 months ago

Here's a testcase and roughtly what I would expect:

it 'flags hooks that contain heredoc arguments and autocorrects correctly' do
  expect_offense(<<~RUBY)
    describe Foo do
      before { foo }
      ^^^^^^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 3).
      before do
      ^^^^^^^^^ Do not define multiple `before` hooks in the same example group (also defined on line 2).
        bar(<<~'TEXT')
          Hello World!
        TEXT
      end
    end
  RUBY

  expect_correction(<<~RUBY)
    describe Foo do
      before {
        foo
        bar(<<~'TEXT')
          Hello World!
        TEXT
      }
    end
  RUBY
end

The actual autocorrect looks like this, which is not valid syntax (among other things):

describe Foo do
  before { foo
bar(<<~TEXT) }

end