Skip to content

Commit

Permalink
Add new RSpec/RemoveConst cop
Browse files Browse the repository at this point in the history
  • Loading branch information
swelther committed Dec 4, 2023
1 parent 9d51eb0 commit 775ad8f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ RSpec/ReceiveMessages:
Enabled: true
RSpec/RedundantAround:
Enabled: true
RSpec/RemoveConst:
Enabled: true
RSpec/SkipBlockInsideExample:
Enabled: true
RSpec/SortMetadata:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Add new `RSpec/RemoveConst` cop. ([@swelther])

## 2.25.0 (2023-10-27)

- Add support single quoted string and percent string and heredoc for `RSpec/Rails/HttpStatus`. ([@ydah])
Expand Down Expand Up @@ -909,6 +911,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@smcgivern]: https://github.com/smcgivern
[@splattael]: https://github.com/splattael
[@stephannv]: https://github.com/stephannv
[@swelther]: https://github.com/swelther
[@t3h2mas]: https://github.com/t3h2mas
[@tdeo]: https://github.com/tdeo
[@tejasbubane]: https://github.com/tejasbubane
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,12 @@ RSpec/RedundantAround:
VersionAdded: '2.19'
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround

RSpec/RemoveConst:
Description: Checks that `remove_const` is not used in specs.
Enabled: pending
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RemoveConst

RSpec/RepeatedDescription:
Description: Check for repeated description strings in example groups.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* xref:cops_rspec.adoc#rspecreceivemessages[RSpec/ReceiveMessages]
* xref:cops_rspec.adoc#rspecreceivenever[RSpec/ReceiveNever]
* xref:cops_rspec.adoc#rspecredundantaround[RSpec/RedundantAround]
* xref:cops_rspec.adoc#rspecremoveconst[RSpec/RemoveConst]
* xref:cops_rspec.adoc#rspecrepeateddescription[RSpec/RepeatedDescription]
* xref:cops_rspec.adoc#rspecrepeatedexample[RSpec/RepeatedExample]
* xref:cops_rspec.adoc#rspecrepeatedexamplegroupbody[RSpec/RepeatedExampleGroupBody]
Expand Down
32 changes: 32 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4491,6 +4491,38 @@ end

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RedundantAround

== RSpec/RemoveConst

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| <<next>>
| -
|===

Checks that `remove_const` is not used in specs.

=== Examples

[source,ruby]
----
# bad
it 'does something' do
Object.send(:remove_const, :SomeConstant)
end
before do
SomeClass.send(:remove_const, :SomeConstant)
end
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RemoveConst

== RSpec/RepeatedDescription

|===
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/rspec/remove_const.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Checks that `remove_const` is not used in specs.
#
# @example
# # bad
# it 'does something' do
# Object.send(:remove_const, :SomeConstant)
# end
#
# before do
# SomeClass.send(:remove_const, :SomeConstant)
# end
#
class RemoveConst < Base
include RuboCop::RSpec::Language
extend RuboCop::RSpec::Language::NodePattern

MSG = 'Do not use remove_const in specs. ' \
'Consider using e.g. `stub_const`.'

# @!method remove_const(node)
def_node_matcher :remove_const, <<~PATTERN
(send _ {:send | :__send__} (sym :remove_const) _)
PATTERN

# Check for offenses
def on_send(node)
remove_const(node) do
add_offense(node)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
require_relative 'rspec/receive_messages'
require_relative 'rspec/receive_never'
require_relative 'rspec/redundant_around'
require_relative 'rspec/remove_const'
require_relative 'rspec/repeated_description'
require_relative 'rspec/repeated_example'
require_relative 'rspec/repeated_example_group_body'
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/rspec/remove_const_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::RemoveConst do
it 'detects the `remove_const` usage' do
expect_offense(<<-RUBY)
it 'does something' do
Object.send(:remove_const, :SomeConstant)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`.
end
RUBY

expect_offense(<<-RUBY)
before do
SomeClass.send(:remove_const, :SomeConstant)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`.
end
RUBY
end

it 'detects the `remove_const` usage when using `__send__`' do
expect_offense(<<-RUBY)
it 'does something' do
NiceClass.__send__(:remove_const, :SomeConstant)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`.
end
RUBY
end
end

0 comments on commit 775ad8f

Please sign in to comment.