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

Constructor Details

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

Returns a new instance of Field.



4971
4972
4973
4974
4975
4976
4977
# File 'lib/syntax_tree/node.rb', line 4971

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



4969
4970
4971
# File 'lib/syntax_tree/node.rb', line 4969

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



4966
4967
4968
# File 'lib/syntax_tree/node.rb', line 4966

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



4963
4964
4965
# File 'lib/syntax_tree/node.rb', line 4963

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



4960
4961
4962
# File 'lib/syntax_tree/node.rb', line 4960

def parent
  @parent
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



4979
4980
4981
# File 'lib/syntax_tree/node.rb', line 4979

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

#deconstruct_keys(keys) ⇒ Object



4985
4986
4987
4988
4989
4990
4991
4992
4993
# File 'lib/syntax_tree/node.rb', line 4985

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

#format(q) ⇒ Object



4995
4996
4997
4998
4999
5000
5001
# File 'lib/syntax_tree/node.rb', line 4995

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



5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
# File 'lib/syntax_tree/node.rb', line 5003

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



5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
# File 'lib/syntax_tree/node.rb', line 5020

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