Skip to content

Commit

Permalink
Merge pull request #961 from rubocop-hq/extract-to-blank-line-separation
Browse files Browse the repository at this point in the history
Extract some shared logic to BlankLineSeparation
  • Loading branch information
bquorning committed Jul 14, 2020
2 parents b948759 + 51d593a commit 1be4c09
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop-rspec.rb
Expand Up @@ -23,7 +23,7 @@
require_relative 'rubocop/rspec/align_let_brace'
require_relative 'rubocop/rspec/factory_bot'
require_relative 'rubocop/rspec/final_end_location'
require_relative 'rubocop/rspec/blank_line_separation'
require_relative 'rubocop/rspec/empty_line_separation'
require_relative 'rubocop/rspec/corrector/move_node'

RuboCop::RSpec::Inject.defaults!
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_example.rb
Expand Up @@ -43,20 +43,16 @@ module RSpec
#
class EmptyLineAfterExample < Cop
extend AutoCorrector
include RuboCop::RSpec::BlankLineSeparation
include RuboCop::RSpec::EmptyLineSeparation

MSG = 'Add an empty line after `%<example>s`.'

def on_block(node)
return unless example?(node)
return if last_child?(node)
return if allowed_one_liner?(node)

missing_separating_line(node) do |location|
msg = format(MSG, example: node.method_name)
add_offense(location, message: msg) do |corrector|
corrector.insert_after(location.end, "\n")
end
missing_separating_line_offense(node) do |method|
format(MSG, example: method)
end
end

Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_example_group.rb
Expand Up @@ -25,19 +25,15 @@ module RSpec
#
class EmptyLineAfterExampleGroup < Cop
extend AutoCorrector
include RuboCop::RSpec::BlankLineSeparation
include RuboCop::RSpec::EmptyLineSeparation

MSG = 'Add an empty line after `%<example_group>s`.'

def on_block(node)
return unless example_group?(node)
return if last_child?(node)

missing_separating_line(node) do |location|
msg = format(MSG, example_group: node.method_name)
add_offense(location, message: msg) do |corrector|
corrector.insert_after(location.end, "\n")
end
missing_separating_line_offense(node) do |method|
format(MSG, example_group: method)
end
end
end
Expand Down
11 changes: 4 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_final_let.rb
Expand Up @@ -18,22 +18,19 @@ module RSpec
# it { does_something }
class EmptyLineAfterFinalLet < Cop
extend AutoCorrector
include RuboCop::RSpec::BlankLineSeparation
include RuboCop::RSpec::EmptyLineSeparation

MSG = 'Add an empty line after the last `let` block.'
MSG = 'Add an empty line after the last `%<let>s`.'

def on_block(node)
return unless example_group_with_body?(node)

latest_let = node.body.child_nodes.select { |child| let?(child) }.last

return if latest_let.nil?
return if last_child?(latest_let)

missing_separating_line(latest_let) do |location|
add_offense(location) do |corrector|
corrector.insert_after(location.end, "\n")
end
missing_separating_line_offense(latest_let) do |method|
format(MSG, let: method)
end
end
end
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_hook.rb
Expand Up @@ -35,19 +35,15 @@ module RSpec
#
class EmptyLineAfterHook < Cop
extend AutoCorrector
include RuboCop::RSpec::BlankLineSeparation
include RuboCop::RSpec::EmptyLineSeparation

MSG = 'Add an empty line after `%<hook>s`.'

def on_block(node)
return unless hook?(node)
return if last_child?(node)

missing_separating_line(node) do |location|
msg = format(MSG, hook: node.method_name)
add_offense(location, message: msg) do |corrector|
corrector.insert_after(location.end, "\n")
end
missing_separating_line_offense(node) do |method|
format(MSG, hook: method)
end
end
end
Expand Down
11 changes: 4 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_subject.rb
Expand Up @@ -16,18 +16,15 @@ module RSpec
# let(:foo) { bar }
class EmptyLineAfterSubject < Cop
extend AutoCorrector
include RuboCop::RSpec::BlankLineSeparation
include RuboCop::RSpec::EmptyLineSeparation

MSG = 'Add empty line after `subject`.'
MSG = 'Add an empty line after `%<subject>s`.'

def on_block(node)
return unless subject?(node) && !in_spec_block?(node)
return if last_child?(node)

missing_separating_line(node) do |location|
add_offense(location) do |corrector|
corrector.insert_after(location.end, "\n")
end
missing_separating_line_offense(node) do |method|
format(MSG, subject: method)
end
end

Expand Down
Expand Up @@ -2,12 +2,23 @@

module RuboCop
module RSpec
# Helps determine the offending location if there is not a blank line
# Helps determine the offending location if there is not an empty line
# following the node. Allows comments to follow directly after.
module BlankLineSeparation
module EmptyLineSeparation
include FinalEndLocation
include RuboCop::Cop::RangeHelp

def missing_separating_line_offense(node)
return if last_child?(node)

missing_separating_line(node) do |location|
msg = yield(node.method_name)
add_offense(location, message: msg) do |corrector|
corrector.insert_after(location.end, "\n")
end
end
end

def missing_separating_line(node)
line = final_end_location(node).line

Expand Down
14 changes: 7 additions & 7 deletions spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb
Expand Up @@ -8,7 +8,7 @@
RSpec.describe User do
let(:a) { a }
let(:b) { b }
^^^^^^^^^^^^^ Add an empty line after the last `let` block.
^^^^^^^^^^^^^ Add an empty line after the last `let`.
it { expect(a).to eq(b) }
end
RUBY
Expand All @@ -30,7 +30,7 @@
let!(:b) do
b
end
^^^ Add an empty line after the last `let` block.
^^^ Add an empty line after the last `let!`.
it { expect(a).to eq(b) }
end
RUBY
Expand All @@ -52,7 +52,7 @@
RSpec.describe User do
let(:a) { a }
let(:user, &args[:build_user])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`.
it { expect(a).to eq(b) }
end
RUBY
Expand Down Expand Up @@ -96,7 +96,7 @@
let(:a) { a }
let(:b) { b }
# end of setup
^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
^^^^^^^^^^^^^^ Add an empty line after the last `let`.
it { expect(a).to eq(b) }
end
RUBY
Expand All @@ -119,7 +119,7 @@
let(:b) { b }
# a multiline comment marking
# the end of setup
^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
^^^^^^^^^^^^^^^^^^ Add an empty line after the last `let`.
it { expect(a).to eq(b) }
end
RUBY
Expand All @@ -144,7 +144,7 @@
subject { described_class }
let!(:b) { b }
^^^^^^^^^^^^^^ Add an empty line after the last `let` block.
^^^^^^^^^^^^^^ Add an empty line after the last `let!`.
it { expect(a).to eq(b) }
end
RUBY
Expand Down Expand Up @@ -236,7 +236,7 @@
hello
world
BAR
^^^ Add an empty line after the last `let` block.
^^^ Add an empty line after the last `let`.
it 'has tricky syntax' do
expect(foo).to eql(" hello\n world\n")
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb
Expand Up @@ -7,7 +7,7 @@
expect_offense(<<-RUBY)
RSpec.describe User do
subject { described_class.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject`.
let(:params) { foo }
end
RUBY
Expand All @@ -25,7 +25,7 @@
expect_offense(<<-RUBY)
RSpec.describe User do
subject! { described_class.new }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line after `subject`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add an empty line after `subject!`.
let(:params) { foo }
end
RUBY
Expand Down

0 comments on commit 1be4c09

Please sign in to comment.