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

This cop 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

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from AutocorrectLogic

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

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

add_parentheses, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, indent, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

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.each_when 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