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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Field.



5145
5146
5147
5148
5149
5150
5151
# File 'lib/syntax_tree/node.rb', line 5145

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



5143
5144
5145
# File 'lib/syntax_tree/node.rb', line 5143

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5140
5141
5142
# File 'lib/syntax_tree/node.rb', line 5140

def location
  @location
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5137
5138
5139
# File 'lib/syntax_tree/node.rb', line 5137

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5134
5135
5136
# File 'lib/syntax_tree/node.rb', line 5134

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



5131
5132
5133
# File 'lib/syntax_tree/node.rb', line 5131

def parent
  @parent
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



5153
5154
5155
# File 'lib/syntax_tree/node.rb', line 5153

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

#deconstruct_keys(keys) ⇒ Object



5159
5160
5161
5162
5163
5164
5165
5166
5167
# File 'lib/syntax_tree/node.rb', line 5159

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

#format(q) ⇒ Object



5169
5170
5171
5172
5173
5174
5175
# File 'lib/syntax_tree/node.rb', line 5169

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

#pretty_print(q) ⇒ Object



5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
# File 'lib/syntax_tree/node.rb', line 5177

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

    q.breakable
    q.pp(parent)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(name)

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

#to_json(*opts) ⇒ Object



5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
# File 'lib/syntax_tree/node.rb', line 5194

def to_json(*opts)
  {
    type: :field,
    parent: parent,
    op: operator,
    name: name,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end