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.



5301
5302
5303
5304
5305
5306
5307
# File 'lib/syntax_tree/node.rb', line 5301

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



5299
5300
5301
# File 'lib/syntax_tree/node.rb', line 5299

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



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

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5293
5294
5295
# File 'lib/syntax_tree/node.rb', line 5293

def operator
  @operator
end

#parentObject (readonly)

Node

the parent object that owns the field being assigned



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

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5351
5352
5353
5354
# File 'lib/syntax_tree/node.rb', line 5351

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

#accept(visitor) ⇒ Object



5309
5310
5311
# File 'lib/syntax_tree/node.rb', line 5309

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

#child_nodesObject Also known as: deconstruct



5313
5314
5315
5316
# File 'lib/syntax_tree/node.rb', line 5313

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

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



5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
# File 'lib/syntax_tree/node.rb', line 5318

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



5333
5334
5335
5336
5337
5338
5339
5340
5341
# File 'lib/syntax_tree/node.rb', line 5333

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

#format(q) ⇒ Object



5343
5344
5345
5346
5347
5348
5349
# File 'lib/syntax_tree/node.rb', line 5343

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