class HamlLint::RubyExtraction::BaseChunk
This is the base class for all of the Chunks of HamlLint::RubyExtraction
. A Chunk represents a part of the HAML file that HamlLint::Linter::RuboCop
is processing and will insert some Ruby code in a file passed to RuboCop.
There are chunks for most HAML concepts, even if they don’t represent Ruby code. For example, there is a chunk that represents a ‘%div` tag, which uses a `begin` in the generated Ruby to add indentation for the children of the %div in the Ruby file just like there is in the HAML file.
Constants
- COMMA_CHANGES_LINES
Attributes
@return [Integer] The indentation (number of spaces) to use to index the marker
that follows this chunk. Unlike the marker before, this one can vary.
@return [Integer] First line index of the auto-correctable code in the Haml
source
Usually same as node.line - 1, but some cases, such as interpolation in a filter will will be different.
@return [HamlLint::Tree::Node] Haml
node that this comes from
@return [Array<String>] The ruby lines that this chunk will insert
@return [Integer] Line number of the line marker in the ruby source placed before
this auto-correctable code
Public Class Methods
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 34 def initialize(node, ruby_lines, end_marker_indent:, haml_line_index: node.line - 1) ruby_lines = [ruby_lines] if ruby_lines.is_a?(String) @node = node @ruby_lines = ruby_lines @haml_line_index = haml_line_index @end_marker_indent = end_marker_indent end
Public Instance Methods
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 100 def assemble_in(coordinator) coordinator.add_lines(@ruby_lines, haml_line_index: haml_line_index, skip_indexes_in_source_map: skip_line_indexes_in_source_map) end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 89 def full_assemble(coordinator) if wrap_in_markers @start_marker_line_number = coordinator.add_marker(start_marker_indent, haml_line_index: haml_line_index) assemble_in(coordinator) coordinator.add_marker(@end_marker_indent, haml_line_index: haml_end_line_index) else assemble_in(coordinator) end end
To be overridden in subclasses. Return a new chunk which is the result of fusing self with the given following chunk. If no fusion is possible, returns nil
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 47 def fuse(_following_chunk) nil end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 80 def haml_end_line_index # the .max is needed to handle cases with 0 nb_haml_lines [@haml_line_index + nb_haml_lines - 1, @haml_line_index].max end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 85 def nb_haml_lines @ruby_lines.size - skip_line_indexes_in_source_map.size end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 106 def skip_line_indexes_in_source_map [] end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 76 def start_marker_indent ruby_lines.first[/ */].size end
Overwrites haml_lines to match the Ruby code that was corrected by RuboCop which is in all_corrected_ruby_lines. This can change non-ruby parts to, especially for indentation.
This will be called on ruby chunks in the reverse order they were created. Two benefits of this approach:
-
No need to track when lines in haml_lines are moved to apply changes later in the file
-
When fixing indentation of lines that follow a corrected line, those following lines will already have been corrected and so require nothing.
Can be overridden by subclasses to make it do nothing
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 61 def transfer_correction(coordinator, _all_corrected_ruby_lines, haml_lines) to_ruby_lines = coordinator.extract_from_corrected_lines(@start_marker_line_number, @ruby_lines.size) transfer_correction_logic(coordinator, to_ruby_lines, haml_lines) end
To be overridden by subclasses.
Logic to transfer the corrections that turned from_ruby_lines into to_ruby_lines.
This method only received the ruby code that belongs to this chunk. (It was extracted using extract_from by transfer_correction
)
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 72 def transfer_correction_logic(_coordinator, _to_ruby_lines, _haml_lines) raise "Implement #transfer_correction_logic in #{self.class.name}" end
# File lib/haml_lint/ruby_extraction/base_chunk.rb, line 110 def wrap_in_markers true end