Class: HamlLint::RubyExtraction::TagAttributesChunk

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

Overview

Chunk for handling the a tag attributes, such as ‘%div’yes_please’‘

Constant Summary

Constants inherited from BaseChunk

BaseChunk::COMMA_CHANGES_LINES

Instance Attribute Summary

Attributes inherited from BaseChunk

#end_marker_indent, #haml_line_index, #node, #ruby_lines, #start_marker_line_number

Instance Method Summary collapse

Methods inherited from BaseChunk

#assemble_in, #full_assemble, #fuse, #haml_end_line_index, #nb_haml_lines, #skip_line_indexes_in_source_map, #start_marker_indent, #transfer_correction, #wrap_in_markers

Constructor Details

#initialize(*args, indent_to_remove:, **kwargs) ⇒ TagAttributesChunk

Returns a new instance of TagAttributesChunk.



6
7
8
9
# File 'lib/haml_lint/ruby_extraction/tag_attributes_chunk.rb', line 6

def initialize(*args, indent_to_remove:, **kwargs)
  super(*args, **kwargs)
  @indent_to_remove = indent_to_remove
end

Instance Method Details

#transfer_correction_logic(_coordinator, to_ruby_lines, haml_lines) ⇒ Object

rubocop:disable Metrics



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/haml_lint/ruby_extraction/tag_attributes_chunk.rb', line 11

def transfer_correction_logic(_coordinator, to_ruby_lines, haml_lines) # rubocop:disable Metrics
  return if @ruby_lines == to_ruby_lines

  affected_haml_lines = haml_lines[@haml_line_index..haml_end_line_index]

  affected_haml = affected_haml_lines.join("\n")

  from_ruby = unwrap(@ruby_lines).join("\n")

  if to_ruby_lines.size > 1
    min_indent = to_ruby_lines.first[/^\s*/]
    to_ruby_lines.each.with_index do |line, i|
      next if i == 0
      next if line.start_with?(min_indent)
      to_ruby_lines[i] = "#{min_indent}#{line.lstrip}"
    end
  end

  to_ruby = unwrap(to_ruby_lines).join("\n")

  affected_start_index = affected_haml.index(from_ruby)
  if affected_start_index
    affected_end_index = affected_start_index + from_ruby.size
  else
    regexp = HamlLint::Utils.regexp_for_parts(from_ruby.split("\n"), "(?:\s*\\|?\n)")
    mo = affected_haml.match(regexp)
    affected_start_index = mo.begin(0)
    affected_end_index = mo.end(0)
  end

  affected_haml[affected_start_index...affected_end_index] = to_ruby

  haml_lines[@haml_line_index..haml_end_line_index] = affected_haml.split("\n")

  if haml_lines[haml_end_line_index].end_with?(' |')
    haml_lines[haml_end_line_index].chop!.rstrip!
  end
end

#unwrap(lines) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/haml_lint/ruby_extraction/tag_attributes_chunk.rb', line 50

def unwrap(lines)
  lines = lines.dup
  lines[0] = lines[0].sub(/^\s*/, '').sub(/W+\(/, '')
  lines[-1] = lines[-1].sub(/\)\s*\Z/, '')

  if @indent_to_remove
    HamlLint::Utils.map_after_first!(lines) do |line|
      line.sub(/^ {1,#{@indent_to_remove}}/, '')
    end
  end
  lines
end