123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::HeredocIndentation

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/layout/heredoc_indentation.rb

Overview

Checks the indentation of the here document bodies. The bodies are indented one step.

Note
When Layout/LineLength's AllowHeredoc is false (not default), this cop does not add any offenses for long here documents to avoid Layout/LineLength's offenses.

Examples:

# bad
<<-RUBY
something
RUBY

# good
<<~RUBY
  something
RUBY

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::Alignment - Included

SPACE

::RuboCop::Cop::Heredoc - Included

OPENING_DELIMITER

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::TargetRubyVersion - Extended

::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 an 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::Heredoc - Included

::RuboCop::Cop::Alignment - 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_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

This class inherits a constructor from RuboCop::Cop::Base

Instance Attribute Details

#unlimited_heredoc_length?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 107

def unlimited_heredoc_length?
  config.for_cop('Layout/LineLength')['AllowHeredoc']
end

Instance Method Details

#adjust_minus(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 120

def adjust_minus(corrector, node)
  heredoc_beginning = node.source
  corrected = heredoc_beginning.sub(/<<-?/, '<<~')
  corrector.replace(node, corrected)
end

#adjust_squiggly(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 115

def adjust_squiggly(corrector, node)
  corrector.replace(node.loc.heredoc_body, indented_body(node))
  corrector.replace(node.loc.heredoc_end, indented_end(node))
end

#base_indent_level(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 144

def base_indent_level(node)
  base_line_num = node.source_range.line
  base_line = processed_source.lines[base_line_num - 1]
  indent_level(base_line)
end

#heredoc_body(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 155

def heredoc_body(node)
  node.loc.heredoc_body.source
end

#heredoc_end(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 159

def heredoc_end(node)
  node.loc.heredoc_end.source
end

#heredoc_indent_type(node) (private)

Returns '~', '-' or nil

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 151

def heredoc_indent_type(node)
  node.source[/^<<([~-])/, 1]
end

#indented_body(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 126

def indented_body(node)
  body = heredoc_body(node)
  body_indent_level = indent_level(body)
  correct_indent_level = base_indent_level(node) + configured_indentation_width
  body.gsub(/^[^\S\r\n]{#{body_indent_level}}/, ' ' * correct_indent_level)
end

#indented_end(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 133

def indented_end(node)
  end_ = heredoc_end(node)
  end_indent_level = indent_level(end_)
  correct_indent_level = base_indent_level(node)
  if end_indent_level < correct_indent_level
    end_.gsub(/^\s{#{end_indent_level}}/, ' ' * correct_indent_level)
  else
    end_
  end
end

#line_too_long?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 91

def line_too_long?(node)
  return false if unlimited_heredoc_length?

  body = heredoc_body(node)

  expected_indent = base_indent_level(node) + configured_indentation_width
  actual_indent = indent_level(body)
  increase_indent_level = expected_indent - actual_indent

  longest_line(body).size + increase_indent_level >= max_line_length
end

#longest_line(lines) (private)

[ GitHub ]

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

def longest_line(lines)
  lines.each_line.max_by { |line| line.chomp.size }.chomp
end

#max_line_length (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 111

def max_line_length
  config.for_cop('Layout/LineLength')['Max']
end

#message(heredoc_indent_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 69

def message(heredoc_indent_type)
  current_indent_type = "<<#{heredoc_indent_type}"

  if current_indent_type == '<<~'
    width_message(configured_indentation_width)
  else
    type_message(configured_indentation_width, current_indent_type)
  end
end

#on_heredoc(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 36

def on_heredoc(node)
  body = heredoc_body(node)
  return if body.strip.empty?

  body_indent_level = indent_level(body)
  heredoc_indent_type = heredoc_indent_type(node)

  if heredoc_indent_type == '~'
    expected_indent_level = base_indent_level(node) + configured_indentation_width
    return if expected_indent_level == body_indent_level
  else
    return unless body_indent_level.zero?
  end

  return if line_too_long?(node)

  register_offense(node, heredoc_indent_type)
end

#register_offense(node, heredoc_indent_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 57

def register_offense(node, heredoc_indent_type)
  message = message(heredoc_indent_type)

  add_offense(node.loc.heredoc_body, message: message) do |corrector|
    if heredoc_indent_type == '~'
      adjust_squiggly(corrector, node)
    else
      adjust_minus(corrector, node)
    end
  end
end

#type_message(indentation_width, current_indent_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 79

def type_message(indentation_width, current_indent_type)
  format(
    TYPE_MSG,
    indentation_width: indentation_width,
    current_indent_type: current_indent_type
  )
end

#width_message(indentation_width) (private)

[ GitHub ]

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

def width_message(indentation_width)
  format(WIDTH_MSG, indentation_width: indentation_width)
end