Class: RuboCop::Cop::RSpec::EmptyLineAfterFinalLet

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/empty_line_after_final_let.rb

Overview

Checks if there is an empty line after the last let block.

Examples:

# bad
let(:foo) { bar }
let(:something) { other }
it { does_something }

# good
let(:foo) { bar }
let(:something) { other }

it { does_something }

Constant Summary collapse

MSG =
'Add an empty line after the last `let` block.'.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



37
38
39
40
# File 'lib/rubocop/cop/rspec/empty_line_after_final_let.rb', line 37

def autocorrect(node)
  loc = last_node_loc(node)
  ->(corrector) { corrector.insert_after(loc.end, "\n") }
end

#on_block(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/rspec/empty_line_after_final_let.rb', line 24

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 latest_let.equal?(node.body.children.last)

  no_new_line_after(latest_let) do
    add_offense(latest_let, location: :expression)
  end
end