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:) ⇒ Field

Returns a new instance of Field.



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

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



5195
5196
5197
# File 'lib/syntax_tree/node.rb', line 5195

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5192
5193
5194
# File 'lib/syntax_tree/node.rb', line 5192

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5189
5190
5191
# File 'lib/syntax_tree/node.rb', line 5189

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



5186
5187
5188
# File 'lib/syntax_tree/node.rb', line 5186

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5246
5247
5248
5249
# File 'lib/syntax_tree/node.rb', line 5246

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

#accept(visitor) ⇒ Object



5205
5206
5207
# File 'lib/syntax_tree/node.rb', line 5205

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

#child_nodesObject Also known as: deconstruct



5209
5210
5211
# File 'lib/syntax_tree/node.rb', line 5209

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

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



5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
# File 'lib/syntax_tree/node.rb', line 5213

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



5228
5229
5230
5231
5232
5233
5234
5235
5236
# File 'lib/syntax_tree/node.rb', line 5228

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

#format(q) ⇒ Object



5238
5239
5240
5241
5242
5243
5244
# File 'lib/syntax_tree/node.rb', line 5238

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