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

#pretty_print, #to_json

Constructor Details

#initialize(message:, arguments:, location:, comments: []) ⇒ Command

Returns a new instance of Command.



2517
2518
2519
2520
2521
2522
# File 'lib/syntax_tree/node.rb', line 2517

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

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments being sent with the message



2512
2513
2514
# File 'lib/syntax_tree/node.rb', line 2512

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2515
2516
2517
# File 'lib/syntax_tree/node.rb', line 2515

def comments
  @comments
end

#messageObject (readonly)

Const | Ident

the message being sent to the implicit receiver



2509
2510
2511
# File 'lib/syntax_tree/node.rb', line 2509

def message
  @message
end

Instance Method Details

#accept(visitor) ⇒ Object



2524
2525
2526
# File 'lib/syntax_tree/node.rb', line 2524

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

#child_nodesObject Also known as: deconstruct



2528
2529
2530
# File 'lib/syntax_tree/node.rb', line 2528

def child_nodes
  [message, arguments]
end

#deconstruct_keys(keys) ⇒ Object



2534
2535
2536
2537
2538
2539
2540
2541
# File 'lib/syntax_tree/node.rb', line 2534

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

#format(q) ⇒ Object



2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
# File 'lib/syntax_tree/node.rb', line 2543

def format(q)
  q.group do
    q.format(message)
    q.text(" ")

    if align?(self)
      q.nest(message.value.length + 1) { q.format(arguments) }
    else
      q.format(arguments)
    end
  end
end