Class: SyntaxTree::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Field.



5548
5549
5550
5551
5552
5553
5554
# File 'lib/syntax_tree.rb', line 5548

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



5546
5547
5548
# File 'lib/syntax_tree.rb', line 5546

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5543
5544
5545
# File 'lib/syntax_tree.rb', line 5543

def location
  @location
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5540
5541
5542
# File 'lib/syntax_tree.rb', line 5540

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5537
5538
5539
# File 'lib/syntax_tree.rb', line 5537

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



5534
5535
5536
# File 'lib/syntax_tree.rb', line 5534

def parent
  @parent
end

Instance Method Details

#child_nodesObject



5556
5557
5558
# File 'lib/syntax_tree.rb', line 5556

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

#format(q) ⇒ Object



5560
5561
5562
5563
5564
5565
5566
# File 'lib/syntax_tree.rb', line 5560

def format(q)
  q.group do
    q.format(parent)
    q.format(CallOperatorFormatter.new(operator))
    q.format(name)
  end
end

#pretty_print(q) ⇒ Object



5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
# File 'lib/syntax_tree.rb', line 5568

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("field")

    q.breakable
    q.pp(parent)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(name)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
# File 'lib/syntax_tree.rb', line 5585

def to_json(*opts)
  {
    type: :field,
    parent: parent,
    op: operator,
    name: name,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end