Class: HamlLint::RubyExtraction::BaseChunk

Inherits:
Object
  • Object
show all
Defined in:
lib/haml_lint/ruby_extraction/base_chunk.rb

Overview

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.

Constant Summary collapse

COMMA_CHANGES_LINES =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, ruby_lines, end_marker_indent:, haml_line_index: node.line - 1) ⇒ BaseChunk

Returns a new instance of BaseChunk.



34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#end_marker_indentInteger (readonly)

Returns The indentation (number of spaces) to use to index the marker that follows this chunk. Unlike the marker before, this one can vary.

Returns:

  • (Integer)

    The indentation (number of spaces) to use to index the marker that follows this chunk. Unlike the marker before, this one can vary.



29
30
31
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 29

def end_marker_indent
  @end_marker_indent
end

#haml_line_indexInteger (readonly)

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

Returns:

  • (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.



21
22
23
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 21

def haml_line_index
  @haml_line_index
end

#nodeHamlLint::Tree::Node (readonly)

Returns Haml node that this comes from.

Returns:



16
17
18
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 16

def node
  @node
end

#ruby_linesArray<String> (readonly)

Returns The ruby lines that this chunk will insert.

Returns:

  • (Array<String>)

    The ruby lines that this chunk will insert



32
33
34
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 32

def ruby_lines
  @ruby_lines
end

#start_marker_line_numberInteger (readonly)

Returns Line number of the line marker in the ruby source placed before this auto-correctable code.

Returns:

  • (Integer)

    Line number of the line marker in the ruby source placed before this auto-correctable code



25
26
27
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 25

def start_marker_line_number
  @start_marker_line_number
end

Instance Method Details

#assemble_in(coordinator) ⇒ Object



100
101
102
103
104
# 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

#full_assemble(coordinator) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# 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

#fuse(_following_chunk) ⇒ Object

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



47
48
49
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 47

def fuse(_following_chunk)
  nil
end

#haml_end_line_indexObject



80
81
82
83
# 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

#nb_haml_linesObject



85
86
87
# 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

#skip_line_indexes_in_source_mapObject



106
107
108
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 106

def skip_line_indexes_in_source_map
  []
end

#start_marker_indentObject



76
77
78
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 76

def start_marker_indent
  ruby_lines.first[/ */].size
end

#transfer_correction(coordinator, _all_corrected_ruby_lines, haml_lines) ⇒ Object

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



61
62
63
64
# 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

#transfer_correction_logic(_coordinator, _to_ruby_lines, _haml_lines) ⇒ Object

To be overriden 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)



72
73
74
# 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

#wrap_in_markersObject



110
111
112
# File 'lib/haml_lint/ruby_extraction/base_chunk.rb', line 110

def wrap_in_markers
  true
end