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.



5283
5284
5285
5286
5287
5288
5289
# File 'lib/syntax_tree/node.rb', line 5283

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



5281
5282
5283
# File 'lib/syntax_tree/node.rb', line 5281

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5278
5279
5280
# File 'lib/syntax_tree/node.rb', line 5278

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5275
5276
5277
# File 'lib/syntax_tree/node.rb', line 5275

def operator
  @operator
end

#parentObject (readonly)

Node

the parent object that owns the field being assigned



5272
5273
5274
# File 'lib/syntax_tree/node.rb', line 5272

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



5291
5292
5293
# File 'lib/syntax_tree/node.rb', line 5291

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

#child_nodesObject Also known as: deconstruct



5295
5296
5297
# File 'lib/syntax_tree/node.rb', line 5295

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

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



5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
# File 'lib/syntax_tree/node.rb', line 5299

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



5314
5315
5316
5317
5318
5319
5320
5321
5322
# File 'lib/syntax_tree/node.rb', line 5314

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

#format(q) ⇒ Object



5324
5325
5326
5327
5328
5329
5330
# File 'lib/syntax_tree/node.rb', line 5324

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