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

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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



5192
5193
5194
5195
5196
5197
5198
# File 'lib/syntax_tree/node.rb', line 5192

def initialize(parent:, operator:, name:, location:)
  @parent = parent
  @operator = operator
  @name = name
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5190
5191
5192
# File 'lib/syntax_tree/node.rb', line 5190

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5187
5188
5189
# File 'lib/syntax_tree/node.rb', line 5187

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5184
5185
5186
# File 'lib/syntax_tree/node.rb', line 5184

def operator
  @operator
end

#parentObject (readonly)

untyped

the parent object that owns the field being assigned



5181
5182
5183
# File 'lib/syntax_tree/node.rb', line 5181

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5241
5242
5243
5244
# File 'lib/syntax_tree/node.rb', line 5241

def ===(other)
  other.is_a?(Field) && parent === other.parent &&
    operator === other.operator && name === other.name
end

#accept(visitor) ⇒ Object



5200
5201
5202
# File 'lib/syntax_tree/node.rb', line 5200

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

#child_nodesObject Also known as: deconstruct



5204
5205
5206
# File 'lib/syntax_tree/node.rb', line 5204

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

#copy(parent: nil, operator: nil, name: nil, location: nil) ⇒ Object



5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
# File 'lib/syntax_tree/node.rb', line 5208

def copy(parent: nil, operator: nil, name: nil, location: nil)
  node =
    Field.new(
      parent: parent || self.parent,
      operator: operator || self.operator,
      name: name || self.name,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5223
5224
5225
5226
5227
5228
5229
5230
5231
# File 'lib/syntax_tree/node.rb', line 5223

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

#format(q) ⇒ Object



5233
5234
5235
5236
5237
5238
5239
# File 'lib/syntax_tree/node.rb', line 5233

def format(q)
  q.group do
    q.format(parent)
    q.format(CallOperatorFormatter.new(operator), stackable: false)
    q.format(name)
  end
end