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

Constructor Details

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

Returns a new instance of Command.



3089
3090
3091
3092
3093
3094
# File 'lib/syntax_tree/node.rb', line 3089

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



3084
3085
3086
# File 'lib/syntax_tree/node.rb', line 3084

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3087
3088
3089
# File 'lib/syntax_tree/node.rb', line 3087

def comments
  @comments
end

#messageObject (readonly)

Const | Ident

the message being sent to the implicit receiver



3081
3082
3083
# File 'lib/syntax_tree/node.rb', line 3081

def message
  @message
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3096
3097
3098
# File 'lib/syntax_tree/node.rb', line 3096

def child_nodes
  [message, arguments]
end

#deconstruct_keys(keys) ⇒ Object



3102
3103
3104
3105
3106
3107
3108
3109
# File 'lib/syntax_tree/node.rb', line 3102

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

#format(q) ⇒ Object



3111
3112
3113
3114
3115
3116
3117
# File 'lib/syntax_tree/node.rb', line 3111

def format(q)
  q.group do
    q.format(message)
    q.text(" ")
    q.nest(message.value.length + 1) { q.format(arguments) }
  end
end

#pretty_print(q) ⇒ Object



3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
# File 'lib/syntax_tree/node.rb', line 3119

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("command")

    q.breakable
    q.pp(message)

    q.breakable
    q.pp(arguments)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3133
3134
3135
3136
3137
3138
3139
3140
3141
# File 'lib/syntax_tree/node.rb', line 3133

def to_json(*opts)
  {
    type: :command,
    message: message,
    args: arguments,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end