rubocop-rspec
rubocop-rspec copied to clipboard
Option so one-line/hooks/examples/whatever shouldn’t need to have a newline between them.
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
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 }
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.
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
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
yea @pirj I'm the one that introduced it to solve it 😂 Just forgot to close this issue
AllowConsecutiveOneLiners is the way
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