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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Assign.



1178
1179
1180
1181
1182
1183
# File 'lib/syntax_tree/node.rb', line 1178

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



1176
1177
1178
# File 'lib/syntax_tree/node.rb', line 1176

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1170
1171
1172
# File 'lib/syntax_tree/node.rb', line 1170

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1173
1174
1175
# File 'lib/syntax_tree/node.rb', line 1173

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1185
1186
1187
# File 'lib/syntax_tree/node.rb', line 1185

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

#child_nodesObject Also known as: deconstruct



1189
1190
1191
# File 'lib/syntax_tree/node.rb', line 1189

def child_nodes
  [target, value]
end

#deconstruct_keys(_keys) ⇒ Object



1195
1196
1197
# File 'lib/syntax_tree/node.rb', line 1195

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

#format(q) ⇒ Object



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
# File 'lib/syntax_tree/node.rb', line 1199

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