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`

Constant Summary collapse

ENUMERATOR_METHODS =

rubocop:disable Metrics/ModuleLength

%i[collect collect_concat detect downto each
find find_all find_index inject loop map!
map reduce reject reject! reverse_each select
select! times upto].to_set.freeze
ENUMERABLE_METHODS =
(Enumerable.instance_methods + [:each]).to_set.freeze
OPERATOR_METHODS =
%i[| ^ & <=> == === =~ > >= < <= << >> + - * /
% ** ~ +@ -@ !@ ~@ [] []= ! != !~ `].to_set.freeze
NONMUTATING_BINARY_OPERATOR_METHODS =
%i[* / % + - == === != < > <= >= <=>].to_set.freeze
NONMUTATING_UNARY_OPERATOR_METHODS =
%i[+@ -@ ~ !].to_set.freeze
NONMUTATING_OPERATOR_METHODS =
(NONMUTATING_BINARY_OPERATOR_METHODS +
NONMUTATING_UNARY_OPERATOR_METHODS).freeze
NONMUTATING_ARRAY_METHODS =
%i[
  all? any? assoc at bsearch bsearch_index collect
  combination compact count cycle deconstruct difference
  dig drop drop_while each each_index empty? eql?
  fetch filter find_index first flatten hash
  include? index inspect intersection join
  last length map max min minmax none? one? pack
  permutation product rassoc reject
  repeated_combination repeated_permutation reverse
  reverse_each rindex rotate sample select shuffle
  size slice sort sum take take_while
  to_a to_ary to_h to_s transpose union uniq
  values_at zip |
].to_set.freeze
NONMUTATING_HASH_METHODS =
%i[
  any? assoc compact dig each each_key each_pair
  each_value empty? eql? fetch fetch_values filter
  flatten has_key? has_value? hash include? inspect
  invert key key? keys? length member? merge rassoc
  rehash reject select size slice to_a to_h to_hash
  to_proc to_s transform_keys transform_values value?
  values values_at
].to_set.freeze
NONMUTATING_STRING_METHODS =
%i[
  ascii_only? b bytes bytesize byteslice capitalize
  casecmp casecmp? center chars chomp chop chr codepoints
  count crypt delete delete_prefix delete_suffix
  downcase dump each_byte each_char each_codepoint
  each_grapheme_cluster each_line empty? encode encoding
  end_with? eql? getbyte grapheme_clusters gsub hash
  hex include index inspect intern length lines ljust lstrip
  match match? next oct ord partition reverse rindex rjust
  rpartition rstrip scan scrub size slice squeeze start_with?
  strip sub succ sum swapcase to_a to_c to_f to_i to_r to_s
  to_str to_sym tr tr_s unicode_normalize unicode_normalized?
  unpack unpack1 upcase upto valid_encoding?
].to_set.freeze

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



133
134
135
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 133

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



162
163
164
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 162

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



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

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



126
127
128
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 126

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



184
185
186
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 184

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



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

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



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

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



70
71
72
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 70

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



191
192
193
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 191

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



105
106
107
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 105

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



84
85
86
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 84

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



112
113
114
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 112

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



98
99
100
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 98

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



119
120
121
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 119

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



91
92
93
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 91

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



77
78
79
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 77

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



155
156
157
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 155

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



205
206
207
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 205

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



198
199
200
# File 'lib/rubocop/ast/node/mixin/method_identifier_predicates.rb', line 198

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`



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

def self_receiver?
  receiver&.self_type?
end