Module: RuboCop::Cop::RSpec::UnusedLet::References

Included in:
ScopeBuilder, SharedExampleRegistry
Defined in:
lib/rubocop/cop/rspec/unused_let/references.rb,
sig/rubocop/cop/rspec/unused_let/references.rbs

Overview

The let-visible names a single send node references. Shared by ScopeBuilder and SharedExampleRegistry so both read references the same way.

Constant Summary collapse

DYNAMIC_DISPATCH_METHODS =

Returns:

  • (Object)
%i[
  send public_send __send__ method respond_to?
].freeze
SUBJECT_ALIASES =

Calls that reach the group's subject without naming it: RSpec's one-liner syntax (is_expected, should, should_not, and rspec-collection_matchers' plural are_expected) and rspec-its (its(:size) { ... }, which derives from the subject). Normalizing them here is what lets a subject be resolved by the same machinery as a let, in a shared block as much as in a group.

Returns:

  • (Object)
%i[is_expected are_expected should should_not its].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.references_in(node) ⇒ Object

A bare (nil receiver) call names a let; a dynamic-dispatch call such as send(:foo) names its literal first argument.

RBS:

  • node: RuboCop::AST::Node



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/rspec/unused_let/references.rb', line 29

def references_in(node) #: Array[Symbol]
  return [] unless node.send_type?

  send = node #: untyped
  names = [] #: Array[Symbol]
  if send.receiver.nil?
    names << send.method_name
    names << :subject if SUBJECT_ALIASES.include?(send.method_name)
  end
  if DYNAMIC_DISPATCH_METHODS.include?(send.method_name)
    arg = send.first_argument
    names << arg.value.to_sym if arg&.type?(:sym, :str)
  end
  names
end

Instance Method Details

#self?.references_inArray[Symbol]

A bare (nil receiver) call names a let; a dynamic-dispatch call such as send(:foo) names its literal first argument.

RBS:

  • node: RuboCop::AST::Node

Parameters:

  • node (RuboCop::AST::Node)

Returns:

  • (Array[Symbol])


25
# File 'sig/rubocop/cop/rspec/unused_let/references.rbs', line 25

def self?.references_in: (RuboCop::AST::Node node) -> Array[Symbol]