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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of Field.



4594
4595
4596
4597
4598
4599
4600
# File 'lib/syntax_tree/node.rb', line 4594

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4592
4593
4594
# File 'lib/syntax_tree/node.rb', line 4592

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



4589
4590
4591
# File 'lib/syntax_tree/node.rb', line 4589

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



4586
4587
4588
# File 'lib/syntax_tree/node.rb', line 4586

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



4583
4584
4585
# File 'lib/syntax_tree/node.rb', line 4583

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object



4602
4603
4604
# File 'lib/syntax_tree/node.rb', line 4602

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

#child_nodesObject Also known as: deconstruct



4606
4607
4608
# File 'lib/syntax_tree/node.rb', line 4606

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

#deconstruct_keys(_keys) ⇒ Object



4612
4613
4614
4615
4616
4617
4618
4619
4620
# File 'lib/syntax_tree/node.rb', line 4612

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

#format(q) ⇒ Object



4622
4623
4624
4625
4626
4627
4628
# File 'lib/syntax_tree/node.rb', line 4622

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