Skip to content

Commit

Permalink
WIP: handle nested blocks and non-top level blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Nov 25, 2024
1 parent e18a682 commit 30f6f77
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/rubocop/cop/discourse/services/empty_lines_around_blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ class EmptyLinesAroundBlocks < Base
)
MATCHER

def_node_matcher :top_level_block?, <<~MATCHER
(block (send nil? _) ...)
MATCHER

def on_class(node)
return unless service_include?(node)
@service = true
end

def on_block(node)
return unless service?
return unless top_level_block?(node)
return if node.single_line?

if missing_empty_lines?(node)
add_offense(node, message: MSG) do |corrector|
if missing_empty_line_before?(node)
corrector.insert_before(node.loc.expression, "\n")
corrector.insert_before(
node.loc.expression.adjust(
begin_pos: -node.loc.expression.column
),
"\n"
)
end
if missing_empty_line_after?(node)
corrector.insert_after(node.loc.end, "\n")
Expand All @@ -48,11 +60,13 @@ def missing_empty_lines?(node)
end

def missing_empty_line_before?(node)
processed_source[node.loc.expression.line - 2].present?
processed_source[node.loc.expression.line - 2].present? &&
node.left_siblings.present?
end

def missing_empty_line_after?(node)
processed_source[node.loc.end.line].present?
processed_source[node.loc.end.line].present? &&
node.right_siblings.present?
end
end
end
Expand Down

0 comments on commit 30f6f77

Please sign in to comment.