rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Option so one-line/hooks/examples/whatever shouldn’t need to have a newline between them.

Open ngouy opened this issue 3 years ago • 3 comments

context

This was already discussed here: https://github.com/rubocop/rubocop-rspec/pull/632#issuecomment-415967045

The issue

what we have today:

If I have

context "given 1, 2, 3" do
  before { do_something } # offense missing new line after hook
  around { do_something_else } # offense missing new line after hook
  after { do_yet_anothing_thing }

  it { should do something }
end

And naturally, would be auto-corrected to

context "given 1, 2, 3" do
  before { do_something }

  around { do_something_else }

  after { do_yet_anothing_thing }

  it { should do something }
end

Proposition:

An option to the EmptyLineAfterHook so we can have multiple one-liner grouped together withouht new lines This option could probably have other usage on other cops

WIth this new option, the first code block wouldn't trigger any offense

Unknowns

Some things I don't know how they should behave. For example:


context "given before no-one-line blocks" do
  before do
    something
  end
  after do
    something_else
  end
end

context "given a mix of before blocks" do
  before { do_something } 
  after do
    something
  end
  around { do_yet_another_thing }
end

ngouy avatar Jul 04 '22 09:07 ngouy

You usually wouldn’t list the same hook more than once, right? In that case, you’d just expand to a multi-line block. So

before do
  something
  something_else
end
# instead of
before { something }
before { something_else }

The issue we want to fix is when you have different one-line hooks after each other:

before { something }
after { the_last_thing }
# instead of
before { something }

after { the_last_thing }

bquorning avatar Jul 04 '22 11:07 bquorning

I'm guilty of separating before hooks when they do unrelated things, e.g.:

before { allow(user).to receive(:delete).and_return(true) }

before { user.tags << 'long-inactive' }

but I prefer to keep a line between them.

pirj avatar Jul 04 '22 14:07 pirj

regardless if we use multiple before (or any other hook) or not, I think it's fair to allow consecutive one-liner for both same and/or different hooks, in order to have a single one-liner hooks block

both

before { do_smt }
before { do_smt_else }

it "..."

# or 

before { do_smt }
after { un_do_smt }

it "..."

Seems fair

ngouy avatar Jul 05 '22 13:07 ngouy

RSpec/EmptyLineAfterHook has a AllowConsecutiveOneLiners option, see example:

        RSpec.describe User do
          before { do_something }
          before { do_something_else }

          it { does_something }
        end

Do you also suggest to allow no empty line between hooks, lets and examples when they all are one-liners like below, @ngouy ?

RSpec.describe 'foo' do
  subject(:foo) { Foo.new }
  let(:bar) { Bar.new }
  before { do_smt }
  after { do_smt_else }
  it { is_expected.to ... }
end

pirj avatar Oct 15 '22 14:10 pirj

yea @pirj I'm the one that introduced it to solve it 😂 Just forgot to close this issue

AllowConsecutiveOneLiners is the way

ngouy avatar Oct 17 '22 12:10 ngouy

Do you also suggest to allow no empty line between hooks, lets and examples when they all are one-liners like below, @ngouy ?

Not really

I don't mind if some does, but that was not my intention And personally I wouldn't do that

ngouy avatar Oct 17 '22 12:10 ngouy