Module: RuboCop::AST::MethodIdentifierPredicates

Included in:
BlockNode, DefNode, MethodDispatchNode
Defined in:
lib/rubocop/ast/node/mixin/method_identifier_predicates.rb

Overview

Note:

this mixin expects `#method_name` and `#receiver` to be implemented

Common predicates for nodes that reference method identifiers: `send`, `csend`, `def`, `defs`, `super`, `zsuper`

Instance Method Summary collapse

Instance Method Details

#assignment_method?Boolean

Checks whether the method is an assignment method.

Returns:

  • (Boolean)

    whether the method is an assignment


142
143
144
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 142

def assignment_method?
  !comparison_method? && method_name.to_s.end_with?('=')
end

#bang_method?Boolean

Checks whether the method is a bang method.

Returns:

  • (Boolean)

    whether the method is a bang method


171
172
173
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 171

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

#camel_case_method?Boolean

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

Returns:

  • (Boolean)

    whether the method is a camel case method


179
180
181
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 179

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

#comparison_method?Boolean

Checks whether the method is a comparison method.

Returns:

  • (Boolean)

    whether the method is a comparison


135
136
137
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 135

def comparison_method?
  Node::COMPARISON_OPERATORS.include?(method_name)
end

#const_receiver?Boolean

Checks whether the explicit receiver of node is a `const` node.

Returns:

  • (Boolean)

    whether the receiver of this node is a `const` node


193
194
195
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 193

def const_receiver?
  receiver&.const_type?
end

#enumerable_method?Boolean

Checks whether the method is an Enumerable method.

Returns:

  • (Boolean)

    whether the method is an Enumerable method


157
158
159
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 157

def enumerable_method?
  ENUMERABLE_METHODS.include?(method_name)
end

#enumerator_method?Boolean

Checks whether the method is an enumerator method.

Returns:

  • (Boolean)

    whether the method is an enumerator


149
150
151
152
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 149

def enumerator_method?
  ENUMERATOR_METHODS.include?(method_name) ||
    method_name.to_s.start_with?('each_')
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


79
80
81
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 79

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

#negation_method?Boolean

Checks whether this is a negation method, i.e. `!` or keyword `not`.

Returns:

  • (Boolean)

    whether this method is a negation method


200
201
202
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 200

def negation_method?
  receiver && method_name == :!
end

#nonmutating_array_method?Boolean

Checks whether the method is a nonmutating Array method.

Returns:

  • (Boolean)

    whether the method is a nonmutating Array method


114
115
116
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 114

def nonmutating_array_method?
  NONMUTATING_ARRAY_METHODS.include?(method_name)
end

#nonmutating_binary_operator_method?Boolean

Checks whether the method is a nonmutating binary operator method.

Returns:

  • (Boolean)

    whether the method is a nonmutating binary operator method


93
94
95
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 93

def nonmutating_binary_operator_method?
  NONMUTATING_BINARY_OPERATOR_METHODS.include?(method_name)
end

#nonmutating_hash_method?Boolean

Checks whether the method is a nonmutating Hash method.

Returns:

  • (Boolean)

    whether the method is a nonmutating Hash method


121
122
123
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 121

def nonmutating_hash_method?
  NONMUTATING_HASH_METHODS.include?(method_name)
end

#nonmutating_operator_method?Boolean

Checks whether the method is a nonmutating operator method.

Returns:

  • (Boolean)

    whether the method is a nonmutating operator method


107
108
109
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 107

def nonmutating_operator_method?
  NONMUTATING_OPERATOR_METHODS.include?(method_name)
end

#nonmutating_string_method?Boolean

Checks whether the method is a nonmutating String method.

Returns:

  • (Boolean)

    whether the method is a nonmutating String method


128
129
130
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 128

def nonmutating_string_method?
  NONMUTATING_STRING_METHODS.include?(method_name)
end

#nonmutating_unary_operator_method?Boolean

Checks whether the method is a nonmutating unary operator method.

Returns:

  • (Boolean)

    whether the method is a nonmutating unary operator method


100
101
102
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 100

def nonmutating_unary_operator_method?
  NONMUTATING_UNARY_OPERATOR_METHODS.include?(method_name)
end

#operator_method?Boolean

Checks whether the method is an operator method.

Returns:

  • (Boolean)

    whether the method is an operator


86
87
88
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 86

def operator_method?
  OPERATOR_METHODS.include?(method_name)
end

#predicate_method?Boolean

Checks whether the method is a predicate method.

Returns:

  • (Boolean)

    whether the method is a predicate method


164
165
166
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 164

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

#prefix_bang?Boolean

Checks whether this is a prefix bang method, e.g. `!foo`.

Returns:

  • (Boolean)

    whether this method is a prefix bang


214
215
216
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 214

def prefix_bang?
  negation_method? && loc.selector.is?('!')
end

#prefix_not?Boolean

Checks whether this is a prefix not method, e.g. `not foo`.

Returns:

  • (Boolean)

    whether this method is a prefix not


207
208
209
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 207

def prefix_not?
  negation_method? && loc.selector.is?('not')
end

#self_receiver?Boolean

Checks whether the explicit receiver of this node is `self`.

Returns:

  • (Boolean)

    whether the receiver of this node is `self`


186
187
188
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 186

def self_receiver?
  receiver&.self_type?
end