Class: SyntaxTree::Assign

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of Assign.



1061
1062
1063
1064
1065
1066
# File 'lib/syntax_tree/node.rb', line 1061

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



1059
1060
1061
# File 'lib/syntax_tree/node.rb', line 1059

def comments
  @comments
end

#targetObject (readonly)

ARefField | ConstPathField | Field | TopConstField | VarField

the target

to assign the result of the expression to



1053
1054
1055
# File 'lib/syntax_tree/node.rb', line 1053

def target
  @target
end

#valueObject (readonly)

untyped

the expression to be assigned



1056
1057
1058
# File 'lib/syntax_tree/node.rb', line 1056

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



1068
1069
1070
# File 'lib/syntax_tree/node.rb', line 1068

def accept(visitor)
  visitor.visit_assign(self)
end

#child_nodesObject Also known as: deconstruct



1072
1073
1074
# File 'lib/syntax_tree/node.rb', line 1072

def child_nodes
  [target, value]
end

#deconstruct_keys(keys) ⇒ Object



1078
1079
1080
# File 'lib/syntax_tree/node.rb', line 1078

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

#format(q) ⇒ Object



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/syntax_tree/node.rb', line 1082

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

    if skip_indent?
      q.text(" ")
      q.format(value)
    else
      q.indent do
        q.breakable
        q.format(value)
      end
    end
  end
end