Class: RuboCop::Cop::Layout::EmptyLineBetweenDefs

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

Overview

This cop checks whether method definitions are separated by one empty line.

NumberOfEmptyLines can be an integer (default is 1) or an array (e.g. [1, 2]) to specify a minimum and maximum number of empty lines permitted.

AllowAdjacentOneLineDefs configures whether adjacent one-line method definitions are considered an offense.

Examples:


# bad
def a
end
def b
end

# good
def a
end

def b
end

Constant Summary collapse

MSG =
'Use empty lines between method definitions.'

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, 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

Class Method Details

.autocorrect_incompatible_withObject



38
39
40
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 38

def self.autocorrect_incompatible_with
  [Layout::EmptyLines]
end

Instance Method Details

#autocorrect(corrector, prev_def, node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 66

def autocorrect(corrector, prev_def, node)
  # finds position of first newline
  end_pos = prev_def.loc.end.end_pos
  source_buffer = prev_def.loc.end.source_buffer
  newline_pos = source_buffer.source.index("\n", end_pos)

  # Handle the case when multiple one-liners are on the same line.
  newline_pos = end_pos + 1 if newline_pos > node.source_range.begin_pos

  count = blank_lines_count_between(prev_def, node)

  if count > maximum_empty_lines
    autocorrect_remove_lines(corrector, newline_pos, count)
  else
    autocorrect_insert_lines(corrector, newline_pos, count)
  end
end

#check_defs(nodes) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 54

def check_defs(nodes)
  return if blank_lines_between?(*nodes)
  return if multiple_blank_lines_groups?(*nodes)
  return if nodes.all?(&:single_line?) &&
            cop_config['AllowAdjacentOneLineDefs']

  location = nodes.last.loc.keyword.join(nodes.last.loc.name)
  add_offense(location) do |corrector|
    autocorrect(corrector, *nodes)
  end
end

#on_begin(node) ⇒ Object

We operate on begin nodes, instead of using OnMethodDef, so that we can walk over pairs of consecutive nodes and efficiently access a node’s predecessor; #prev_node ends up doing a linear scan over siblings, so we don’t want to call it on each def.



47
48
49
50
51
52
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 47

def on_begin(node)
  node.children.each_cons(2) do |prev, n|
    nodes = [prev, n]
    check_defs(nodes) if nodes.all?(&method(:def_node?))
  end
end