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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Field.



4374
4375
4376
4377
4378
4379
4380
# File 'lib/syntax_tree/node.rb', line 4374

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



4372
4373
4374
# File 'lib/syntax_tree/node.rb', line 4372

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



4369
4370
4371
# File 'lib/syntax_tree/node.rb', line 4369

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



4366
4367
4368
# File 'lib/syntax_tree/node.rb', line 4366

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



4363
4364
4365
# File 'lib/syntax_tree/node.rb', line 4363

def parent
  @parent
end

Instance Method Details

#accept(visitor) ⇒ Object



4382
4383
4384
# File 'lib/syntax_tree/node.rb', line 4382

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

#child_nodesObject Also known as: deconstruct



4386
4387
4388
# File 'lib/syntax_tree/node.rb', line 4386

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

#deconstruct_keys(keys) ⇒ Object



4392
4393
4394
4395
4396
4397
4398
4399
4400
# File 'lib/syntax_tree/node.rb', line 4392

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

#format(q) ⇒ Object



4402
4403
4404
4405
4406
4407
4408
# File 'lib/syntax_tree/node.rb', line 4402

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