Class: RuboCop::Cop::Layout::HeredocIndentation
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::TargetRubyVersion,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::Heredoc,
::RuboCop::Cop::Alignment,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| 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.
|
Constant Summary
-
TYPE_MSG =
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 32'Use %<indentation_width>d spaces for indentation in a ' \ 'heredoc by using `<<~` instead of `%<current_indent_type>s`.'
-
WIDTH_MSG =
# File 'lib/rubocop/cop/layout/heredoc_indentation.rb', line 34'Use %<indentation_width>d spaces for indentation in a heredoc.'
::RuboCop::Cop::Base - Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::Alignment - Included
::RuboCop::Cop::Heredoc - Included
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 |
Class Method Summary
::RuboCop::Cop::TargetRubyVersion - Extended
| maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version? |
::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
- #unlimited_heredoc_length? ⇒ Boolean readonly private
::RuboCop::Cop::Alignment - Included
::RuboCop::Cop::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
Instance Method Summary
- #on_heredoc(node)
- #adjust_minus(corrector, node) private
- #adjust_squiggly(corrector, node) private
- #base_indent_level(node) private
- #heredoc_body(node) private
- #heredoc_end(node) private
-
#heredoc_indent_type(node)
private
Returns '~', '-' or nil.
- #indented_body(node) private
- #indented_end(node) private
- #line_too_long?(node) ⇒ Boolean private
- #longest_line(lines) private
- #max_line_length private
- #message(heredoc_indent_type) private
- #register_offense(node, heredoc_indent_type) private
- #type_message(indentation_width, current_indent_type) private
- #width_message(indentation_width) private
::RuboCop::Cop::Heredoc - Included
| #on_dstr | Alias for Heredoc#on_str. |
| #on_heredoc, #on_str, | |
| #on_xstr | Alias for Heredoc#on_str. |
| #delimiter_string, #heredoc_type, #indent_level | |
::RuboCop::Cop::Alignment - Included
| #check_alignment, #configured_indentation_width, #display_column, #each_bad_alignment, #end_of_line_comment, #indentation, #offset, #register_offense, #within? |
::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 |
| #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
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
# 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)
# 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 (heredoc_indent_type) current_indent_type = "<<#{heredoc_indent_type}" if current_indent_type == '<<~' (configured_indentation_width) else (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) = (heredoc_indent_type) add_offense(node.loc.heredoc_body, 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 (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 (indentation_width) format(WIDTH_MSG, indentation_width: indentation_width) end