LeakyConstantDeclaration - allow definitions on example group#1798
LeakyConstantDeclaration - allow definitions on example group#1798pirj merged 1 commit intorubocop:masterfrom
Conversation
51f9e38 to
b11854b
Compare
|
Sorry, I didn’t read the article, but the description of this is misleading. I’m fine to accept this if we change the scope to ignore all const definitions with explicitly specified owner. |
|
The problem with the approach described in the article is that examples may litter class, and those changes would persist between examples. The cop was initially introduced to avoid side effects and surprises. I’d love it to keep what it’s doing. https://fili.pp.ru/leaky-constants.html I’m more inclined to close the PR. WDYT @rubocop/rubocop-rspec? |
This is true of any class defined in any location. The issue flagged by the cop is that it's quite easy to define classes in the global namespace without realizing that is happening. Using Allowing any explicitly specified namespace would make sense to me - even |
let(:klass) { Class.new { } } won’t suffer from this.
Agree. |
9b897fb to
cdbfcce
Compare
|
@pirj I have finally returned to this one and updated it to allow all constants defined with explicit namespaces. Do you think this is a reasonable balance between avoiding the global namespace footgun, and providing an escape hatch for code authors? I'm not sure how to resolve the code coverage failure - I think that I have tests for everything |
| it 'ignores classes defined explicitly in the global namespace' do | ||
| expect_no_offenses(<<~RUBY) | ||
| describe SomeClass do | ||
| ::CONSTANT = "Accessible as ::CONSTANT".freeze |
There was a problem hiding this comment.
Also accessible as CONSTANT aka “I know what I am doing”.
pirj
left a comment
There was a problem hiding this comment.
Looks reasonable, thanks for bringing this up.
What’s in coverage/index.html if you run specs locally? |
It’s the subtle “not covering the implicit Since the conditional covers the only 3 types of nodes that can reach this method, I think you should add the following lines to the method: else
# :nocov:
:noop
# :nocov: |
6def7ff to
ce46ba9
Compare
|
@pirj is there anything else I can do to help this r get merged and released? |
|
Could you please squash the commits into one. Otherwise, I think it’s ready to merge. |
Constant, classes, and modules that are defined on explicit namespaces do not "leak" into the global namespace, because the author is making their intention clear. This is even true when when explicitly using `::`. While doing this may not be a particularly good practice, it should ideally not result in an offense. The practice is described here: https://makandracards.com/makandra/47189-rspec-how-to-define-classes-for-specs#section-1-defining-the-constant-on-the-example-class
cb8838e to
568cd20
Compare
|
@bquorning done, thanks! |
Constant, classes, and modules that are defined on the example group do
not leak into the global namespace. While doing this may not be
a particularly good practice, it should ideally not result in
an offense.
The practice is described here: https://makandracards.com/makandra/47189-rspec-how-to-define-classes-for-specs#section-1-defining-the-constant-on-the-example-class
Before submitting the PR make sure the following are checked:
master(if not - rebase it).CHANGELOG.mdif the new code introduces user-observable changes.bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml.Enabled: pendinginconfig/default.yml.Enabled: truein.rubocop.yml.VersionAdded: "<<next>>"indefault/config.yml.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"inconfig/default.yml.