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.



348
349
350
351
352
353
354
355
356
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 348

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



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

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



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

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

#forwarded_block_argObject



370
371
372
373
374
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 370

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



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

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

#forwarded_kwrest_argObject



364
365
366
367
368
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 364

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



358
359
360
361
362
# File 'lib/rubocop/cop/style/arguments_forwarding.rb', line 358

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



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

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