From c81b279033b4ad46601307695bf4acb2d6eae27b Mon Sep 17 00:00:00 2001 From: Brad Lindsay Date: Sat, 23 Nov 2024 11:46:09 -0600 Subject: [PATCH] Remove `RSpecRails/InferredSpecType` At the time this cop was added, [it was noted][PR Comment] that this was legacy behavior left as the default for people migrating to RSpec 3. Now RSpec 7.1.0 has [removed this default][PR] to make it clear that this is deprecated / legacy behavior, let's delete this cop. [PR Comment]: https://github.com/rubocop/rubocop-rspec/pull/1365#issuecomment-1243454790 [PR]: https://github.com/rspec/rspec-rails/pull/2804 --- CHANGELOG.md | 2 + config/default.yml | 23 --- docs/modules/ROOT/pages/cops.adoc | 1 - docs/modules/ROOT/pages/cops_rspecrails.adoc | 89 ----------- .../cop/rspec_rails/inferred_spec_type.rb | 143 ----------------- lib/rubocop/cop/rspec_rails_cops.rb | 1 - .../rspec_rails/inferred_spec_type_spec.rb | 144 ------------------ 7 files changed, 2 insertions(+), 401 deletions(-) delete mode 100644 lib/rubocop/cop/rspec_rails/inferred_spec_type.rb delete mode 100644 spec/rubocop/cop/rspec_rails/inferred_spec_type_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bdfe4cc..6322b0ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Handle unknown HTTP status codes for `RSpecRails/HttpStatus` cop. ([@viralpraxis]) +- Remove `RSpecRails/InferredSpecType` now that it is clearly legacy / deprecated behavior. ([@bfad]) ## 2.30.0 (2024-06-12) @@ -72,6 +73,7 @@ [@akiomik]: https://github.com/akiomik [@anthony-robin]: https://github.com/anthony-robin +[@bfad]: https://github.com/bfad [@bquorning]: https://github.com/bquorning [@corydiamand]: https://github.com/corydiamand [@g-rath]: https://github.com/G-Rath diff --git a/config/default.yml b/config/default.yml index 51ec8889..f41a85e2 100644 --- a/config/default.yml +++ b/config/default.yml @@ -35,29 +35,6 @@ RSpecRails/HttpStatus: VersionChanged: '2.20' Reference: https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/HttpStatus -RSpecRails/InferredSpecType: - Description: Identifies redundant spec type. - Enabled: pending - Safe: false - VersionAdded: '2.14' - Reference: https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/InferredSpecType - Inferences: - channels: channel - controllers: controller - features: feature - generator: generator - helpers: helper - jobs: job - mailboxes: mailbox - mailers: mailer - models: model - requests: request - integration: request - api: request - routing: routing - system: system - views: view - RSpecRails/MinitestAssertions: Description: Check if using Minitest-like matchers. Enabled: pending diff --git a/docs/modules/ROOT/pages/cops.adoc b/docs/modules/ROOT/pages/cops.adoc index b4fc11c6..a83eadec 100644 --- a/docs/modules/ROOT/pages/cops.adoc +++ b/docs/modules/ROOT/pages/cops.adoc @@ -5,7 +5,6 @@ * xref:cops_rspecrails.adoc#rspecrailsavoidsetuphook[RSpecRails/AvoidSetupHook] * xref:cops_rspecrails.adoc#rspecrailshavehttpstatus[RSpecRails/HaveHttpStatus] * xref:cops_rspecrails.adoc#rspecrailshttpstatus[RSpecRails/HttpStatus] -* xref:cops_rspecrails.adoc#rspecrailsinferredspectype[RSpecRails/InferredSpecType] * xref:cops_rspecrails.adoc#rspecrailsminitestassertions[RSpecRails/MinitestAssertions] * xref:cops_rspecrails.adoc#rspecrailsnegationbevalid[RSpecRails/NegationBeValid] * xref:cops_rspecrails.adoc#rspecrailstravelaround[RSpecRails/TravelAround] diff --git a/docs/modules/ROOT/pages/cops_rspecrails.adoc b/docs/modules/ROOT/pages/cops_rspecrails.adoc index dd014e5a..db7bd813 100644 --- a/docs/modules/ROOT/pages/cops_rspecrails.adoc +++ b/docs/modules/ROOT/pages/cops_rspecrails.adoc @@ -211,95 +211,6 @@ it { is_expected.to have_http_status :ok } * https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/HttpStatus -[#rspecrailsinferredspectype] -== RSpecRails/InferredSpecType - -|=== -| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed - -| Pending -| No -| Always (Unsafe) -| 2.14 -| - -|=== - -Identifies redundant spec type. - -After setting up rspec-rails, you will have enabled -`config.infer_spec_type_from_file_location!` by default in -spec/rails_helper.rb. This cop works in conjunction with this config. -If you disable this config, disable this cop as well. - -[#safety-rspecrailsinferredspectype] -=== Safety - -This cop is marked as unsafe because -`config.infer_spec_type_from_file_location!` may not be enabled. - -[#examples-rspecrailsinferredspectype] -=== Examples - -[source,ruby] ----- -# bad -# spec/models/user_spec.rb -RSpec.describe User, type: :model do -end - -# good -# spec/models/user_spec.rb -RSpec.describe User do -end - -# good -# spec/models/user_spec.rb -RSpec.describe User, type: :common do -end ----- - -[#_inferences_-configuration-rspecrailsinferredspectype] -==== `Inferences` configuration - -[source,ruby] ----- -# .rubocop.yml -# RSpecRails/InferredSpecType: -# Inferences: -# services: service - -# bad -# spec/services/user_spec.rb -RSpec.describe User, type: :service do -end - -# good -# spec/services/user_spec.rb -RSpec.describe User do -end - -# good -# spec/services/user_spec.rb -RSpec.describe User, type: :common do -end ----- - -[#configurable-attributes-rspecrailsinferredspectype] -=== Configurable attributes - -|=== -| Name | Default value | Configurable values - -| Inferences -| `{"channels"=>"channel", "controllers"=>"controller", "features"=>"feature", "generator"=>"generator", "helpers"=>"helper", "jobs"=>"job", "mailboxes"=>"mailbox", "mailers"=>"mailer", "models"=>"model", "requests"=>"request", "integration"=>"request", "api"=>"request", "routing"=>"routing", "system"=>"system", "views"=>"view"}` -| -|=== - -[#references-rspecrailsinferredspectype] -=== References - -* https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/InferredSpecType - [#rspecrailsminitestassertions] == RSpecRails/MinitestAssertions diff --git a/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb b/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb deleted file mode 100644 index 6eff7f08..00000000 --- a/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb +++ /dev/null @@ -1,143 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module RSpecRails - # Identifies redundant spec type. - # - # After setting up rspec-rails, you will have enabled - # `config.infer_spec_type_from_file_location!` by default in - # spec/rails_helper.rb. This cop works in conjunction with this config. - # If you disable this config, disable this cop as well. - # - # @safety - # This cop is marked as unsafe because - # `config.infer_spec_type_from_file_location!` may not be enabled. - # - # @example - # # bad - # # spec/models/user_spec.rb - # RSpec.describe User, type: :model do - # end - # - # # good - # # spec/models/user_spec.rb - # RSpec.describe User do - # end - # - # # good - # # spec/models/user_spec.rb - # RSpec.describe User, type: :common do - # end - # - # @example `Inferences` configuration - # # .rubocop.yml - # # RSpecRails/InferredSpecType: - # # Inferences: - # # services: service - # - # # bad - # # spec/services/user_spec.rb - # RSpec.describe User, type: :service do - # end - # - # # good - # # spec/services/user_spec.rb - # RSpec.describe User do - # end - # - # # good - # # spec/services/user_spec.rb - # RSpec.describe User, type: :common do - # end - class InferredSpecType < ::RuboCop::Cop::RSpec::Base - extend AutoCorrector - - MSG = 'Remove redundant spec type.' - - # @param [RuboCop::AST::BlockNode] node - def on_block(node) - return unless example_group?(node) - - pair_node = describe_with_type(node) - return unless pair_node - return unless inferred_type?(pair_node) - - removable_node = detect_removable_node(pair_node) - add_offense(removable_node) do |corrector| - autocorrect(corrector, removable_node) - end - end - alias on_numblock on_block - - private - - # @!method describe_with_type(node) - # @param [RuboCop::AST::BlockNode] node - # @return [RuboCop::AST::PairNode, nil] - def_node_matcher :describe_with_type, <<~PATTERN - (block - (send #rspec? #ExampleGroups.all - ... - (hash <$(pair (sym :type) sym) ...>) - ) - ... - ) - PATTERN - - # @param [RuboCop::AST::Corrector] corrector - # @param [RuboCop::AST::Node] node - def autocorrect(corrector, node) - corrector.remove(remove_range(node)) - end - - # @param [RuboCop::AST::Node] node - # @return [Parser::Source::Range] - def remove_range(node) - if node.left_sibling - node.source_range.with( - begin_pos: node.left_sibling.source_range.end_pos - ) - elsif node.right_sibling - node.source_range.with( - end_pos: node.right_sibling.source_range.begin_pos - ) - end - end - - # @param [RuboCop::AST::PairNode] node - # @return [RuboCop::AST::Node] - def detect_removable_node(node) - if node.parent.pairs.size == 1 - node.parent - else - node - end - end - - # @return [String] - def file_path - processed_source.file_path - end - - # @param [RuboCop::AST::PairNode] node - # @return [Boolean] - def inferred_type?(node) - inferred_type_from_file_path.inspect == node.value.source - end - - # @return [Symbol, nil] - def inferred_type_from_file_path - inferences.find do |prefix, type| - break type.to_sym if file_path.include?("spec/#{prefix}/") - end - end - - # @return [Hash] - def inferences - cop_config['Inferences'] || {} - end - end - end - end -end diff --git a/lib/rubocop/cop/rspec_rails_cops.rb b/lib/rubocop/cop/rspec_rails_cops.rb index 1a101b34..f6f3174f 100644 --- a/lib/rubocop/cop/rspec_rails_cops.rb +++ b/lib/rubocop/cop/rspec_rails_cops.rb @@ -3,7 +3,6 @@ require_relative 'rspec_rails/avoid_setup_hook' require_relative 'rspec_rails/have_http_status' require_relative 'rspec_rails/http_status' -require_relative 'rspec_rails/inferred_spec_type' require_relative 'rspec_rails/minitest_assertions' require_relative 'rspec_rails/negation_be_valid' require_relative 'rspec_rails/travel_around' diff --git a/spec/rubocop/cop/rspec_rails/inferred_spec_type_spec.rb b/spec/rubocop/cop/rspec_rails/inferred_spec_type_spec.rb deleted file mode 100644 index 55a68f7a..00000000 --- a/spec/rubocop/cop/rspec_rails/inferred_spec_type_spec.rb +++ /dev/null @@ -1,144 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe RuboCop::Cop::RSpecRails::InferredSpecType do - describe 'with necessary type in keyword arguments' do - it 'does not register any offense' do - expect_no_offenses(<<~RUBY) - RSpec.describe User, type: :model do - end - RUBY - end - end - - describe 'with redundant type in keyword arguments' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User, type: :model do - ^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User do - end - RUBY - end - end - - describe 'with redundant type in Hash arguments' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User, { type: :model } do - ^^^^^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User do - end - RUBY - end - end - - describe 'with redundant type before other Hash metadata' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User, type: :model, other: true do - ^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User, other: true do - end - RUBY - end - end - - describe 'with redundant type after other Hash metadata' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User, other: true, type: :model do - ^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User, other: true do - end - RUBY - end - end - - describe 'with redundant type and other Symbol metadata' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User, :other, type: :model do - ^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User, :other do - end - RUBY - end - end - - describe 'with redundant type and receiver-less describe' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - describe User, type: :model do - ^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - describe User do - end - RUBY - end - end - - describe 'with redundant type in inner example group' do - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/models/user_spec.rb') - RSpec.describe User do - describe 'inner', type: :model do - ^^^^^^^^^^^^ Remove redundant spec type. - end - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User do - describe 'inner' do - end - end - RUBY - end - end - - describe 'with Inferences configuration' do - let(:cop_config) do - { - 'Inferences' => { - 'services' => 'service' - } - } - end - - it 'registers and corrects an offense' do - expect_offense(<<~RUBY, '/path/to/project/spec/services/user_spec.rb') - RSpec.describe User, type: :service do - ^^^^^^^^^^^^^^ Remove redundant spec type. - end - RUBY - - expect_correction(<<~RUBY) - RSpec.describe User do - end - RUBY - end - end -end