Class: RuboCop::Cop::Layout::EmptyLineAfterMultilineCondition

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb

Overview

Enforces empty line after multiline condition.

Examples:

# bad
if multiline &&
  condition
  do_something
end

# good
if multiline &&
  condition

  do_something
end

# bad
case x
when foo,
  bar
  do_something
end

# good
case x
when foo,
  bar

  do_something
end

# bad
begin
  do_something
rescue FooError,
  BarError
  handle_error
end

# good
begin
  do_something
rescue FooError,
  BarError

  handle_error
end

Constant Summary collapse

MSG =
'Use empty line after multiline condition.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_case(node) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb', line 82

def on_case(node)
  node.when_branches.each do |when_node|
    last_condition = when_node.conditions.last

    next if !multiline_when_condition?(when_node) ||
            next_line_empty?(last_condition.last_line)

    add_offense(when_node, &autocorrect(last_condition))
  end
end

#on_if(node) ⇒ Object



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

def on_if(node)
  return if node.ternary?

  if node.modifier_form?
    check_condition(node.condition) if node.right_sibling
  else
    check_condition(node.condition)
  end
end

#on_rescue(node) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb', line 93

def on_rescue(node)
  node.resbody_branches.each do |resbody|
    rescued_exceptions = resbody.exceptions
    next if !multiline_rescue_exceptions?(rescued_exceptions) ||
            next_line_empty?(rescued_exceptions.last.last_line)

    add_offense(resbody, &autocorrect(rescued_exceptions.last))
  end
end

#on_while(node) ⇒ Object Also known as: on_until



70
71
72
# File 'lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb', line 70

def on_while(node)
  check_condition(node.condition)
end

#on_while_post(node) ⇒ Object Also known as: on_until_post



75
76
77
78
79
# File 'lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb', line 75

def on_while_post(node)
  return unless node.right_sibling

  check_condition(node.condition)
end