Class: RuboCop::Cop::RSpec::EmptyLineAfterHook

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, EmptyLineSeparation
Defined in:
lib/rubocop/cop/rspec/empty_line_after_hook.rb

Overview

Checks if there is an empty line after hook blocks.

‘AllowConsecutiveOneLiners` configures whether adjacent one-line definitions are considered an offense.

Examples:

# bad
before { do_something }
it { does_something }

# bad
after { do_something }
it { does_something }

# bad
around { |test| test.run }
it { does_something }

# good
after { do_something }

it { does_something }

# fair - it's ok to have non-separated one-liners hooks
around { |test| test.run }
after { do_something }

it { does_something }

with AllowConsecutiveOneLiners configuration

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

# bad
around { |test| test.run }
after { do_something }

it { does_something }

# good
around { |test| test.run }

after { do_something }

it { does_something }

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from EmptyLineSeparation

#last_child?, #missing_separating_line, #missing_separating_line_offense, #offending_loc

Methods included from FinalEndLocation

#final_end_location

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/rspec/empty_line_after_hook.rb', line 60

def on_block(node)
  return unless hook?(node)
  return if cop_config['AllowConsecutiveOneLiners'] &&
    chained_single_line_hooks?(node)

  missing_separating_line_offense(node) do |method|
    format(MSG, hook: method)
  end
end