Class: SyntaxTree::Field

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

Overview

Field is always the child of an assignment. It represents assigning to a “field” on an object.

object.variable = value

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(parent:, operator:, name:, location:) ⇒ Field

Returns a new instance of Field.



5304
5305
5306
5307
5308
5309
5310
# File 'lib/syntax_tree/node.rb', line 5304

def initialize(parent:, operator:, name:, location:)
  @parent = parent
  @operator = operator
  @name = name
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5302
5303
5304
# File 'lib/syntax_tree/node.rb', line 5302

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5299
5300
5301
# File 'lib/syntax_tree/node.rb', line 5299

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5296
5297
5298
# File 'lib/syntax_tree/node.rb', line 5296

def operator
  @operator
end

#parentObject (readonly)

Node

the parent object that owns the field being assigned



5293
5294
5295
# File 'lib/syntax_tree/node.rb', line 5293

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5354
5355
5356
5357
# File 'lib/syntax_tree/node.rb', line 5354

def ===(other)
  other.is_a?(Field) && parent === other.parent &&
    operator === other.operator && name === other.name
end

#accept(visitor) ⇒ Object



5312
5313
5314
# File 'lib/syntax_tree/node.rb', line 5312

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

#child_nodesObject Also known as: deconstruct



5316
5317
5318
5319
# File 'lib/syntax_tree/node.rb', line 5316

def child_nodes
  operator = self.operator
  [parent, (operator if operator != :"::"), name]
end

#copy(parent: nil, operator: nil, name: nil, location: nil) ⇒ Object



5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
# File 'lib/syntax_tree/node.rb', line 5321

def copy(parent: nil, operator: nil, name: nil, location: nil)
  node =
    Field.new(
      parent: parent || self.parent,
      operator: operator || self.operator,
      name: name || self.name,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5336
5337
5338
5339
5340
5341
5342
5343
5344
# File 'lib/syntax_tree/node.rb', line 5336

def deconstruct_keys(_keys)
  {
    parent: parent,
    operator: operator,
    name: name,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5346
5347
5348
5349
5350
5351
5352
# File 'lib/syntax_tree/node.rb', line 5346

def format(q)
  q.group do
    q.format(parent)
    q.format(CallOperatorFormatter.new(operator), stackable: false)
    q.format(name)
  end
end