Class: RuboCop::Cop::RSpec::EmptyLineAfterExample

Inherits:
Cop
  • Object
show all
Includes:
RSpec::BlankLineSeparation
Defined in:
lib/rubocop/cop/rspec/empty_line_after_example.rb

Overview

Checks if there is an empty line after example blocks.

Examples:

# bad
RSpec.describe Foo do
  it 'does this' do
  end
  it 'does that' do
  end
end

# good
RSpec.describe Foo do
  it 'does this' do
  end

  it 'does that' do
  end
end

# fair - it's ok to have non-separated one-liners
RSpec.describe Foo do
  it { one }
  it { two }
end

with AllowConsecutiveOneLiners configuration


# rubocop.yml
# RSpec/EmptyLineAfterExample:
#   AllowConsecutiveOneLiners: false

# bad
RSpec.describe Foo do
  it { one }
  it { two }
end

Constant Summary collapse

MSG =
'Add an empty line after `%<example>s`.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods included from RSpec::BlankLineSeparation

#autocorrect, #last_child?, #missing_separating_line, #offending_loc

Methods included from RSpec::FinalEndLocation

#final_end_location

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#allow_consecutive_one_liners?Boolean



65
66
67
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 65

def allow_consecutive_one_liners?
  cop_config['AllowConsecutiveOneLiners']
end

#allowed_one_liner?(node) ⇒ Boolean



61
62
63
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 61

def allowed_one_liner?(node)
  consecutive_one_liner?(node) && allow_consecutive_one_liners?
end

#consecutive_one_liner?(node) ⇒ Boolean



69
70
71
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 69

def consecutive_one_liner?(node)
  node.line_count == 1 && next_one_line_example?(node)
end

#next_one_line_example?(node) ⇒ Boolean



73
74
75
76
77
78
79
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 73

def next_one_line_example?(node)
  next_sibling = next_sibling(node)
  return unless next_sibling
  return unless example?(next_sibling)

  next_sibling.line_count == 1
end

#next_sibling(node) ⇒ Object



81
82
83
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 81

def next_sibling(node)
  node.parent.children[node.sibling_index + 1]
end

#on_block(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/rspec/empty_line_after_example.rb', line 49

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|
    add_offense(node,
                location: location,
                message: format(MSG, example: node.method_name))
  end
end