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 10, 2020
1 parent 8ffc33d commit e872b3c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 33 deletions.
9 changes: 2 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_example.rb
Expand Up @@ -49,15 +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
end
msg = format(MSG, example: node.method_name)
check_missing_separating_lines(node, message: msg)
end

def allowed_one_liner?(node)
Expand Down
9 changes: 2 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_example_group.rb
Expand Up @@ -31,14 +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
end
msg = format(MSG, example_group: node.method_name)
check_missing_separating_lines(node, message: msg)
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/rubocop/cop/rspec/empty_line_after_final_let.rb
Expand Up @@ -28,13 +28,8 @@ 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|
add_offense(location) do |corrector|
corrector.insert_after(location.end, "\n")
end
end
check_missing_separating_lines(latest_let, message: MSG)
end
end
end
Expand Down
9 changes: 2 additions & 7 deletions lib/rubocop/cop/rspec/empty_line_after_hook.rb
Expand Up @@ -41,14 +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
end
msg = format(MSG, hook: node.method_name)
check_missing_separating_lines(node, message: msg)
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/rubocop/cop/rspec/empty_line_after_subject.rb
Expand Up @@ -22,13 +22,8 @@ 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|
add_offense(location) do |corrector|
corrector.insert_after(location.end, "\n")
end
end
check_missing_separating_lines(node, message: MSG)
end

private
Expand Down
10 changes: 10 additions & 0 deletions lib/rubocop/rspec/blank_line_separation.rb
Expand Up @@ -8,6 +8,16 @@ module BlankLineSeparation
include FinalEndLocation
include RuboCop::Cop::RangeHelp

def check_missing_separating_lines(node, message:)
return if last_child?(node)

missing_separating_line(node) do |location|
add_offense(location, message: message) 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 e872b3c

Please sign in to comment.