Class: SyntaxTree::OpAssign

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

Overview

OpAssign represents assigning a value to a variable or constant using an operator like += or ||=.

variable += value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OpAssign.



7640
7641
7642
7643
7644
7645
7646
# File 'lib/syntax_tree/node.rb', line 7640

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7638
7639
7640
# File 'lib/syntax_tree/node.rb', line 7638

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7635
7636
7637
# File 'lib/syntax_tree/node.rb', line 7635

def location
  @location
end

#operatorObject (readonly)

Op

the operator being used for the assignment



7629
7630
7631
# File 'lib/syntax_tree/node.rb', line 7629

def operator
  @operator
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



7626
7627
7628
# File 'lib/syntax_tree/node.rb', line 7626

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



7632
7633
7634
# File 'lib/syntax_tree/node.rb', line 7632

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7648
7649
7650
# File 'lib/syntax_tree/node.rb', line 7648

def child_nodes
  [target, operator, value]
end

#deconstruct_keys(keys) ⇒ Object



7654
7655
7656
7657
7658
7659
7660
7661
7662
# File 'lib/syntax_tree/node.rb', line 7654

def deconstruct_keys(keys)
  {
    target: target,
    operator: operator,
    value: value,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
# File 'lib/syntax_tree/node.rb', line 7664

def format(q)
  q.group do
    q.format(target)
    q.text(" ")
    q.format(operator)
    q.indent do
      q.breakable
      q.format(value)
    end
  end
end

#pretty_print(q) ⇒ Object



7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
# File 'lib/syntax_tree/node.rb', line 7676

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

    q.breakable
    q.pp(target)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
# File 'lib/syntax_tree/node.rb', line 7693

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