123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
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

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RangeHelp - Included

#add_range, #column_offset_between,
#contents_range

A range containing only the contents of a literal with delimiters (e.g.

#directions,
#effective_column

Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.

#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?,
#target_gem_version

Returns a gems locked versions (i.e.

#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

.new(config = nil, options = nil) ⇒ EmptyLinesAroundAccessModifier

[ GitHub ]

  
# 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

#allowed_only_before_style?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 114

def allowed_only_before_style?(node)
  if node.special_modifier?
    return true if processed_source[node.last_line] == 'end'
    return false if next_line_empty?(node.last_line)
  end

  previous_line_empty?(node.first_line)
end

#block_start?(line) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 163

def block_start?(line)
  return false unless @block_line

  line == @block_line + 1
end

#body_end?(line) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 169

def body_end?(line)
  return false unless @class_or_module_def_last_line

  line == @class_or_module_def_last_line - 1
end

#class_def?(line) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 157

def class_def?(line)
  return false unless @class_or_module_def_first_line

  line == @class_or_module_def_first_line + 1
end

#correct_next_line_if_denied_style(corrector, node, line) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 123

def correct_next_line_if_denied_style(corrector, node, line)
  case style
  when :around
    corrector.insert_after(line, "\n") unless next_line_empty?(node.last_line)
  when :only_before
    if next_line_empty?(node.last_line)
      range = next_empty_line_range(node)

      corrector.remove(range)
    end
  end
end

#empty_lines_around?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 153

def empty_lines_around?(node)
  previous_line_empty?(node.first_line) && next_line_empty?(node.last_line)
end

#expected_empty_lines?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 103

def expected_empty_lines?(node)
  case style
  when :around
    return true if empty_lines_around?(node)
  when :only_before
    return true if allowed_only_before_style?(node)
  end

  false
end

#message(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 179

def message(node)
  case style
  when :around
    message_for_around_style(node)
  when :only_before
    message_for_only_before_style(node)
  end
end

#message_for_around_style(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 188

def message_for_around_style(node)
  send_line = node.first_line

  if block_start?(send_line) || class_def?(send_line)
    format(MSG_AFTER, modifier: node.loc.selector.source)
  else
    format(MSG_BEFORE_AND_AFTER, modifier: node.loc.selector.source)
  end
end

#message_for_only_before_style(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 198

def message_for_only_before_style(node)
  modifier = node.loc.selector.source

  if next_line_empty?(node.last_line)
    format(MSG_AFTER_FOR_ONLY_BEFORE, modifier: modifier)
  else
    format(MSG_BEFORE_FOR_ONLY_BEFORE, modifier: modifier)
  end
end

#next_empty_line_range(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 175

def next_empty_line_range(node)
  source_range(processed_source.buffer, node.last_line + 1, 0)
end

#next_line_empty?(last_send_line) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 147

def next_line_empty?(last_send_line)
  next_line = processed_source[last_send_line]

  body_end?(last_send_line) || next_line.blank?
end

#on_block(node) Also known as: #on_numblock

[ GitHub ]

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

[ GitHub ]

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

[ GitHub ]

  
# 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_numblock(node)

Alias for #on_block.

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 85

alias on_numblock on_block

#on_sclass(node)

[ GitHub ]

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

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 87

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

#previous_line_empty?(send_line) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 140

def previous_line_empty?(send_line)
  previous_line = previous_line_ignoring_comments(processed_source, send_line)
  return true unless previous_line

  block_start?(send_line) || class_def?(send_line) || previous_line.blank?
end

#previous_line_ignoring_comments(processed_source, send_line) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb', line 136

def previous_line_ignoring_comments(processed_source, send_line)
  processed_source[0..send_line - 2].reverse.find { |line| !comment_line?(line) }
end