Class: Solargraph::Parser::ParserGem::NodeProcessors::OpasgnNode

Inherits:
NodeProcessor::Base show all
Defined in:
lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb

Instance Attribute Summary

Attributes inherited from NodeProcessor::Base

#locals, #node, #pins, #region

Instance Method Summary collapse

Methods inherited from NodeProcessor::Base

#initialize

Constructor Details

This class inherits a constructor from Solargraph::Parser::NodeProcessor::Base

Instance Method Details

#processvoid



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb', line 11

def process
  target = node.children[0]
  operator = node.children[1]
  argument = node.children[2]
  if target.type == :send
    # @sg-ignore Need a downcast here

    process_send_target(target, operator, argument)
  elsif target.type.to_s.end_with?('vasgn')
    # @sg-ignore Need a downcast here

    process_vasgn_target(target, operator, argument)
  else
    Solargraph.assert_or_log(:opasgn_unknown_target,
                             "Unexpected op_asgn target type: #{target.type}")
  end
end

#process_send_target(call, operator, argument) ⇒ void



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
# File 'lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb', line 32

def process_send_target call, operator, argument
  # if target is a call:

  # [10] pry(main)> Parser::CurrentRuby.parse("Foo.bar += baz")

  # => s(:op_asgn,

  #      s(:send, # call

  #        s(:const, nil, :Foo), # calee

  #        :bar), # call_method

  #      :+, # operator

  #      s(:send, nil, :baz)) # argument

  # [11] pry(main)>

  callee = call.children[0]
  call_method = call.children[1]
  asgn_method = :"#{call_method}="

  # [8] pry(main)> Parser::CurrentRuby.parse("Foo.bar = Foo.bar + baz")

  # => s(:send,

  #       s(:const, nil, :Foo), # callee

  #       :bar=, # asgn_method

  #        s(:send,

  #          s(:send,

  #             s(:const, nil, :Foo), # callee

  #             :bar), # call_method

  #          :+, # operator

  #          s(:send, nil, :baz))) # argument

  new_send = node.updated(:send,
                          [callee,
                           asgn_method,
                           node.updated(:send, [call, operator, argument])])
  NodeProcessor.process(new_send, region, pins, locals)
end

#process_vasgn_target(asgn, operator, argument) ⇒ void



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/solargraph/parser/parser_gem/node_processors/opasgn_node.rb', line 68

def process_vasgn_target asgn, operator, argument
  # => s(:op_asgn,

  #      s(:lvasgn, :a), # asgn

  #      :+, # operator

  #      s(:int, 2)) # argument


  # @type [Parser::AST::Node]

  variable_name = asgn.children[0]
  # for lvasgn, gvasgn, cvasgn, convert to lvar, gvar, cvar

  # [6] pry(main)> Parser::CurrentRuby.parse("a = a + 1")

  # => s(:lvasgn, :a,

  #   s(:send,

  #     s(:lvar, :a), :+,

  #     s(:int, 1)))

  # [7] pry(main)>

  variable_reference_type = asgn.type.to_s.sub(/vasgn$/, 'var').to_sym
  target_reference = node.updated(variable_reference_type, asgn.children)
  send_children = [
    target_reference,
    operator,
    argument
  ]
  send_node = node.updated(:send, send_children)
  new_asgn = node.updated(asgn.type, [variable_name,  send_node])
  NodeProcessor.process(new_asgn, region, pins, locals)
end