Class: RuboCop::Cop::RSpec::UnusedLet::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/unused_let/scope.rb,
sig/rubocop/cop/rspec/unused_let/scope.rbs

Overview

A mutable record of one example or shared group: the definitions it makes, the references it makes (kept apart by whether they sit in a helper body or in an example), whether an example runs in it, whether it pulls in a shared example group, and which of its definitions have been resolved to a reference.

Defined Under Namespace

Classes: Definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, kind:, type: nil, tags: [], carries_examples: false) ⇒ Scope

Returns a new instance of Scope.

RBS:

  • node: RuboCop::AST::Node

  • kind: kind

  • type: Symbol?

  • tags: Array[Symbol]

  • carries_examples: bool

Parameters:

  • node: (RuboCop::AST::Node)
  • kind: (kind)
  • type: (Symbol, nil) (defaults to: nil)
  • tags: (Array[Symbol]) (defaults to: [])
  • carries_examples: (Boolean) (defaults to: false)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 63

def initialize(node:, kind:, type: nil, tags: [], carries_examples: false) #: void
  @node = node
  @kind = kind
  @defs = []
  @refs = Set.new
  @refs_in_example = Set.new
  @inclusion = false
  @type = type
  @tags = tags
  @resolved = Set.new
  @carries_examples = carries_examples
end

Instance Attribute Details

#carries_examplesBoolean (readonly)

Signature:

  • bool

Returns:

  • (Boolean)


123
124
125
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 123

def carries_examples
  @carries_examples
end

#defsArray[Definition] (readonly)

Signature:

  • Array[Definition] -- one per let, subjectordef helper

Returns:



50
51
52
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 50

def defs
  @defs
end

#inclusionBoolean

Signature:

  • bool -- whether this group pulls in a shared example group

Returns:

  • (Boolean)


53
54
55
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 53

def inclusion
  @inclusion
end

#kindkind (readonly)

Signature:

  • kind

Returns:



49
50
51
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 49

def kind
  @kind
end

#nodeRuboCop::AST::Node (readonly)

Signature:

  • RuboCop::AST::Node

Returns:

  • (RuboCop::AST::Node)


48
49
50
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 48

def node
  @node
end

#refsSet[Symbol] (readonly)

Signature:

  • Set[Symbol] -- names referenced in this group's helper bodies

Returns:

  • (Set[Symbol])


51
52
53
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 51

def refs
  @refs
end

#refs_in_exampleSet[Symbol] (readonly)

Signature:

  • Set[Symbol] -- names referenced outside this group's helper bodies

Returns:

  • (Set[Symbol])


52
53
54
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 52

def refs_in_example
  @refs_in_example
end

#resolvedSet[Symbol] (readonly)

Signature:

  • Set[Symbol] -- names of this group's definitions resolved to a reference

Returns:

  • (Set[Symbol])


56
57
58
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 56

def resolved
  @resolved
end

#tagsArray[Symbol] (readonly)

Signature:

  • Array[Symbol] -- the bare symbols this group carries as metadata, e.g. :config``

Returns:

  • (Array[Symbol])


55
56
57
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 55

def tags
  @tags
end

#typeSymbol? (readonly)

Signature:

  • Symbol? -- this group's type:, explicit or inferred from the spec's location

Returns:

  • (Symbol, nil)


54
55
56
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 54

def type
  @type
end

Instance Method Details

#add_definition(helper, name, def_node, alias_name: nil) ⇒ void

This method returns an undefined value.

RBS:

  • helper: Symbol

  • name: Symbol?

  • def_node: RuboCop::AST::Node

  • alias_name: Symbol?

Parameters:

  • helper (Symbol)
  • name (Symbol, nil)
  • def_node (RuboCop::AST::Node)
  • alias_name: (Symbol, nil) (defaults to: nil)


80
81
82
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 80

def add_definition(helper, name, def_node, alias_name: nil) #: void
  defs << Definition.new(helper, name, [name, alias_name].compact.uniq, def_node)
end

#add_reference(name) ⇒ void

This method returns an undefined value.

RBS:

  • name: Symbol

Parameters:

  • name (Symbol)


85
86
87
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 85

def add_reference(name) #: void
  refs << name
end

#add_reference_in_example(name) ⇒ void

This method returns an undefined value.

RBS:

  • name: Symbol

Parameters:

  • name (Symbol)


90
91
92
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 90

def add_reference_in_example(name) #: void
  refs_in_example << name
end

#carries_examples?Boolean

Whether an example runs in this group or in an example group nested in it.

Returns:

  • (Boolean)


109
110
111
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 109

def carries_examples? #: bool
  carries_examples
end

#defined_namesArray[Symbol]

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])


113
114
115
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 113

def defined_names #: Array[Symbol]
  defs.flat_map(&:names).uniq
end

#mark_inclusionvoid

This method returns an undefined value.

Signature:

  • void



94
95
96
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 94

def mark_inclusion #: void
  self.inclusion = true
end

#mark_referenced(name) ⇒ void

This method returns an undefined value.

RBS:

  • name: Symbol

Parameters:

  • name (Symbol)


99
100
101
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 99

def mark_referenced(name) #: void
  resolved << name
end

#shared?Boolean

Signature:

  • bool

Returns:

  • (Boolean)


103
104
105
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 103

def shared? #: bool
  kind == :shared
end

#unreferenced_defsArray[Definition]

Signature:

  • Array[Definition]

Returns:



117
118
119
# File 'lib/rubocop/cop/rspec/unused_let/scope.rb', line 117

def unreferenced_defs #: Array[Definition]
  defs.reject { |definition| definition.names.any? { resolved.include?(_1) } }
end