lib/rubocop/cop/rspec/describe_class.rb in rubocop-rspec-1.5.1 vs lib/rubocop/cop/rspec/describe_class.rb in rubocop-rspec-1.5.2
- old
+ new
@@ -1,6 +1,5 @@
-# encoding: utf-8
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
@@ -19,28 +18,37 @@
# describe "A feature example", type: :feature do
# end
class DescribeClass < Cop
include RuboCop::RSpec::TopLevelDescribe
- REQUEST_PAIR = s(:pair, s(:sym, :type), s(:sym, :request))
- FEATURE_PAIR = s(:pair, s(:sym, :type), s(:sym, :feature))
- ROUTING_PAIR = s(:pair, s(:sym, :type), s(:sym, :routing))
- VIEW_PAIR = s(:pair, s(:sym, :type), s(:sym, :view))
+ MSG = 'The first argument to describe should be '\
+ 'the class or module being tested.'.freeze
- MESSAGE = 'The first argument to describe should be the class or ' \
- 'module being tested.'.freeze
+ def_node_matcher :valid_describe?, <<-PATTERN
+ {(send {(const nil :RSpec) nil} :describe const ...) (send nil :describe)}
+ PATTERN
- def on_top_level_describe(_node, args)
- return if args[0] && args[0].type == :const
+ def_node_matcher :describe_with_metadata, <<-PATTERN
+ (send {(const nil :RSpec) nil} :describe
+ !const
+ ...
+ (hash $...))
+ PATTERN
- return if args[1..-1].any? do |arg|
- next unless arg.hash_type?
- arg.children.any? do |n|
- [REQUEST_PAIR, FEATURE_PAIR, ROUTING_PAIR, VIEW_PAIR].include?(n)
- end
+ def_node_matcher :rails_metadata?, <<-PATTERN
+ (pair
+ (sym :type)
+ (sym {:request :feature :routing :view}))
+ PATTERN
+
+ def on_top_level_describe(node, args)
+ return if valid_describe?(node)
+
+ describe_with_metadata(node) do |pairs|
+ return if pairs.any?(&method(:rails_metadata?))
end
- add_offense(args[0], :expression, MESSAGE)
+ add_offense(args.first, :expression)
end
end
end
end
end