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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Command.



3188
3189
3190
3191
3192
3193
# File 'lib/syntax_tree/node.rb', line 3188

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



3180
3181
3182
# File 'lib/syntax_tree/node.rb', line 3180

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3186
3187
3188
# File 'lib/syntax_tree/node.rb', line 3186

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3183
3184
3185
# File 'lib/syntax_tree/node.rb', line 3183

def location
  @location
end

#messageObject (readonly)

Const | Ident

the message being sent to the implicit receiver



3177
3178
3179
# File 'lib/syntax_tree/node.rb', line 3177

def message
  @message
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3195
3196
3197
# File 'lib/syntax_tree/node.rb', line 3195

def child_nodes
  [message, arguments]
end

#deconstruct_keys(keys) ⇒ Object



3201
3202
3203
3204
3205
3206
3207
3208
# File 'lib/syntax_tree/node.rb', line 3201

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

#format(q) ⇒ Object



3210
3211
3212
3213
3214
3215
3216
# File 'lib/syntax_tree/node.rb', line 3210

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



3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
# File 'lib/syntax_tree/node.rb', line 3218

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



3232
3233
3234
3235
3236
3237
3238
3239
3240
# File 'lib/syntax_tree/node.rb', line 3232

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