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

Inherits:
Object
  • Object
show all
Extended by:
Macros
Defined in:
lib/rubocop/cop/style/arguments_forwarding.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(def_node, send_node, referenced_lvars, forwardable_args, **config) ⇒ SendNodeClassifier

Returns a new instance of SendNodeClassifier.



403
404
405
406
407
408
409
410
411
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 403

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 Method Details

#classificationObject



431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 431

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

#def_all_anonymous_args?(node) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 387

def_node_matcher :def_all_anonymous_args?, <<~PATTERN
  (
    def _
    (args ... (restarg) (kwrestarg) (blockarg nil?))
    _
  )
PATTERN

#extract_forwarded_kwrest_arg(node, kwrest_name) ⇒ Object



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

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

#forwarded_block_argObject



425
426
427
428
429
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 425

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) ⇒ Object



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

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

#forwarded_kwrest_argObject



419
420
421
422
423
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 419

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_argObject



413
414
415
416
417
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 413

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) ⇒ Object



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

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

#send_all_anonymous_args?(node) ⇒ Object



396
397
398
399
400
401
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 396

def_node_matcher :send_all_anonymous_args?, <<~PATTERN
  (
    send _ _
    ... (forwarded_restarg) (hash (forwarded_kwrestarg)) (block_pass nil?)
  )
PATTERN