Skip to content

Commit

Permalink
Extract some shared logic to BlankLineSeparation
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning committed Jul 14, 2020
1 parent ee6da00 commit 51d593a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
8 changes: 2 additions & 6 deletions lib/rubocop/cop/rspec/empty_line_after_example.rb
Expand Up @@ -49,14 +49,10 @@ class EmptyLineAfterExample < Cop

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
8 changes: 2 additions & 6 deletions lib/rubocop/cop/rspec/empty_line_after_example_group.rb
Expand Up @@ -31,13 +31,9 @@ class EmptyLineAfterExampleGroup < Cop

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
8 changes: 2 additions & 6 deletions lib/rubocop/cop/rspec/empty_line_after_final_let.rb
Expand Up @@ -28,13 +28,9 @@ def on_block(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|
msg = format(MSG, let: latest_let.method_name)
add_offense(location, message: msg) 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
8 changes: 2 additions & 6 deletions lib/rubocop/cop/rspec/empty_line_after_hook.rb
Expand Up @@ -41,13 +41,9 @@ class EmptyLineAfterHook < Cop

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
8 changes: 2 additions & 6 deletions lib/rubocop/cop/rspec/empty_line_after_subject.rb
Expand Up @@ -22,13 +22,9 @@ class EmptyLineAfterSubject < Cop

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

missing_separating_line(node) do |location|
msg = format(MSG, subject: 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, subject: method)
end
end

Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/rspec/empty_line_separation.rb
Expand Up @@ -8,6 +8,17 @@ 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

0 comments on commit 51d593a

Please sign in to comment.