From 634538659877cd7da64bf186afd064f9d227af2f Mon Sep 17 00:00:00 2001 From: Sebastian Welther Date: Fri, 1 Dec 2023 15:57:40 +0100 Subject: [PATCH] Add new RSpec/RemoveConst cop --- .rubocop.yml | 2 ++ CHANGELOG.md | 3 ++ config/default.yml | 6 ++++ docs/modules/ROOT/pages/cops.adoc | 1 + docs/modules/ROOT/pages/cops_rspec.adoc | 32 +++++++++++++++++ lib/rubocop/cop/rspec/remove_const.rb | 39 +++++++++++++++++++++ lib/rubocop/cop/rspec_cops.rb | 1 + spec/rubocop/cop/rspec/remove_const_spec.rb | 19 ++++++++++ 8 files changed, 103 insertions(+) create mode 100644 lib/rubocop/cop/rspec/remove_const.rb create mode 100644 spec/rubocop/cop/rspec/remove_const_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index bf1be2c134..96c82cb097 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -177,6 +177,8 @@ RSpec/ReceiveMessages: Enabled: true RSpec/RedundantAround: Enabled: true +RSpec/RemoveConst: + Enabled: true RSpec/SkipBlockInsideExample: Enabled: true RSpec/SortMetadata: diff --git a/CHANGELOG.md b/CHANGELOG.md index e7974d61bf..4f7c05e235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 diff --git a/config/default.yml b/config/default.yml index f672f26f4b..2bce0aeda8 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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: "<>" + 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 diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index 9f0962c129..b1433be8c1 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -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] diff --git a/docs/modules/ROOT/pages/cops_rspec.adoc b/docs/modules/ROOT/pages/cops_rspec.adoc index f985c7e5fb..60c366f59a 100644 --- a/docs/modules/ROOT/pages/cops_rspec.adoc +++ b/docs/modules/ROOT/pages/cops_rspec.adoc @@ -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 +| <> +| - +|=== + +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 + Object.send(:remove_const, :SomeConstant) +end +---- + +=== References + +* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RemoveConst + == RSpec/RepeatedDescription |=== diff --git a/lib/rubocop/cop/rspec/remove_const.rb b/lib/rubocop/cop/rspec/remove_const.rb new file mode 100644 index 0000000000..90e798a346 --- /dev/null +++ b/lib/rubocop/cop/rspec/remove_const.rb @@ -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 + # Object.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 Object :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 diff --git a/lib/rubocop/cop/rspec_cops.rb b/lib/rubocop/cop/rspec_cops.rb index b6d8d83d7b..3041c3ea76 100644 --- a/lib/rubocop/cop/rspec_cops.rb +++ b/lib/rubocop/cop/rspec_cops.rb @@ -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' diff --git a/spec/rubocop/cop/rspec/remove_const_spec.rb b/spec/rubocop/cop/rspec/remove_const_spec.rb new file mode 100644 index 0000000000..bfd72de705 --- /dev/null +++ b/spec/rubocop/cop/rspec/remove_const_spec.rb @@ -0,0 +1,19 @@ +# 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 + Object.send(:remove_const, :SomeConstant) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use remove_const in specs. Consider using e.g. `stub_const`. + end + RUBY + end +end