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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of Field.



5252
5253
5254
5255
5256
5257
5258
# File 'lib/syntax_tree/node.rb', line 5252

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



5250
5251
5252
# File 'lib/syntax_tree/node.rb', line 5250

def comments
  @comments
end

#nameObject (readonly)

Const | Ident

the name of the field being assigned



5247
5248
5249
# File 'lib/syntax_tree/node.rb', line 5247

def name
  @name
end

#operatorObject (readonly)

:“::” | Op | Period

the operator being used for the assignment



5244
5245
5246
# File 'lib/syntax_tree/node.rb', line 5244

def operator
  @operator
end

#parentObject (readonly)

Node

the parent object that owns the field being assigned



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

def parent
  @parent
end

Instance Method Details

#===(other) ⇒ Object



5301
5302
5303
5304
# File 'lib/syntax_tree/node.rb', line 5301

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

#accept(visitor) ⇒ Object



5260
5261
5262
# File 'lib/syntax_tree/node.rb', line 5260

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

#child_nodesObject Also known as: deconstruct



5264
5265
5266
# File 'lib/syntax_tree/node.rb', line 5264

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

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



5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
# File 'lib/syntax_tree/node.rb', line 5268

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



5283
5284
5285
5286
5287
5288
5289
5290
5291
# File 'lib/syntax_tree/node.rb', line 5283

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

#format(q) ⇒ Object



5293
5294
5295
5296
5297
5298
5299
# File 'lib/syntax_tree/node.rb', line 5293

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