Class: Rbs::Merge::ConflictResolver

Inherits:
Ast::Merge::ConflictResolverBase
  • Object
show all
Defined in:
lib/rbs/merge/conflict_resolver.rb

Overview

Resolves conflicts between template and destination declarations.
Determines which version to use when both files have declarations
with matching signatures but different content.

Examples:

Basic usage

resolver = ConflictResolver.new(
  preference: :destination,
  template_analysis: template_analysis,
  dest_analysis: dest_analysis
)
winner = resolver.resolve(template_decl, dest_decl)

Instance Method Summary collapse

Constructor Details

#initialize(preference:, template_analysis:, dest_analysis:, node_typing: nil, **options) ⇒ ConflictResolver

Initialize a conflict resolver

Parameters:

  • preference (Symbol, Hash)

    Which version wins on conflict (:template or :destination)
    or per-node-type Hash when used with node_typing

  • template_analysis (FileAnalysis)

    Analysis of the template file

  • dest_analysis (FileAnalysis)

    Analysis of the destination file

  • node_typing (Hash{Symbol,String => #call}, nil) (defaults to: nil)

    Node typing configuration
    for per-node-type preferences

  • options (Hash)

    Additional options for forward compatibility



26
27
28
29
30
31
32
33
34
35
# File 'lib/rbs/merge/conflict_resolver.rb', line 26

def initialize(preference:, template_analysis:, dest_analysis:, node_typing: nil, **options)
  super(
    strategy: :node,
    preference: preference,
    template_analysis: template_analysis,
    dest_analysis: dest_analysis,
    **options
  )
  @node_typing = node_typing
end

Instance Method Details

#can_recursive_merge?(template_decl, dest_decl) ⇒ Boolean

Check if declarations can be recursively merged

Parameters:

  • template_decl (Object)

    Template declaration

  • dest_decl (Object)

    Destination declaration

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbs/merge/conflict_resolver.rb', line 98

def can_recursive_merge?(template_decl, dest_decl)
  # Only container types can be recursively merged
  # Container types are: class, module, interface

  template_type = canonical_type(template_decl)
  dest_type = canonical_type(dest_decl)

  # Both must be the same container type
  return false unless template_type == dest_type
  return false unless NodeTypeNormalizer.container_type?(template_type)

  # Both must have members
  has_members?(template_decl) && has_members?(dest_decl)
end

#declarations_identical?(decl1, decl2) ⇒ Boolean

Check if two declarations are identical

Parameters:

  • decl1 (Object)

    First declaration

  • decl2 (Object)

    Second declaration

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/rbs/merge/conflict_resolver.rb', line 85

def declarations_identical?(decl1, decl2)
  # Compare text content for backend-agnostic comparison
  text1 = extract_declaration_text(decl1, @template_analysis)
  text2 = extract_declaration_text(decl2, @dest_analysis)

  # Normalize whitespace for comparison
  normalize_text(text1) == normalize_text(text2)
end

#resolve(template_decl, dest_decl, template_index:, dest_index:) ⇒ Hash

Resolve a conflict between template and destination declarations

Parameters:

  • template_decl (Object)

    Template declaration

  • dest_decl (Object)

    Destination declaration

  • template_index (Integer)

    Index in template statements

  • dest_index (Integer)

    Index in destination statements

Returns:

  • (Hash)

    Resolution result with :source and :declaration keys



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbs/merge/conflict_resolver.rb', line 44

def resolve(template_decl, dest_decl, template_index:, dest_index:)
  # Freeze blocks always win (they represent protected content)
  # Template freeze blocks take precedence - frozen content from template is preserved
  if freeze_node?(template_decl)
    return {source: :template, declaration: template_decl, decision: DECISION_FREEZE_BLOCK}
  end

  # Destination freeze blocks also win (though less common)
  if freeze_node?(dest_decl)
    return {source: :destination, declaration: dest_decl, decision: DECISION_FREEZE_BLOCK}
  end

  # Check if declarations are identical
  if declarations_identical?(template_decl, dest_decl)
    # Prefer destination to minimize diffs
    return {source: :destination, declaration: dest_decl, decision: DECISION_DESTINATION}
  end

  # Check if we should recursively merge (for container types)
  if can_recursive_merge?(template_decl, dest_decl)
    return {
      source: :recursive,
      template_declaration: template_decl,
      dest_declaration: dest_decl,
      decision: DECISION_RECURSIVE,
    }
  end

  # Apply preference (supports per-node-type preferences with node_typing)
  case resolve_preference(template_decl, dest_decl)
  when :template
    {source: :template, declaration: template_decl, decision: DECISION_TEMPLATE}
  else # :destination (validated in initialize)
    {source: :destination, declaration: dest_decl, decision: DECISION_DESTINATION}
  end
end