Skip to content

Commit

Permalink
Refactor DescribeClass to use TopLevelGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Jul 29, 2020
1 parent 9520638 commit ea8bf0e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add a new base cop class `::RuboCop::Cop::RSpec::Base`. The old base class `::RuboCop::Cop::RSpec::Cop` is deprecated, and will be removed in the next major release. ([@bquorning][])
* Add support for subject detection after includes and example groups in `RSpec/LeadingSubject`. ([@pirj][])
* Ignore trailing punctuation in context description prefix. ([@elliterate][])
* Improve `RSpec/NestedGroups`, `RSpec/FilePath`, `RSpec/DescribeMethod`, `RSpec/MultipleDescribes`, `RSpec/DescribeClass`'s top-level example group detection. ([@pirj][])

## 1.42.0 (2020-07-09)

Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ RSpec/ContextWording:
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextWording

RSpec/DescribeClass:
Description: Check that the first argument to the top level describe is a constant.
Description: Check that the first argument to the top-level describe is a constant.
Enabled: true
VersionAdded: '1.0'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeClass
Expand Down
47 changes: 18 additions & 29 deletions lib/rubocop/cop/rspec/describe_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RuboCop
module Cop
module RSpec
# Check that the first argument to the top level describe is a constant.
# Check that the first argument to the top-level describe is a constant.
#
# @example
# # bad
Expand All @@ -22,49 +22,38 @@ module RSpec
# describe "A feature example", type: :feature do
# end
class DescribeClass < Base
include RuboCop::RSpec::TopLevelDescribe
include RuboCop::RSpec::TopLevelGroup

MSG = 'The first argument to describe should be '\
'the class or module being tested.'

def_node_matcher :valid_describe?, <<-PATTERN
{
(send #{RSPEC} :describe const ...)
(send #{RSPEC} :describe)
}
PATTERN

def_node_matcher :describe_with_rails_metadata?, <<-PATTERN
(send #{RSPEC} :describe !const ...
(hash <#rails_metadata? ...>)
)
PATTERN

def_node_matcher :rails_metadata?, <<-PATTERN
(pair
(sym :type)
(sym {
:channel :controller :helper :job :mailer :model :request
:routing :view :feature :system :mailbox
}
)
(sym { :channel :controller :helper :job :mailer :model :request
:routing :view :feature :system :mailbox })
)
PATTERN

def on_top_level_describe(node, (described_value, _))
return if shared_group?(root_node)
return if valid_describe?(node)
return if describe_with_rails_metadata?(node)
return if string_constant_describe?(described_value)
def_node_matcher :not_a_const_described, <<~PATTERN
(block
[ (send #{RSPEC} :describe $[!const !#string_constant?] ...)
!(send #{RSPEC} :describe ... (hash <#rails_metadata? ...>)) ]
...
)
PATTERN

add_offense(described_value)
def on_top_level_group(top_level_node)
not_a_const_described(top_level_node) do |described|
add_offense(described)
end
end

private

def string_constant_describe?(described_value)
described_value.str_type? &&
described_value.value.match?(/^(?:(?:::)?[A-Z]\w*)+$/)
def string_constant?(described)
described.str_type? &&
described.value.match?(/^(?:(?:::)?[A-Z]\w*)+$/)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_rspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
--- | --- | --- | --- | ---
Enabled | Yes | No | 1.0 | -

Check that the first argument to the top level describe is a constant.
Check that the first argument to the top-level describe is a constant.

### Examples

Expand Down

0 comments on commit ea8bf0e

Please sign in to comment.