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



1212
1213
1214
1215
1216
1217
# File 'lib/syntax_tree/node.rb', line 1212

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



1210
1211
1212
# File 'lib/syntax_tree/node.rb', line 1210

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1204
1205
1206
# File 'lib/syntax_tree/node.rb', line 1204

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1207
1208
1209
# File 'lib/syntax_tree/node.rb', line 1207

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1219
1220
1221
# File 'lib/syntax_tree/node.rb', line 1219

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

#child_nodesObject Also known as: deconstruct



1223
1224
1225
# File 'lib/syntax_tree/node.rb', line 1223

def child_nodes
  [target, value]
end

#deconstruct_keys(_keys) ⇒ Object



1229
1230
1231
# File 'lib/syntax_tree/node.rb', line 1229

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

#format(q) ⇒ Object



1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/syntax_tree/node.rb', line 1233

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_space
        q.format(value)
      end
    end
  end
end