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

Constructor Details

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

Returns a new instance of Assign.



1297
1298
1299
1300
1301
1302
# File 'lib/syntax_tree/node.rb', line 1297

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



1295
1296
1297
# File 'lib/syntax_tree/node.rb', line 1295

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1289
1290
1291
# File 'lib/syntax_tree/node.rb', line 1289

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1292
1293
1294
# File 'lib/syntax_tree/node.rb', line 1292

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



1304
1305
1306
# File 'lib/syntax_tree/node.rb', line 1304

def child_nodes
  [target, value]
end

#deconstruct_keys(keys) ⇒ Object



1310
1311
1312
# File 'lib/syntax_tree/node.rb', line 1310

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

#format(q) ⇒ Object



1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'lib/syntax_tree/node.rb', line 1314

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

#pretty_print(q) ⇒ Object



1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
# File 'lib/syntax_tree/node.rb', line 1331

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("assign")

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



1345
1346
1347
1348
1349
1350
1351
1352
1353
# File 'lib/syntax_tree/node.rb', line 1345

def to_json(*opts)
  {
    type: :assign,
    target: target,
    value: value,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end