diff --git a/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb b/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb index 6eff7f08..5022014b 100644 --- a/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb +++ b/lib/rubocop/cop/rspec_rails/inferred_spec_type.rb @@ -50,7 +50,7 @@ module RSpecRails # # spec/services/user_spec.rb # RSpec.describe User, type: :common do # end - class InferredSpecType < ::RuboCop::Cop::RSpec::Base + class InferredSpecType < ::RuboCop::Cop::Base extend AutoCorrector MSG = 'Remove redundant spec type.' @@ -85,6 +85,35 @@ def on_block(node) ) PATTERN + # @!method example_group?(node) + def_node_matcher :example_group?, <<~PATTERN + ({block numblock} + (send #rspec? #ExampleGroups.all ... + ) ... + ) + PATTERN + + # @!method rspec?(node) + def_node_matcher :rspec?, <<~PATTERN + { + (const {nil? cbase} :RSpec) + nil? + } + PATTERN + + module ExampleGroups # :nodoc: + REGULAR = %i[describe context feature example_group].freeze + SKIPPED = %i[xdescribe xcontext xfeature].freeze + FOCUSED = %i[fdescribe fcontext ffeature].freeze + ALL = REGULAR + SKIPPED + FOCUSED + + class << self + def all(element) + ALL.include?(element) + end + end + end + # @param [RuboCop::AST::Corrector] corrector # @param [RuboCop::AST::Node] node def autocorrect(corrector, node) diff --git a/lib/rubocop/rspec_rails/description_extractor.rb b/lib/rubocop/rspec_rails/description_extractor.rb index c524694c..7143b2f5 100644 --- a/lib/rubocop/rspec_rails/description_extractor.rb +++ b/lib/rubocop/rspec_rails/description_extractor.rb @@ -21,7 +21,6 @@ def to_h # Decorator of a YARD code object for working with documented rspec cops class CodeObject - RSPEC_RAILS_COP_CLASS_NAME = 'RuboCop::Cop::RSpec::Base' RUBOCOP_COP_CLASS_NAME = 'RuboCop::Cop::Base' def initialize(yardoc) @@ -57,8 +56,7 @@ def documented_constant end def cop_subclass? - yardoc.superclass.path == RSPEC_RAILS_COP_CLASS_NAME || - yardoc.superclass.path == RUBOCOP_COP_CLASS_NAME + yardoc.superclass.path == RUBOCOP_COP_CLASS_NAME end def abstract?