Class: SyntaxTree::Command

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

Overview

Command represents a method call with arguments and no parentheses. Note that Command nodes only happen when there is no explicit receiver for this method.

method argument

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(message:, arguments:, block:, location:) ⇒ Command

Returns a new instance of Command.



3464
3465
3466
3467
3468
3469
3470
# File 'lib/syntax_tree/node.rb', line 3464

def initialize(message:, arguments:, block:, location:)
  @message = message
  @arguments = arguments
  @block = block
  @location = location
  @comments = []
end

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments being sent with the message



3456
3457
3458
# File 'lib/syntax_tree/node.rb', line 3456

def arguments
  @arguments
end

#blockObject (readonly)

nil | BlockNode

the optional block being passed to the method



3459
3460
3461
# File 'lib/syntax_tree/node.rb', line 3459

def block
  @block
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3462
3463
3464
# File 'lib/syntax_tree/node.rb', line 3462

def comments
  @comments
end

#messageObject (readonly)

Const | Ident

the message being sent to the implicit receiver



3453
3454
3455
# File 'lib/syntax_tree/node.rb', line 3453

def message
  @message
end

Instance Method Details

#===(other) ⇒ Object



3514
3515
3516
3517
# File 'lib/syntax_tree/node.rb', line 3514

def ===(other)
  other.is_a?(Command) && message === other.message &&
    arguments === other.arguments && block === other.block
end

#accept(visitor) ⇒ Object



3472
3473
3474
# File 'lib/syntax_tree/node.rb', line 3472

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

#arityObject



3519
3520
3521
# File 'lib/syntax_tree/node.rb', line 3519

def arity
  arguments.arity
end

#child_nodesObject Also known as: deconstruct



3476
3477
3478
# File 'lib/syntax_tree/node.rb', line 3476

def child_nodes
  [message, arguments, block]
end

#copy(message: nil, arguments: nil, block: nil, location: nil) ⇒ Object



3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
# File 'lib/syntax_tree/node.rb', line 3480

def copy(message: nil, arguments: nil, block: nil, location: nil)
  node =
    Command.new(
      message: message || self.message,
      arguments: arguments || self.arguments,
      block: block || self.block,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



3495
3496
3497
3498
3499
3500
3501
3502
3503
# File 'lib/syntax_tree/node.rb', line 3495

def deconstruct_keys(_keys)
  {
    message: message,
    arguments: arguments,
    block: block,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3505
3506
3507
3508
3509
3510
3511
3512
# File 'lib/syntax_tree/node.rb', line 3505

def format(q)
  q.group do
    q.format(message)
    align(q, self) { q.format(arguments) }
  end

  q.format(block) if block
end