rubocop-vicenzo 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bcae934be9ffca6f86007a710cdd35a9c610a7d5610b5c83960b12905c1a25b
4
- data.tar.gz: a2b2cb6db0f9d177099b845caf9de7b87d71381efbc37041ca2f01f7c1f62e8b
3
+ metadata.gz: fdd24a07de899f160e3348295372bec2487ff29d33a9cd6db71478eec020bc8b
4
+ data.tar.gz: b360bb5a873d11650ebb79bd8c9ce0c2b7d4677f6d8d88bff5aa237298414295
5
5
  SHA512:
6
- metadata.gz: 3568dc18ce54dc425af8fad2e06f0d2fb23e52c190c886a8781d879f4f46f0120dd949eab9ead4f05510b280a97c2d7513a01f95264f8a8c9c9facf3fa03d09b
7
- data.tar.gz: 93c229f5feb3bfc0b5ea551b57afe5910e48f94d3e44f9da82fc054297d7499936e22f1ff84a7e5f45a2c9b1464b8204f6d38af5f99cdb625d8cf2b51bf99c60
6
+ metadata.gz: d7328c0cd9d8b76a2c59abbc5ce1a10d1e809c11321c134506733f8fe6b00084cb6eb466b241b7d2fd7e46badf2d8de672e6a1b492f25c555e42cc6cd88f94dc
7
+ data.tar.gz: 0c1e78145f6cf3c45b23a2d9e49ed418ebad1fdd57da123ec27683a6e16eb10d052004b85d3fe7adfe1e3982de986cbe923333da42fe4c4dba216ebbf3f1d5d3
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.11.0"
2
+ ".": "0.12.0"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.12.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.11.0...v0.12.0) (2026-12-02)
4
+
5
+
6
+ ### Features
7
+
8
+ * add Vicenzo/RSpec/LambdaPremise cop ([#61](https://github.com/bvicenzo/rubocop-vicenzo/pull/61)) ([87a11c0](https://github.com/bvicenzo/rubocop-vicenzo/commit/87a11c00f87c2bd299898952293987369e64194d))
9
+ * add Vicenzo/RSpec/SharedPremises cop ([#57](https://github.com/bvicenzo/rubocop-vicenzo/pull/57)) ([f6610f8](https://github.com/bvicenzo/rubocop-vicenzo/commit/f6610f88d66ad6d870802e55b2dad829f87fcbd8))
10
+
3
11
  ## [0.11.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.10.0...v0.11.0) (2026-11-18)
4
12
 
5
13
 
data/README.md CHANGED
@@ -11,7 +11,7 @@ Add it to the `development`/`test` group of your `Gemfile`:
11
11
  <!-- x-release-please-start-version -->
12
12
  ```ruby
13
13
  group :development, :test do
14
- gem 'rubocop-vicenzo', '~> 0.11.0', require: false
14
+ gem 'rubocop-vicenzo', '~> 0.12.0', require: false
15
15
  end
16
16
  ```
17
17
  <!-- x-release-please-end-version -->
data/config/default.yml CHANGED
@@ -88,6 +88,17 @@ Vicenzo/RSpec/IterationInsideExample:
88
88
  Severity: warning
89
89
  VersionAdded: '0.4.0'
90
90
 
91
+ Vicenzo/RSpec/LambdaPremise:
92
+ Description: 'Do not store a callable in a premise and call it from the spec: a premise declares a value.'
93
+ Enabled: true
94
+ Severity: warning
95
+ Include:
96
+ - '**/spec/**/*_spec.rb'
97
+ Exclude:
98
+ - '**/spec/support/**/*'
99
+ - '**/spec/factories/**/*'
100
+ VersionAdded: '0.12.0'
101
+
91
102
  Vicenzo/RSpec/MutatedPremises:
92
103
  Description: 'Do not mutate the premises (let, let_it_be, subject) an example starts from.'
93
104
  Enabled: true
@@ -162,6 +173,15 @@ Vicenzo/RSpec/ScenarioDescribe:
162
173
  - '**/spec/factories/**/*'
163
174
  VersionAdded: '0.11.0'
164
175
 
176
+ Vicenzo/RSpec/SharedPremises:
177
+ Description: 'Do not share premises through a shared context; declare them in the scenario that reads them, and share only complete behaviour through shared examples.'
178
+ Enabled: true
179
+ Severity: warning
180
+ AllowedContexts: []
181
+ Include:
182
+ - '**/spec/**/*.rb'
183
+ VersionAdded: '0.12.0'
184
+
165
185
  Vicenzo/RSpec/SubjectDefinedAsLet:
166
186
  Description: 'A let the expectations assert on, in a group declaring no subject, must be declared as the subject.'
167
187
  Enabled: true
@@ -0,0 +1,236 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'mixin/premise_tracking'
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Vicenzo
8
+ module RSpec
9
+ # Do not store a callable in a premise and call it from the spec.
10
+ #
11
+ # A premise (`let`, `let!`, `let_it_be`, `subject`) declares the value an example starts from. A premise whose
12
+ # value is a lambda or a proc the spec itself calls is not a value: it is a method dressed as a premise.
13
+ #
14
+ # 1. It takes arguments, so the declaration does not say which records exist; the reader has to find every
15
+ # call site and replay it.
16
+ # 2. It hides creation behind an indirection exactly like a helper method would - the helper
17
+ # `Vicenzo/RSpec/LeakyDefinition` already keeps out of spec files.
18
+ # 3. It is memoized, so the callable is shared, which invites state captured in its closure.
19
+ #
20
+ # There is always a static alternative, and the one to reach for depends on what the callable does. Replacing
21
+ # it with a `def` in the spec, a `before` that stores it, or a hash of builders only moves the problem.
22
+ #
23
+ # Only callables written as a literal in the premise count - `-> {}`, `lambda {}`, `proc {}`, `Proc.new {}`,
24
+ # or a hash or array holding nothing but those - and only when the spec invokes them (`call`, `.()`, `[]`,
25
+ # `yield`, after a `curry` or after reading one element of the collection). A callable handed to the unit
26
+ # under test is data, like a string or a number: the spec never calls it, production code does. A callable
27
+ # that a method returns is the unit under test itself, and calling it is the test. A block that only hands its
28
+ # parameters over to the callable (`middleware.call(job) { execution.call }`) is the same as passing it with
29
+ # `&`, so the object receiving the block is the one calling it.
30
+ #
31
+ # @example
32
+ # # bad - the records differ by a parameter
33
+ #
34
+ # let(:warehouse_at) do
35
+ # ->(address_trait, name) { create(:warehouse, name:, address: create(:address, address_trait)) }
36
+ # end
37
+ # let(:stops) { [build(:stop, warehouse: warehouse_at.(:downtown, 'Central'))] }
38
+ #
39
+ # # good - the variation lives in the factory, the record is created where it is read
40
+ #
41
+ # let(:stops) { [build(:stop, warehouse: create(:warehouse, name: 'Central', at: :downtown))] }
42
+ #
43
+ # @example
44
+ # # bad - one premise in different flavours
45
+ #
46
+ # let(:order_builder) { ->(status:) { create(:order, status:) } }
47
+ # let(:shipped_order) { order_builder.call(status: :shipped) }
48
+ #
49
+ # # good - one trait per flavour, or one `let` per context
50
+ #
51
+ # let(:shipped_order) { create(:order, :shipped) }
52
+ #
53
+ # @example
54
+ # # bad - measuring something during the example
55
+ #
56
+ # let(:count_queries) { -> { QueryCounter.count { get orders_path } } }
57
+ #
58
+ # it { expect(count_queries.call).to be <= 3 }
59
+ #
60
+ # # good - a helper in `spec/support`, read explicitly by the example
61
+ #
62
+ # it { expect(query_count { get orders_path }).to be <= 3 }
63
+ #
64
+ # @example
65
+ # # bad - the subject defers a call to the unit under test
66
+ #
67
+ # subject(:query) { -> { described_class.call } }
68
+ #
69
+ # it { expect { query.call }.not_to raise_error }
70
+ #
71
+ # # good - the unit is the subject, and the example calls it
72
+ #
73
+ # subject(:query) { described_class.new }
74
+ #
75
+ # it { expect { query.call }.not_to raise_error }
76
+ #
77
+ # @example
78
+ # # good - the callable is handed to the unit under test, which is the one calling it
79
+ #
80
+ # subject(:report) { Report.new(rows:, formatter:) }
81
+ #
82
+ # let(:formatter) { ->(value) { value.to_s.upcase } }
83
+ class LambdaPremise < RuboCop::Cop::RSpec::Base
84
+ include PremiseTracking
85
+
86
+ MSG = '`%<name>s` is a method dressed as a premise: a premise declares a value, it does not build one on ' \
87
+ 'demand. If the records it builds differ only by a parameter, move that variation into the factory ' \
88
+ '(a trait for the shared state, a transient for the parameter) and create each record where it is ' \
89
+ 'read. If it builds one premise in different flavours, declare one `let` per context or one trait ' \
90
+ 'per flavour. If it measures or computes something during the example, extract a helper to ' \
91
+ '`spec/support`. If it defers a call to the unit under test, make the unit the subject and call ' \
92
+ 'the method in the example. Do not replace it with a `def` in the spec, a `before` that stores it, ' \
93
+ 'or a hash of builders.'
94
+
95
+ # `.()` is `call` in the AST.
96
+ INVOKERS = %i[call [] yield].freeze
97
+ CURRY = Set[:curry].freeze
98
+ ELEMENT_READERS = Set[:[], :fetch, :dig, :first, :last].freeze
99
+
100
+ # @!method callable_literal?(node)
101
+ def_node_matcher :callable_literal?, <<~PATTERN
102
+ (any_block
103
+ {
104
+ (lambda)
105
+ (send nil? {:lambda :proc})
106
+ (send (const {nil? cbase} :Proc) :new)
107
+ }
108
+ ...
109
+ )
110
+ PATTERN
111
+
112
+ # @!method callable_collection?(node)
113
+ # A hash or an array holding nothing but callable literals - at least one, and no splat.
114
+ def_node_matcher :callable_collection?, <<~PATTERN
115
+ {
116
+ (hash (pair _ #callable_literal?)+)
117
+ (array #callable_literal?+)
118
+ }
119
+ PATTERN
120
+
121
+ # @!method premise_reference?(node, name)
122
+ # A bare call to the premise. A local variable or a block parameter with the same name is a `lvar`, so it
123
+ # never passes for the premise.
124
+ def_node_matcher :premise_reference?, <<~PATTERN
125
+ (send nil? %1)
126
+ PATTERN
127
+
128
+ # @!method read_from_premise?(node, name, methods)
129
+ # A call on the premise that hands back something to invoke: `name.curry`, `name[:key]`, `name.first`.
130
+ def_node_matcher :read_from_premise?, <<~PATTERN
131
+ (call (send nil? %1) %2 ...)
132
+ PATTERN
133
+
134
+ def on_block(node)
135
+ return unless spec_group?(node)
136
+
137
+ each_child_block(node) do |child|
138
+ check_premise(child, node) if premise?(child)
139
+ end
140
+ end
141
+
142
+ alias on_numblock on_block
143
+
144
+ private
145
+
146
+ def check_premise(premise, group)
147
+ name = declared_name(premise)
148
+ kind = callable_kind(value_of(premise))
149
+
150
+ return if name.nil? || kind.nil?
151
+ return unless invoked?(name, kind, visible_scopes(group))
152
+
153
+ add_offense(premise.send_node, message: format(MSG, name:))
154
+ end
155
+
156
+ # An anonymous `subject!` is still read as `subject`.
157
+ def declared_name(premise)
158
+ name = premise_name(premise)&.to_sym
159
+
160
+ name == :subject! ? :subject : name
161
+ end
162
+
163
+ # The value is the last expression of the premise body.
164
+ def value_of(premise)
165
+ body = premise.body
166
+
167
+ body&.begin_type? ? body.children.last : body
168
+ end
169
+
170
+ def callable_kind(value)
171
+ return :callable if callable_literal?(value)
172
+
173
+ :collection if callable_collection?(value)
174
+ end
175
+
176
+ # Where the premise can be invoked from: the group declaring it, with everything below it, and the hooks
177
+ # and premises of its ancestors, which run in its scope - the "abstract let" shape. The ancestors' examples
178
+ # do not: they never see a premise declared below them.
179
+ def visible_scopes(group)
180
+ ancestors = group.each_ancestor(:any_block).select { |ancestor| spec_group?(ancestor) }
181
+
182
+ [group, *ancestors.flat_map { |ancestor| inherited_definitions(ancestor) }]
183
+ end
184
+
185
+ def inherited_definitions(group)
186
+ definitions = []
187
+
188
+ each_child_block(group) { |child| definitions << child if hook?(child) || premise?(child) }
189
+
190
+ definitions
191
+ end
192
+
193
+ def invoked?(name, kind, scopes)
194
+ scopes.any? do |scope|
195
+ scope.each_node(:call).any? { |node| invocation?(node, name, kind) && !forwarded_block?(node) }
196
+ end
197
+ end
198
+
199
+ def invocation?(node, name, kind)
200
+ return false unless INVOKERS.include?(node.method_name)
201
+
202
+ receiver = node.receiver
203
+
204
+ if kind == :callable
205
+ premise_reference?(receiver, name) || read_from_premise?(receiver, name, CURRY)
206
+ else
207
+ read_from_premise?(receiver, name, ELEMENT_READERS)
208
+ end
209
+ end
210
+
211
+ # `middleware.call(job) { execution.call }` and `rows.map { |row| builder.(row) }` hand the callable to
212
+ # another object, as `&execution` and `&builder` would: the spec does not call it, the object does. A block
213
+ # handed to a receiverless call - a helper such as `query_count { report.call }`, or `expect { ... }` -
214
+ # still has the spec calling it.
215
+ def forwarded_block?(invocation)
216
+ block = invocation.parent
217
+
218
+ return false unless block.block_type? && block.body.equal?(invocation) && block.receiver
219
+
220
+ forwards_parameters?(block, invocation.arguments)
221
+ end
222
+
223
+ def forwards_parameters?(block, arguments)
224
+ parameters = block.arguments.children
225
+
226
+ return false unless parameters.all?(&:arg_type?) && parameters.size == arguments.size
227
+
228
+ parameters.zip(arguments).all? do |parameter, argument|
229
+ argument.lvar_type? && argument.children.first == parameter.name
230
+ end
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module RSpec
7
+ # Do not share premises through a shared context. Declare them in the scenario that reads them, and
8
+ # share only a complete behaviour, the same for every group, through shared examples.
9
+ #
10
+ # A shared context starts as a handful of premises a few groups happen to need. Then one group
11
+ # needs a premise slightly different, and instead of declaring its own, it bends the shared one: a
12
+ # parameter, an `if`, a `case`, a `let` overridden from the outside. Every bend is a scenario
13
+ # hidden inside the shared context, coupling specs that have nothing to do with each other, and
14
+ # the reader of a group can no longer see what the group starts from without opening another file
15
+ # and replaying its branches.
16
+ #
17
+ # Shared examples are a different thing when they assert a whole behaviour that holds the same for
18
+ # every group including them, such as being timestamped or auditable. That is sharing a contract,
19
+ # not premises.
20
+ #
21
+ # Tests do not have to be DRY; the code does. Repeating a premise in each scenario that reads it
22
+ # keeps every scenario whole and free to change on its own.
23
+ #
24
+ # Both the definition (`shared_context`) and every inclusion (`include_context`) are reported:
25
+ # the definition is the one to remove, and each inclusion is where its premises have to land.
26
+ #
27
+ # @example
28
+ # # bad - premises shared across scenarios
29
+ # RSpec.shared_context 'with an active subscription' do
30
+ # let(:plan) { create(:plan, :monthly) }
31
+ # let(:subscription) { create(:subscription, plan:, status: :active) }
32
+ # end
33
+ #
34
+ # RSpec.describe Invoice do
35
+ # describe '#total' do
36
+ # context 'when the subscription is active' do
37
+ # include_context 'with an active subscription'
38
+ #
39
+ # it { expect(invoice.total).to eq(100) }
40
+ # end
41
+ # end
42
+ # end
43
+ #
44
+ # # good - the scenario declares what it starts from
45
+ # RSpec.describe Invoice do
46
+ # describe '#total' do
47
+ # context 'when the subscription is active' do
48
+ # let(:subscription) { create(:subscription, plan: create(:plan, :monthly), status: :active) }
49
+ #
50
+ # it { expect(invoice.total).to eq(100) }
51
+ # end
52
+ # end
53
+ # end
54
+ #
55
+ # # good - a complete behaviour, the same for every group including it
56
+ # RSpec.shared_examples 'a timestamped record' do
57
+ # it { is_expected.to respond_to(:created_at) }
58
+ # it { is_expected.to respond_to(:updated_at) }
59
+ # end
60
+ #
61
+ # @example AllowedContexts: ['with a frozen clock']
62
+ # # good - an infrastructure context allowed by name
63
+ # RSpec.shared_context 'with a frozen clock' do
64
+ # around { |example| travel_to(Time.zone.local(2026, 1, 1)) { example.run } }
65
+ # end
66
+ #
67
+ # RSpec.describe Invoice do
68
+ # include_context 'with a frozen clock'
69
+ # end
70
+ class SharedPremises < RuboCop::Cop::RSpec::Base
71
+ DEFINITION_MSG = 'Shared context `%<name>s` shares premises across scenarios. Declare them in each ' \
72
+ 'scenario that reads them; share only a complete behaviour, the same for every ' \
73
+ 'group, through shared examples. Tests do not have to be DRY; the code does.'
74
+
75
+ INCLUSION_MSG = '`%<method>s` pulls premises from shared context `%<name>s`. Declare them in this ' \
76
+ 'scenario, so it reads whole and changes without branching the shared context. ' \
77
+ 'Tests do not have to be DRY; the code does.'
78
+
79
+ # @!method shared_context_definition(node)
80
+ def_node_matcher :shared_context_definition, <<~PATTERN
81
+ (any_block $(send #rspec? #SharedGroups.context $_ ...) ...)
82
+ PATTERN
83
+
84
+ # @!method shared_context_inclusion(node)
85
+ def_node_matcher :shared_context_inclusion, <<~PATTERN
86
+ (send nil? #Includes.context $_ ...)
87
+ PATTERN
88
+
89
+ def on_block(node)
90
+ shared_context_definition(node) do |send_node, name|
91
+ next if allowed_context?(name)
92
+
93
+ add_offense(send_node, message: format(DEFINITION_MSG, name: context_name(name)))
94
+ end
95
+ end
96
+ alias on_numblock on_block
97
+
98
+ def on_send(node)
99
+ shared_context_inclusion(node) do |name|
100
+ next if allowed_context?(name)
101
+
102
+ add_offense(node, message: format(INCLUSION_MSG, method: node.method_name, name: context_name(name)))
103
+ end
104
+ end
105
+ alias on_csend on_send
106
+
107
+ private
108
+
109
+ def allowed_context?(name) = allowed_contexts.include?(context_name(name))
110
+
111
+ def context_name(name) = name.respond_to?(:value) ? name.value.to_s : name.source
112
+
113
+ def allowed_contexts
114
+ cop_config.fetch('AllowedContexts', [])
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -7,6 +7,7 @@ require_relative 'vicenzo/rspec/describe_inside_context'
7
7
  require_relative 'vicenzo/rspec/dynamic_example_generation'
8
8
  require_relative 'vicenzo/rspec/inconsistent_sibling_structure'
9
9
  require_relative 'vicenzo/rspec/iteration_inside_example'
10
+ require_relative 'vicenzo/rspec/lambda_premise'
10
11
  require_relative 'vicenzo/rspec/let_is_method_result'
11
12
  require_relative 'vicenzo/rspec/mutated_premises'
12
13
  require_relative 'vicenzo/rspec/nested_context_improper_start'
@@ -15,6 +16,7 @@ require_relative 'vicenzo/rspec/nested_subject_redefinition'
15
16
  require_relative 'vicenzo/rspec/leaky_definition'
16
17
  require_relative 'vicenzo/rspec/let_aliasing_subject'
17
18
  require_relative 'vicenzo/rspec/scenario_describe'
19
+ require_relative 'vicenzo/rspec/shared_premises'
18
20
  require_relative 'vicenzo/rspec/subject_defined_as_let'
19
21
  require_relative 'vicenzo/rspec/subject_is_method_result'
20
22
  require_relative 'vicenzo/rspec/top_level_context'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Vicenzo
5
- VERSION = '0.11.0'
5
+ VERSION = '0.12.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-vicenzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Vicenzo
@@ -85,6 +85,7 @@ files:
85
85
  - lib/rubocop/cop/vicenzo/rspec/dynamic_example_generation.rb
86
86
  - lib/rubocop/cop/vicenzo/rspec/inconsistent_sibling_structure.rb
87
87
  - lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb
88
+ - lib/rubocop/cop/vicenzo/rspec/lambda_premise.rb
88
89
  - lib/rubocop/cop/vicenzo/rspec/leaky_definition.rb
89
90
  - lib/rubocop/cop/vicenzo/rspec/let_aliasing_subject.rb
90
91
  - lib/rubocop/cop/vicenzo/rspec/let_is_method_result.rb
@@ -99,6 +100,7 @@ files:
99
100
  - lib/rubocop/cop/vicenzo/rspec/nested_subject_redefinition.rb
100
101
  - lib/rubocop/cop/vicenzo/rspec/returned_chain.rb
101
102
  - lib/rubocop/cop/vicenzo/rspec/scenario_describe.rb
103
+ - lib/rubocop/cop/vicenzo/rspec/shared_premises.rb
102
104
  - lib/rubocop/cop/vicenzo/rspec/subject_defined_as_let.rb
103
105
  - lib/rubocop/cop/vicenzo/rspec/subject_is_method_result.rb
104
106
  - lib/rubocop/cop/vicenzo/rspec/top_level_context.rb