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.



5900
5901
5902
5903
5904
5905
5906
# File 'lib/syntax_tree.rb', line 5900

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



5898
5899
5900
# File 'lib/syntax_tree.rb', line 5898

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



5895
5896
5897
# File 'lib/syntax_tree.rb', line 5895

def location
  @location
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5892
5893
5894
# File 'lib/syntax_tree.rb', line 5892

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5889
5890
5891
# File 'lib/syntax_tree.rb', line 5889

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



5886
5887
5888
# File 'lib/syntax_tree.rb', line 5886

def parent
  @parent
end

Instance Method Details

#child_nodesObject



5908
5909
5910
# File 'lib/syntax_tree.rb', line 5908

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

#format(q) ⇒ Object



5912
5913
5914
5915
5916
5917
5918
# File 'lib/syntax_tree.rb', line 5912

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

#pretty_print(q) ⇒ Object



5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
# File 'lib/syntax_tree.rb', line 5920

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



5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
# File 'lib/syntax_tree.rb', line 5937

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