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:) ⇒ ConflictResolver

Initialize a conflict resolver

Parameters:

  • preference (Symbol)

    Which version wins on conflict (:template or :destination)

  • template_analysis (FileAnalysis)

    Analysis of the template file

  • dest_analysis (FileAnalysis)

    Analysis of the destination file



22
23
24
25
26
27
28
29
# File 'lib/rbs/merge/conflict_resolver.rb', line 22

def initialize(preference:, template_analysis:, dest_analysis:)
  super(
    strategy: :node,
    preference: preference,
    template_analysis: template_analysis,
    dest_analysis: dest_analysis
  )
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)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbs/merge/conflict_resolver.rb', line 82

def can_recursive_merge?(template_decl, dest_decl)
  # Only container types can be recursively merged
  container_types = [
    RBS::AST::Declarations::Class,
    RBS::AST::Declarations::Module,
    RBS::AST::Declarations::Interface,
  ]

  template_decl.class == dest_decl.class &&
    container_types.any? { |type| template_decl.is_a?(type) } &&
    template_decl.respond_to?(:members) &&
    dest_decl.respond_to?(:members) &&
    template_decl.members.any? &&
    dest_decl.members.any?
end

#declarations_identical?(decl1, decl2) ⇒ Boolean

Check if two declarations are identical

Parameters:

  • decl1 (Object)

    First declaration

  • decl2 (Object)

    Second declaration

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/rbs/merge/conflict_resolver.rb', line 73

def declarations_identical?(decl1, decl2)
  # Use RBS's built-in equality
  decl1 == decl2
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



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/rbs/merge/conflict_resolver.rb', line 38

def resolve(template_decl, dest_decl, template_index:, dest_index:)
  # Freeze blocks always win (they represent protected content)
  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
  case @preference
  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