Class: RuboCop::Cop::Discourse::Services::EmptyLinesAroundBlocks

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/discourse/services/empty_lines_around_blocks.rb

Overview

Put empty lines around multiline blocks.

Examples:

# bad
model :my_model
params do
  attribute :my_attribute
  validates :my_attribute, presence: true
end
policy :my_policy
step :another_step

# good
model :my_model

params do
  attribute :my_attribute
  validates :my_attribute, presence: true
end

policy :my_policy
step :another_step

Constant Summary collapse

MSG =
"Add empty lines around a step block."

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/discourse/services/empty_lines_around_blocks.rb', line 53

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

  if missing_empty_lines?(node)
    add_offense(node, message: MSG) do |corrector|
      if missing_empty_line_before?(node) && corrected_after.exclude?(node.left_sibling)
        corrected_before << node
        corrector.insert_before(
          node.loc.expression.adjust(begin_pos: -node.loc.expression.column),
          "\n",
        )
      end
      if missing_empty_line_after?(node) && corrected_before.exclude?(node.right_sibling)
        corrected_after << node
        corrector.insert_after(node.loc.end, "\n")
      end
    end
  end
end

#on_class(node) ⇒ Object



48
49
50
51
# File 'lib/rubocop/cop/discourse/services/empty_lines_around_blocks.rb', line 48

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