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.



377
378
379
380
381
382
383
384
385
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 377

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



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

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



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

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

#forwarded_block_argObject



399
400
401
402
403
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 399

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



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

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

#forwarded_kwrest_argObject



393
394
395
396
397
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 393

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



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

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



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

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