Class: RuboCop::Cop::Layout::EmptyLinesAroundAccessModifier

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

Overview

Access modifiers should be surrounded by blank lines.

Examples:

EnforcedStyle: around (default)


# bad
class Foo
  def bar; end
  private
  def baz; end
end

# good
class Foo
  def bar; end

  private

  def baz; end
end

EnforcedStyle: only_before


# bad
class Foo
  def bar; end
  private
  def baz; end
end

# good
class Foo
  def bar; end

  private
  def baz; end
end

Constant Summary collapse

MSG_AFTER =
'Keep a blank line after `%<modifier>s`.'
MSG_BEFORE_AND_AFTER =
'Keep a blank line before and after `%<modifier>s`.'
MSG_BEFORE_FOR_ONLY_BEFORE =
'Keep a blank line before `%<modifier>s`.'
MSG_AFTER_FOR_ONLY_BEFORE =
'Remove a blank line after `%<modifier>s`.'
RESTRICT_ON_SEND =
%i[public protected private module_function].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

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, #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_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

#initialize(config = nil, options = nil) ⇒ EmptyLinesAroundAccessModifier

Returns a new instance of EmptyLinesAroundAccessModifier.



56
57
58
59
60
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 56

def initialize(config = nil, options = nil)
  super

  @block_line = nil
end

Instance Method Details

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



81
82
83
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 81

def on_block(node)
  @block_line = node.source_range.first_line
end

#on_class(node) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 62

def on_class(node)
  @class_or_module_def_first_line = if node.parent_class
                                      node.parent_class.first_line
                                    else
                                      node.source_range.first_line
                                    end
  @class_or_module_def_last_line = node.source_range.last_line
end

#on_module(node) ⇒ Object



71
72
73
74
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 71

def on_module(node)
  @class_or_module_def_first_line = node.source_range.first_line
  @class_or_module_def_last_line = node.source_range.last_line
end

#on_sclass(node) ⇒ Object



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

def on_sclass(node)
  @class_or_module_def_first_line = node.identifier.source_range.first_line
  @class_or_module_def_last_line = node.source_range.last_line
end

#on_send(node) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 87

def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
  return unless node.bare_access_modifier? &&
                !(node.parent&.block_type? || node.parent&.numblock_type?)
  return if expected_empty_lines?(node)

  message = message(node)
  add_offense(node, message: message) do |corrector|
    line = range_by_whole_lines(node.source_range)

    corrector.insert_before(line, "\n") unless previous_line_empty?(node.first_line)

    correct_next_line_if_denied_style(corrector, node, line)
  end
end