class RuboCop::Cop::Style::ArgumentsForwarding::SendNodeClassifier
Classifies send nodes for possible rest/kwrest/all (including block) forwarding.
Public Class Methods
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 422 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
Public Instance Methods
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 450 def classification return nil unless forwarded_rest_arg || forwarded_kwrest_arg || forwarded_block_arg if ruby_32_only_anonymous_forwarding? :all_anonymous elsif can_forward_all? :all else :rest_or_kwrest end end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 444 def forwarded_block_arg return nil if referenced_block_arg? arguments.find { |arg| forwarded_block_arg?(arg, @block_arg_name) } end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 438 def forwarded_kwrest_arg return nil if referenced_kwrest_arg? arguments.filter_map { |arg| extract_forwarded_kwrest_arg(arg, @kwrest_arg_name) }.first end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 432 def forwarded_rest_arg return nil if referenced_rest_arg? arguments.find { |arg| forwarded_rest_arg?(arg, @rest_arg_name) } end
Private Instance Methods
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 535 def additional_kwargs? @def_node.arguments.any? { |a| a.type?(:kwarg, :kwoptarg) } end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 531 def additional_kwargs_or_forwarded_kwargs? additional_kwargs? || forward_additional_kwargs? end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 545 def allow_offense_for_no_block? !@config.fetch(:allow_only_rest_arguments) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 516 def any_arg_referenced? referenced_rest_arg? || referenced_kwrest_arg? || referenced_block_arg? end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 500 def arguments @send_node.arguments end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 465 def can_forward_all? return false if any_arg_referenced? return false if ruby_30_or_lower_optarg? return false if ruby_32_or_higher_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
rubocop:disable Metrics/CyclomaticComplexity
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 539 def forward_additional_kwargs? return false unless forwarded_kwrest_arg !forwarded_kwrest_arg.parent.children.one? end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 496 def forwarded_rest_and_kwrest_args forwarded_rest_arg && forwarded_kwrest_arg end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 558 def missing_rest_arg_or_kwrest_arg? (@rest_arg_name && !forwarded_rest_arg) || (@kwrest_arg_name && !forwarded_kwrest_arg) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 549 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
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 524 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
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 492 def offensive_block_forwarding? @block_arg ? forwarded_block_arg : allow_offense_for_no_block? end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 512 def referenced_block_arg? @referenced_lvars.include?(@block_arg_name) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 508 def referenced_kwrest_arg? @referenced_lvars.include?(@kwrest_arg_name) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 504 def referenced_rest_arg? @referenced_lvars.include?(@rest_arg_name) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 477 def ruby_30_or_lower_optarg? target_ruby_version <= 3.0 && @def_node.arguments.any?(&:optarg_type?) end
def foo(a = 41, …) is a syntax error in 3.0.
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 481 def ruby_32_only_anonymous_forwarding? # A block argument and an anonymous block argument are never passed together. return false if @send_node.each_ancestor(:any_block).any? def_all_anonymous_args?(@def_node) && send_all_anonymous_args?(@send_node) end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 488 def ruby_32_or_higher_missing_rest_or_kwest? target_ruby_version >= 3.2 && !forwarded_rest_and_kwrest_args end
Source
# File lib/rubocop/cop/style/arguments_forwarding.rb, line 520 def target_ruby_version @config.fetch(:target_ruby_version) end