Class: SyntaxTree::VarField

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

VarField represents a variable that is being assigned a value. As such, it is always a child of an assignment type node.

variable = value

In the example above, the VarField node represents the variable token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ VarField

Returns a new instance of VarField.



12937
12938
12939
12940
12941
# File 'lib/syntax_tree.rb', line 12937

def initialize(value:, location:, comments: [])
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12935
12936
12937
# File 'lib/syntax_tree.rb', line 12935

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12932
12933
12934
# File 'lib/syntax_tree.rb', line 12932

def location
  @location
end

#valueObject (readonly)

nil | Const | CVar | GVar | Ident | IVar

the target of this node



12929
12930
12931
# File 'lib/syntax_tree.rb', line 12929

def value
  @value
end

Instance Method Details

#child_nodesObject



12943
12944
12945
# File 'lib/syntax_tree.rb', line 12943

def child_nodes
  [value]
end

#format(q) ⇒ Object



12947
12948
12949
# File 'lib/syntax_tree.rb', line 12947

def format(q)
  q.format(value) if value
end

#pretty_print(q) ⇒ Object



12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
# File 'lib/syntax_tree.rb', line 12951

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



12962
12963
12964
12965
12966
# File 'lib/syntax_tree.rb', line 12962

def to_json(*opts)
  { type: :var_field, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end