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:, comments: []) ⇒ Field

Returns a new instance of Field.



4413
4414
4415
4416
4417
4418
4419
# File 'lib/syntax_tree/node.rb', line 4413

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



4411
4412
4413
# File 'lib/syntax_tree/node.rb', line 4411

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



4408
4409
4410
# File 'lib/syntax_tree/node.rb', line 4408

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



4405
4406
4407
# File 'lib/syntax_tree/node.rb', line 4405

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



4402
4403
4404
# File 'lib/syntax_tree/node.rb', line 4402

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object



4421
4422
4423
# File 'lib/syntax_tree/node.rb', line 4421

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

#child_nodesObject Also known as: deconstruct



4425
4426
4427
# File 'lib/syntax_tree/node.rb', line 4425

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

#deconstruct_keys(_keys) ⇒ Object



4431
4432
4433
4434
4435
4436
4437
4438
4439
# File 'lib/syntax_tree/node.rb', line 4431

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

#format(q) ⇒ Object



4441
4442
4443
4444
4445
4446
4447
# File 'lib/syntax_tree/node.rb', line 4441

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