Class: RuboCop::AST::SendNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rubocop/ast/node/send_node.rb

Overview

A node extension for ‘send` nodes. This will be used in place of a plain node when the builder constructs the AST, making its methods available to all `send` nodes within RuboCop.

Constant Summary collapse

MACRO_PARENT_NODES =
%i(class module).freeze

Constants inherited from Node

Node::BASIC_LITERALS, Node::COMPARISON_OPERATORS, Node::COMPOSITE_LITERALS, Node::FALSEY_LITERALS, Node::IMMUTABLE_LITERALS, Node::KEYWORDS, Node::LITERALS, Node::MUTABLE_LITERALS, Node::OPERATOR_KEYWORDS, Node::REFERENCES, Node::SPECIAL_KEYWORDS, Node::TRUTHY_LITERALS, Node::VARIABLES

Instance Method Summary collapse

Methods inherited from Node

#ancestors, #argument?, #asgn_method_call?, #basic_literal?, #binary_operation?, #chained?, #child_nodes, #complete!, #complete?, #const_name, def_matcher, #defined_module, #defined_module_name, #descendants, #each_ancestor, #each_child_node, #each_descendant, #each_node, #falsey_literal?, #immutable_literal?, #initialize, #keyword?, #keyword_bang?, #keyword_not?, #literal?, #multiline?, #mutable_literal?, #numeric_type?, #operator_keyword?, #parent, #parent_module_name, #pure?, #reference?, #sibling_index, #single_line?, #source, #source_range, #special_keyword?, #truthy_literal?, #unary_operation?, #updated, #value_used?, #variable?

Methods included from Sexp

#s

Constructor Details

This class inherits a constructor from RuboCop::AST::Node

Instance Method Details

#argumentsArray<Node>

An array containing the arguments of the method invocation.

Returns:

  • (Array<Node>)

    the arguments of the method invocation or ‘nil`



56
57
58
# File 'lib/rubocop/ast/node/send_node.rb', line 56

def arguments
  node_parts[2..-1]
end

#arguments?Boolean

Checks whether this method was invoked with arguments.

Returns:

  • (Boolean)

    whether this method was invoked with arguments



81
82
83
# File 'lib/rubocop/ast/node/send_node.rb', line 81

def arguments?
  !arguments.empty?
end

#bang_method?Boolean

Checks whether the invoked method is a bang method.

Returns:

  • (Boolean)

    whether the invoked method is a bang method



153
154
155
# File 'lib/rubocop/ast/node/send_node.rb', line 153

def bang_method?
  method_name.to_s.end_with?('!')
end

#block_argument?Boolean

Whether the last argument of the method invocation is a block pass, i.e. ‘&block`.

Returns:

  • (Boolean)

    whether the invoked method is a block pass



169
170
171
# File 'lib/rubocop/ast/node/send_node.rb', line 169

def block_argument?
  arguments? && last_argument.block_pass_type?
end

#camel_case_method?Boolean

Checks whether the invoked method is a camel case method, e.g. ‘Integer()`.

Returns:

  • (Boolean)

    whether the invoked method is a camel case method



161
162
163
# File 'lib/rubocop/ast/node/send_node.rb', line 161

def camel_case_method?
  method_name.to_s =~ /\A[A-Z]/
end

#command?(name) ⇒ Boolean

Checks whether the method name matches the argument and has an implicit receiver.

Parameters:

  • name (Symbol, String)

    the method name to check for

Returns:

  • (Boolean)

    whether the method name matches the argument



49
50
51
# File 'lib/rubocop/ast/node/send_node.rb', line 49

def command?(name)
  !receiver && method?(name)
end

#dot?Boolean

Checks whether the method call uses a dot to connect the receiver and the method name.

This is useful for comparison operators, which can be called either with or without a dot, i.e. ‘foo == bar` or `foo.== bar`.

Returns:

  • (Boolean)

    whether the method was called with a connecting dot



115
116
117
# File 'lib/rubocop/ast/node/send_node.rb', line 115

def dot?
  loc.dot && loc.dot.is?('.')
end

#double_colon?Boolean

Checks whether the method call uses a double colon to connect the receiver and the method name.

Returns:

  • (Boolean)

    whether the method was called with a connecting dot



123
124
125
# File 'lib/rubocop/ast/node/send_node.rb', line 123

def double_colon?
  loc.dot && loc.dot.is?('::')
end

#first_argumentNode?

A shorthand for getting the first argument of the method invocation. Equivalent to ‘arguments.first`.

Returns:

  • (Node, nil)

    the first argument of the method invocation, or ‘nil` if there are no arguments



65
66
67
# File 'lib/rubocop/ast/node/send_node.rb', line 65

def first_argument
  arguments[0]
end

#implicit_call?Boolean

Checks whether the method call is of the implicit form of ‘#call`, e.g. `foo.(bar)`.

Returns:

  • (Boolean)

    whether the method is an implicit form of ‘#call`



139
140
141
# File 'lib/rubocop/ast/node/send_node.rb', line 139

def implicit_call?
  method_name == :call && !loc.selector
end

#last_argumentNode?

A shorthand for getting the last argument of the method invocation. Equivalent to ‘arguments.last`.

Returns:

  • (Node, nil)

    the last argument of the method invocation, or ‘nil` if there are no arguments



74
75
76
# File 'lib/rubocop/ast/node/send_node.rb', line 74

def last_argument
  arguments[-1]
end

#macro?Boolean

Note:

This does not include DSLs that use nested blocks, like RSpec

Checks whether the method is a macro method. A macro method is defined as a method that sits in a class- or module body and has an implicit receiver.

Returns:

  • (Boolean)

    whether the method is a macro method



40
41
42
# File 'lib/rubocop/ast/node/send_node.rb', line 40

def macro?
  !receiver && MACRO_PARENT_NODES.include?(parent && parent.type)
end

#method?(name) ⇒ Boolean

Checks whether the method name matches the argument.

Parameters:

  • name (Symbol, String)

    the method name to check for

Returns:

  • (Boolean)

    whether the method name matches the argument



29
30
31
# File 'lib/rubocop/ast/node/send_node.rb', line 29

def method?(name)
  method_name == name.to_sym
end

#method_nameSymbol

The name of the invoked method called as a string.

Returns:

  • (Symbol)

    the name of the invoked method



21
22
23
# File 'lib/rubocop/ast/node/send_node.rb', line 21

def method_name
  node_parts[1]
end

#node_partsArray

Custom destructuring method. This can be used to normalize destructuring for different variations of the node.

Returns:

  • (Array)

    the different parts of the ‘send` node



185
186
187
# File 'lib/rubocop/ast/node/send_node.rb', line 185

def node_parts
  [*self]
end

#operator_method?Boolean

Checks whether the invoked method is an operator method.

Returns:

  • (Boolean)

    whether the invoked method is an operator



104
105
106
# File 'lib/rubocop/ast/node/send_node.rb', line 104

def operator_method?
  RuboCop::Cop::Util::OPERATOR_METHODS.include?(method_name)
end

#parenthesized?Boolean

Checks whether this method invocation’s arguments are wrapped in parentheses.

Returns:

  • (Boolean)

    whether this method invocation’s arguments are wrapped in parentheses



90
91
92
# File 'lib/rubocop/ast/node/send_node.rb', line 90

def parenthesized?
  loc.end && loc.end.is?(')')
end

#predicate_method?Boolean

Checks whether the invoked method is a predicate method.

Returns:

  • (Boolean)

    whether the invoked method is a predicate method



146
147
148
# File 'lib/rubocop/ast/node/send_node.rb', line 146

def predicate_method?
  method_name.to_s.end_with?('?')
end

#receiverNode?

The receiving node of the method invocation.

Returns:

  • (Node, nil)

    the receiver of the invoked method or ‘nil`



14
15
16
# File 'lib/rubocop/ast/node/send_node.rb', line 14

def receiver
  node_parts[0]
end

#self_receiver?Boolean

Checks whether the receiver of this method invocation is ‘self`.

Returns:

  • (Boolean)

    whether the receiver of this method invocation is ‘self`



131
132
133
# File 'lib/rubocop/ast/node/send_node.rb', line 131

def self_receiver?
  receiver && receiver.self_type?
end

#setter_method?Boolean

Checks whether the invoked method is a setter method.

Returns:

  • (Boolean)

    whether the invoked method is a setter



97
98
99
# File 'lib/rubocop/ast/node/send_node.rb', line 97

def setter_method?
  loc.operator
end

#splat_argument?Boolean

Checks whether any argument of the method invocation is a splat argument, i.e. ‘*splat`.

Returns:

  • (Boolean)

    whether the invoked method is a splat argument



177
178
179
# File 'lib/rubocop/ast/node/send_node.rb', line 177

def splat_argument?
  arguments? && arguments.any?(&:splat_type?)
end