123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::ArgumentsForwarding::SendNodeClassifier

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Macros
Inherits: Object
Defined in: lib/rubocop/cop/style/arguments_forwarding.rb

Overview

Classifies send nodes for possible rest/kwrest/all (including block) forwarding.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(def_node, send_node, referenced_lvars, forwardable_args, **config) ⇒ SendNodeClassifier

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 370

def initialize(def_node, send_node, referenced_lvars, forwardable_args, **config)
  @def_node = def_node
  @send_node = send_node
  @referenced_lvars = referenced_lvars
  @rest_arg, @kwrest_arg, @block_arg = *forwardable_args
  @rest_arg_name, @kwrest_arg_name, @block_arg_name =
    *forwardable_args.map { |a| a&.name }
  @config = config
end

Instance Attribute Details

#additional_kwargs?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 466

def additional_kwargs?
  @def_node.arguments.any? { |a| a.kwarg_type? || a.kwoptarg_type? }
end

#additional_kwargs_or_forwarded_kwargs?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 462

def additional_kwargs_or_forwarded_kwargs?
  additional_kwargs? || forward_additional_kwargs?
end

#allow_offense_for_no_block?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 476

def allow_offense_for_no_block?
  !@config.fetch(:allow_only_rest_arguments)
end

#any_arg_referenced?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 447

def any_arg_referenced?
  referenced_rest_arg? || referenced_kwrest_arg? || referenced_block_arg?
end

#can_forward_all?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 410

def can_forward_all?
  return false if any_arg_referenced?
  return false if ruby_32_missing_rest_or_kwest?
  return false unless offensive_block_forwarding?
  return false if additional_kwargs_or_forwarded_kwargs?

  no_additional_args? || (target_ruby_version >= 3.0 && no_post_splat_args?)
end

#forward_additional_kwargs?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 470

def forward_additional_kwargs?
  return false unless forwarded_kwrest_arg

  !forwarded_kwrest_arg.parent.children.one?
end

#missing_rest_arg_or_kwrest_arg?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 489

def missing_rest_arg_or_kwrest_arg?
  (@rest_arg_name && !forwarded_rest_arg) ||
    (@kwrest_arg_name && !forwarded_kwrest_arg)
end

#no_additional_args?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 480

def no_additional_args?
  forwardable_count = [@rest_arg, @kwrest_arg, @block_arg].compact.size

  return false if missing_rest_arg_or_kwrest_arg?

  @def_node.arguments.size == forwardable_count &&
    @send_node.arguments.size == forwardable_count
end

#no_post_splat_args?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 455

def no_post_splat_args?
  return true unless (splat_index = arguments.index(forwarded_rest_arg))

  arg_after_splat = arguments[splat_index + 1]
  [nil, :hash, :block_pass].include?(arg_after_splat&.type)
end

#offensive_block_forwarding?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 423

def offensive_block_forwarding?
  @block_arg ? forwarded_block_arg : allow_offense_for_no_block?
end

#referenced_block_arg?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 443

def referenced_block_arg?
  @referenced_lvars.include?(@block_arg_name)
end

#referenced_kwrest_arg?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 439

def referenced_kwrest_arg?
  @referenced_lvars.include?(@kwrest_arg_name)
end

#referenced_rest_arg?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 435

def referenced_rest_arg?
  @referenced_lvars.include?(@rest_arg_name)
end

#ruby_32_missing_rest_or_kwest?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 419

def ruby_32_missing_rest_or_kwest?
  target_ruby_version >= 3.2 && !forwarded_rest_and_kwrest_args
end

Instance Method Details

#arguments (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 431

def arguments
  @send_node.arguments
end

#classification

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 398

def classification
  return nil unless forwarded_rest_arg || forwarded_kwrest_arg || forwarded_block_arg

  if can_forward_all?
    :all
  else
    :rest_or_kwrest
  end
end

#extract_forwarded_kwrest_arg(node, kwrest_name)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 365

def_node_matcher :extract_forwarded_kwrest_arg, '(hash <$(kwsplat (lvar %1)) ...>)'

#forwarded_block_arg

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 392

def forwarded_block_arg
  return nil if referenced_block_arg?

  arguments.find { |arg| forwarded_block_arg?(arg, @block_arg_name) }
end

#forwarded_block_arg?(node, block_name)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 368

def_node_matcher :forwarded_block_arg?, '(block_pass {(lvar %1) nil?})'

#forwarded_kwrest_arg

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 386

def forwarded_kwrest_arg
  return nil if referenced_kwrest_arg?

  arguments.filter_map { |arg| extract_forwarded_kwrest_arg(arg, @kwrest_arg_name) }.first
end

#forwarded_rest_and_kwrest_args (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 427

def forwarded_rest_and_kwrest_args
  forwarded_rest_arg && forwarded_kwrest_arg
end

#forwarded_rest_arg

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 380

def forwarded_rest_arg
  return nil if referenced_rest_arg?

  arguments.find { |arg| forwarded_rest_arg?(arg, @rest_arg_name) }
end

#forwarded_rest_arg?(node, rest_name)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 362

def_node_matcher :forwarded_rest_arg?, '(splat (lvar %1))'

#target_ruby_version (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 451

def target_ruby_version
  @config.fetch(:target_ruby_version)
end