-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Check for repeated description strings in example groups. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# RSpec.describe User do | ||
# it 'is valid' do | ||
# # ... | ||
# end | ||
# | ||
# it 'is valid' do | ||
# # ... | ||
# end | ||
# end | ||
# | ||
# # good | ||
# RSpec.describe User do | ||
# it 'is valid when first and last name are present' do | ||
# # ... | ||
# end | ||
# | ||
# it 'is valid when last name only is present' do | ||
# # ... | ||
# end | ||
# end | ||
# | ||
class RepeatedDescription < Cop | ||
include RuboCop::Cop::ConfigurableEnforcedStyle | ||
|
||
def_node_matcher :example, <<-PATTERN | ||
(block $(send _ {#{Examples::ALL.to_node_pattern}} ...) ...) | ||
PATTERN | ||
|
||
# Selectors which indicate that indicate we should stop searching | ||
SCOPE_CHANGES = ExampleGroups::ALL + SharedGroups::ALL | ||
|
||
# @!method scope_change?(node) | ||
# | ||
# Detect if the node is an example or a new example group | ||
# | ||
def_node_matcher :scope_change?, <<-PATTERN | ||
(block (send _ {#{SCOPE_CHANGES.to_node_pattern}} ...) ...) | ||
PATTERN | ||
|
||
MSG = 'Repeated description'.freeze | ||
|
||
def on_block(node) | ||
return unless example_group?(node) | ||
|
||
repeated_descriptions(node).each do |repeated_hook| | ||
add_offense(repeated_hook, :expression) | ||
end | ||
end | ||
|
||
private | ||
|
||
def repeated_descriptions(node) | ||
hooks_in_group(node) | ||
.group_by(&:method_args) | ||
.reject { |args, _| args.empty? } | ||
.values | ||
.reject(&:one?) | ||
.flatten | ||
end | ||
|
||
def hooks_in_group(node, &block) | ||
return to_enum(__method__, node) unless block_given? | ||
|
||
node.each_child_node { |child| find_hooks(child, &block) } | ||
end | ||
|
||
def find_hooks(node, &block) | ||
return if scope_change?(node) | ||
|
||
example(node, &block) | ||
hooks_in_group(node, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
describe RuboCop::Cop::RSpec::RepeatedDescription do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for repeated descriptions' do | ||
expect_violation(<<-RUBY) | ||
describe 'doing x' do | ||
it "does x" do | ||
^^^^^^^^^^^ Repeated description | ||
end | ||
it "does x" do | ||
^^^^^^^^^^^ Repeated description | ||
end | ||
end | ||
RUBY | ||
end | ||
end |