Class: Prism::Merge::CommentOnlyFileMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/merge/comment_only_file_merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merger:) ⇒ CommentOnlyFileMerger

Returns a new instance of CommentOnlyFileMerger.



8
9
10
11
12
13
14
# File 'lib/prism/merge/comment_only_file_merger.rb', line 8

def initialize(merger:)
  @merger = merger
  @comment_only_prefix_lines = {
    template: Set.new,
    destination: Set.new
  }
end

Instance Attribute Details

#comment_only_prefix_linesObject (readonly)

Returns the value of attribute comment_only_prefix_lines.



6
7
8
# File 'lib/prism/merge/comment_only_file_merger.rb', line 6

def comment_only_prefix_lines
  @comment_only_prefix_lines
end

#mergerObject (readonly)

Returns the value of attribute merger.



6
7
8
# File 'lib/prism/merge/comment_only_file_merger.rb', line 6

def merger
  @merger
end

Instance Method Details

#comment_only_file?(analysis) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'lib/prism/merge/comment_only_file_merger.rb', line 16

def comment_only_file?(analysis)
  statements = analysis.statements
  return false if statements.nil? || statements.empty?

  statements.all? do |statement|
    statement.is_a?(Ast::Merge::Comment::Empty) ||
      statement.is_a?(Ast::Merge::Comment::Block) ||
      statement.is_a?(Ast::Merge::Comment::Line)
  end
end

#mergeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prism/merge/comment_only_file_merger.rb', line 27

def merge
  @comment_only_prefix_lines = {
    template: Set.new,
    destination: Set.new
  }

  template_lines = merger.template_content.lines.map(&:chomp)
  dest_lines = merger.dest_content.lines.map(&:chomp)

  emit_comment_only_prefix_lines(template_lines, dest_lines)

  template_nodes = Comment::Parser.parse(template_lines)
  dest_nodes = Comment::Parser.parse(dest_lines)
  template_context = build_comment_only_merge_context(nodes: template_nodes, lines: template_lines,
                                                      source: :template)
  dest_context = build_comment_only_merge_context(nodes: dest_nodes, lines: dest_lines, source: :destination)
  output_plan = build_output_plan(template_context: template_context, dest_context: dest_context)
  retained_owners_by_source = output_plan.group_by { |entry| entry[:source] }
                                         .transform_values do |entries|
    entries.map do |entry|
      entry[:node]
    end
  end
  emitted_gap_keys = {
    template: Set.new,
    destination: Set.new
  }

  output_plan.each do |entry|
    context = entry[:source] == :template ? template_context : dest_context
    emit_comment_node_with_layout(
      node: entry[:node],
      source: entry[:source],
      context: context,
      retained_owners: retained_owners_by_source.fetch(entry[:source], []),
      emitted_gap_keys: emitted_gap_keys
    )
  end

  merger.result
end