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`.'

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

#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, joining_forces, lint?, match?, #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

silence_warnings

Constructor Details

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

Returns a new instance of EmptyLinesAroundAccessModifier.


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

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

  @block_line = nil
end

Instance Method Details

#on_block(node) ⇒ Object


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

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

#on_class(node) ⇒ Object


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

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


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

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


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

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


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

def on_send(node)
  return unless node.bare_access_modifier? && !node.parent&.block_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