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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Field.



4012
4013
4014
4015
4016
4017
4018
# File 'lib/syntax_tree/node.rb', line 4012

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



4010
4011
4012
# File 'lib/syntax_tree/node.rb', line 4010

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



4007
4008
4009
# File 'lib/syntax_tree/node.rb', line 4007

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



4004
4005
4006
# File 'lib/syntax_tree/node.rb', line 4004

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



4001
4002
4003
# File 'lib/syntax_tree/node.rb', line 4001

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object



4020
4021
4022
# File 'lib/syntax_tree/node.rb', line 4020

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

#child_nodesObject Also known as: deconstruct



4024
4025
4026
# File 'lib/syntax_tree/node.rb', line 4024

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

#deconstruct_keys(keys) ⇒ Object



4030
4031
4032
4033
4034
4035
4036
4037
4038
# File 'lib/syntax_tree/node.rb', line 4030

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

#format(q) ⇒ Object



4040
4041
4042
4043
4044
4045
4046
# File 'lib/syntax_tree/node.rb', line 4040

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