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.



5319
5320
5321
5322
5323
5324
5325
# File 'lib/syntax_tree/node.rb', line 5319

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



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

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5314
5315
5316
# File 'lib/syntax_tree/node.rb', line 5314

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



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

def operator
  @operator
end

#parentObject (readonly)

Node

the parent object that owns the field being assigned



5308
5309
5310
# File 'lib/syntax_tree/node.rb', line 5308

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5369
5370
5371
5372
# File 'lib/syntax_tree/node.rb', line 5369

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

#accept(visitor) ⇒ Object



5327
5328
5329
# File 'lib/syntax_tree/node.rb', line 5327

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

#child_nodesObject Also known as: deconstruct



5331
5332
5333
5334
# File 'lib/syntax_tree/node.rb', line 5331

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

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



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

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



5351
5352
5353
5354
5355
5356
5357
5358
5359
# File 'lib/syntax_tree/node.rb', line 5351

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

#format(q) ⇒ Object



5361
5362
5363
5364
5365
5366
5367
# File 'lib/syntax_tree/node.rb', line 5361

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