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

Checks whether class/module/method definitions are separated by one or more empty lines.

‘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 definitions are considered an offense.

Examples:

EmptyLineBetweenMethodDefs: true (default)

# checks for empty lines between method definitions.  # bad def a end def b end # good def a end def b end

EmptyLineBetweenClassDefs: true (default)

# checks for empty lines between class definitions.  # bad class A end class B end def b end # good class A end class B end def b end

EmptyLineBetweenModuleDefs: true (default)

# checks for empty lines between module definitions.  # bad module A end module B end def b end # good module A end module B end def b end

AllowAdjacentOneLineDefs: true (default)

 # good class ErrorA < BaseError; end class ErrorB < BaseError; end # good class ErrorA < BaseError; end class ErrorB < BaseError; end # good - DefLikeMacros: [memoize] memoize :attribute_a memoize :attribute_b # good memoize :attribute_a memoize :attribute_b

AllowAdjacentOneLineDefs: false

 # bad class ErrorA < BaseError; end class ErrorB < BaseError; end # good class ErrorA < BaseError; end class ErrorB < BaseError; end # bad - DefLikeMacros: [memoize] memoize :attribute_a memoize :attribute_b # good memoize :attribute_a memoize :attribute_b

Constant Summary collapse

MSG =
'Expected %<expected>s between %<type>s definitions; found %<actual>d.'

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants inherited from Base

Base::RESTRICT_ON_SEND

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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, 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?, #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_gem_version, #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

Class Method Details

.autocorrect_incompatible_withObject

 120 121 122
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 120 def self.autocorrect_incompatible_with [Layout::EmptyLines] end

Instance Method Details

#autocorrect(corrector, prev_def, node, count) ⇒ Object

 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 151 def autocorrect(corrector, prev_def, node, count) # finds position of first newline  end_pos = end_loc(prev_def).end_pos source_buffer = end_loc(prev_def).source_buffer newline_pos = source_buffer.source.index("\n", end_pos) # Handle the case when multiple one-liners are on the same line.  begin_pos = node.source_range.begin_pos newline_pos = begin_pos - 1 if newline_pos > begin_pos 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

 136 137 138 139 140 141 142 143 144 145 146 147 148 149
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 136 def check_defs(nodes) count = blank_lines_count_between(*nodes) return if line_count_allowed?(count) return if multiple_blank_lines_groups?(*nodes) return if nodes.all?(&:single_line?) && cop_config['AllowAdjacentOneLineDefs'] correction_node = nodes.last location = def_location(correction_node) add_offense(location, message: message(correction_node, count: count)) do |corrector| autocorrect(corrector, *nodes, count) 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.

 129 130 131 132 133 134
# File 'lib/rubocop/cop/layout/empty_line_between_defs.rb', line 129 def on_begin(node) node.children.each_cons(2) do |prev, n| nodes = [prev, n] check_defs(nodes) if nodes.all? { |def_candidate| candidate?(def_candidate) } end end