Class: SyntaxTree::Assign

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

Overview

Assign represents assigning something to a variable or constant. Generally, the left side of the assignment is going to be any node that ends with the name “Field”.

variable = value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Assign.



1765
1766
1767
1768
1769
1770
# File 'lib/syntax_tree.rb', line 1765

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1763
1764
1765
# File 'lib/syntax_tree.rb', line 1763

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



1760
1761
1762
# File 'lib/syntax_tree.rb', line 1760

def location
  @location
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1754
1755
1756
# File 'lib/syntax_tree.rb', line 1754

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1757
1758
1759
# File 'lib/syntax_tree.rb', line 1757

def value
  @value
end

Instance Method Details

#child_nodesObject



1772
1773
1774
# File 'lib/syntax_tree.rb', line 1772

def child_nodes
  [target, value]
end

#format(q) ⇒ Object



1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
# File 'lib/syntax_tree.rb', line 1776

def format(q)
  q.group do
    q.format(target)
    q.text(" =")

    if target.comments.empty? && (skip_indent_target? || skip_indent_value?)
      q.text(" ")
      q.format(value)
    else
      q.indent do
        q.breakable
        q.format(value)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
# File 'lib/syntax_tree.rb', line 1793

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

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



1807
1808
1809
1810
1811
1812
1813
1814
1815
# File 'lib/syntax_tree.rb', line 1807

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