Class: Prism::Merge::BeginNodeRescueSemantics

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_analysis:, dest_analysis:) ⇒ BeginNodeRescueSemantics

Returns a new instance of BeginNodeRescueSemantics.



8
9
10
11
# File 'lib/prism/merge/begin_node_rescue_semantics.rb', line 8

def initialize(template_analysis:, dest_analysis:)
  @template_analysis = template_analysis
  @dest_analysis = dest_analysis
end

Instance Attribute Details

#dest_analysisObject (readonly)

Returns the value of attribute dest_analysis.



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

def dest_analysis
  @dest_analysis
end

#template_analysisObject (readonly)

Returns the value of attribute template_analysis.



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

def template_analysis
  @template_analysis
end

Instance Method Details

#canonicalize_begin_clause_kind_order(clause_types) ⇒ Object



56
57
58
# File 'lib/prism/merge/begin_node_rescue_semantics.rb', line 56

def canonicalize_begin_clause_kind_order(clause_types)
  ruby_rescue_semantics.canonicalize_begin_clause_kind_order(clause_types)
end

#canonicalize_rescue_clause_order(clause_types) ⇒ Object



52
53
54
# File 'lib/prism/merge/begin_node_rescue_semantics.rb', line 52

def canonicalize_rescue_clause_order(clause_types)
  ruby_rescue_semantics.canonicalize_rescue_clause_order(clause_types)
end

#merge_ordered_clause_types(primary_types, secondary_types) ⇒ Object



48
49
50
# File 'lib/prism/merge/begin_node_rescue_semantics.rb', line 48

def merge_ordered_clause_types(primary_types, secondary_types)
  ruby_rescue_semantics.merge_ordered_clause_types(primary_types, secondary_types)
end

#normalized_clause_body_and_header_source(template_clause_node:, dest_clause_node:, clause_body:, preferred_source:) ⇒ Object



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
# File 'lib/prism/merge/begin_node_rescue_semantics.rb', line 13

def normalized_clause_body_and_header_source(template_clause_node:, dest_clause_node:, clause_body:,
                                             preferred_source:)
  unless template_clause_node.type.to_s == 'rescue_node' && dest_clause_node.type.to_s == 'rescue_node'
    return { header_source: preferred_source,
             clause_body: clause_body }
  end

  template_reference = rescue_node_reference_name(template_clause_node)
  dest_reference = rescue_node_reference_name(dest_clause_node)
  return { header_source: preferred_source, clause_body: clause_body } if template_reference == dest_reference

  merged_references = local_variable_read_names_in_source(clause_body)
  needs_template_reference = template_reference && merged_references.include?(template_reference)
  needs_dest_reference = dest_reference && merged_references.include?(dest_reference)

  header_source = if needs_dest_reference && !needs_template_reference
                    :destination
                  elsif needs_template_reference && !needs_dest_reference
                    :template
                  else
                    preferred_source
                  end

  chosen_reference = header_source == :template ? template_reference : dest_reference
  alternate_reference = header_source == :template ? dest_reference : template_reference
  normalized_body = if chosen_reference && alternate_reference && merged_references.include?(alternate_reference)
                      rewrite_local_reference_in_source(clause_body, from: alternate_reference,
                                                                     to: chosen_reference)
                    else
                      clause_body
                    end

  { header_source: header_source, clause_body: normalized_body }
end