Class: SyntaxTree::Assign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Assign represents assigning something to a variable or constant. Generally, the left side of the assignment is going to be any node that ends with the name “Field”.

variable = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(target:, value:, location:, comments: []) ⇒ Assign

Returns a new instance of Assign.



1109
1110
1111
1112
1113
1114
# File 'lib/syntax_tree/node.rb', line 1109

def initialize(target:, value:, location:, comments: [])
  @target = target
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1107
1108
1109
# File 'lib/syntax_tree/node.rb', line 1107

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1101
1102
1103
# File 'lib/syntax_tree/node.rb', line 1101

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1104
1105
1106
# File 'lib/syntax_tree/node.rb', line 1104

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1116
1117
1118
# File 'lib/syntax_tree/node.rb', line 1116

def accept(visitor)
  visitor.visit_assign(self)
end

#child_nodesObject Also known as: deconstruct



1120
1121
1122
# File 'lib/syntax_tree/node.rb', line 1120

def child_nodes
  [target, value]
end

#deconstruct_keys(keys) ⇒ Object



1126
1127
1128
# File 'lib/syntax_tree/node.rb', line 1126

def deconstruct_keys(keys)
  { target: target, value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
# File 'lib/syntax_tree/node.rb', line 1130

def format(q)
  q.group do
    q.format(target)
    q.text(" =")

    if skip_indent?
      q.text(" ")
      q.format(value)
    else
      q.indent do
        q.breakable
        q.format(value)
      end
    end
  end
end