@@ -8,8 +8,10 @@ module RSpec
88 # If the first argument of describe is a class, the class is exposed to
99 # each example via described_class.
1010 #
11- # This cop can be configured using the `EnforcedStyle` and `SkipBlocks`
12- # options.
11+ # This cop can be configured using the `EnforcedStyle`, `SkipBlocks`
12+ # and `OnlyStaticConstants` options.
13+ # `OnlyStaticConstants` is only relevant when `EnforcedStyle` is
14+ # `described_class`.
1315 #
1416 # @example `EnforcedStyle: described_class` (default)
1517 # # bad
@@ -22,6 +24,18 @@ module RSpec
2224 # subject { described_class.do_something }
2325 # end
2426 #
27+ # @example `OnlyStaticConstants: true` (default)
28+ # # good
29+ # describe MyClass do
30+ # subject { MyClass::CONSTANT }
31+ # end
32+ #
33+ # @example `OnlyStaticConstants: false`
34+ # # bad
35+ # describe MyClass do
36+ # subject { MyClass::CONSTANT }
37+ # end
38+ #
2539 # @example `EnforcedStyle: explicit`
2640 # # bad
2741 # describe MyClass do
@@ -54,7 +68,7 @@ module RSpec
5468 # end
5569 # end
5670 #
57- class DescribedClass < Base
71+ class DescribedClass < Base # rubocop:disable Metrics/ClassLength
5872 extend AutoCorrector
5973 include ConfigurableEnforcedStyle
6074 include Namespace
@@ -112,14 +126,17 @@ def autocorrect(corrector, match)
112126
113127 def find_usage ( node , &block )
114128 yield ( node ) if offensive? ( node )
115-
116- return if scope_change? ( node )
129+ return if scope_change? ( node ) || allowed? ( node )
117130
118131 node . each_child_node do |child |
119132 find_usage ( child , &block )
120133 end
121134 end
122135
136+ def allowed? ( node )
137+ node . const_type? && only_static_constants?
138+ end
139+
123140 def message ( offense )
124141 if style == :described_class
125142 format ( MSG , replacement : DESCRIBED_CLASS , src : offense )
@@ -139,6 +156,10 @@ def skippable_block?(node)
139156 node . block_type? && !rspec_block? ( node ) && cop_config [ 'SkipBlocks' ]
140157 end
141158
159+ def only_static_constants?
160+ cop_config . fetch ( 'OnlyStaticConstants' , true )
161+ end
162+
142163 def offensive? ( node )
143164 if style == :described_class
144165 offensive_described_class? ( node )
0 commit comments