-
-
Notifications
You must be signed in to change notification settings - Fork 285
[Close #856] implement repeated example group description/body cops #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,87 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module RSpec | ||
| # Check for repeated describe and context block body. | ||
| # | ||
| # @example | ||
| # | ||
| # # bad | ||
| # describe 'cool feature x' do | ||
| # it { cool_predicate } | ||
| # end | ||
| # | ||
| # describe 'cool feature y' do | ||
| # it { cool_predicate } | ||
| # end | ||
| # | ||
| # # good | ||
| # describe 'cool feature' do | ||
| # it { cool_predicate } | ||
| # end | ||
| # | ||
| # describe 'another cool feature' do | ||
| # it { another_predicate } | ||
| # end | ||
| # | ||
| # # good | ||
| # context 'when case x', :tag do | ||
| # it { cool_predicate } | ||
| # end | ||
| # | ||
| # context 'when case y' do | ||
| # it { cool_predicate } | ||
| # end | ||
| # | ||
| class RepeatedExampleGroupBody < Cop | ||
| MSG = 'Repeated %<group>s block body on line(s) %<loc>s' | ||
|
|
||
| def_node_matcher :several_example_groups?, <<-PATTERN | ||
| (begin <#example_group_with_body? #example_group_with_body? ...>) | ||
| PATTERN | ||
|
|
||
| def_node_matcher :metadata, '(block (send _ _ _ $...) ...)' | ||
lazycoder9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def_node_matcher :body, '(block _ args $...)' | ||
|
|
||
| def_node_matcher :skip_or_pending?, <<-PATTERN | ||
| (block <(send nil? {:skip :pending}) ...>) | ||
| PATTERN | ||
|
|
||
| def on_begin(node) | ||
| return unless several_example_groups?(node) | ||
|
|
||
| repeated_group_bodies(node).each do |group, repeats| | ||
| add_offense(group, message: message(group, repeats)) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def repeated_group_bodies(node) | ||
| node | ||
| .children | ||
lazycoder9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .select { |child| example_group_with_body?(child) } | ||
| .reject { |child| skip_or_pending?(child) } | ||
| .group_by { |group| signature_keys(group) } | ||
| .values | ||
| .reject(&:one?) | ||
| .flat_map { |groups| add_repeated_lines(groups) } | ||
| end | ||
|
|
||
| def add_repeated_lines(groups) | ||
| repeated_lines = groups.map(&:first_line) | ||
| groups.map { |group| [group, repeated_lines - [group.first_line]] } | ||
| end | ||
|
|
||
| def signature_keys(group) | ||
| [metadata(group), body(group)] | ||
Darhazer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def message(group, repeats) | ||
| format(MSG, group: group.method_name, loc: repeats) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
96 changes: 96 additions & 0 deletions
96
lib/rubocop/cop/rspec/repeated_example_group_description.rb
This file contains hidden or 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,96 @@ | ||
| # frozen_string_literal: true | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module RSpec | ||
| # Check for repeated example group descriptions. | ||
| # | ||
| # @example | ||
| # | ||
| # # bad | ||
| # describe 'cool feature' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # describe 'cool feature' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # # bad | ||
| # context 'when case x' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # describe 'when case x' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # # good | ||
| # describe 'cool feature' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # describe 'another cool feature' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # # good | ||
| # context 'when case x' do | ||
| # # example group | ||
| # end | ||
| # | ||
| # context 'when another case' do | ||
| # # example group | ||
| # end | ||
lazycoder9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # | ||
| class RepeatedExampleGroupDescription < Cop | ||
| MSG = 'Repeated %<group>s block description on line(s) %<loc>s' | ||
|
|
||
| def_node_matcher :several_example_groups?, <<-PATTERN | ||
| (begin <#example_group? #example_group? ...>) | ||
| PATTERN | ||
|
|
||
| def_node_matcher :doc_string_and_metadata, <<-PATTERN | ||
| (block (send _ _ $_ $...) ...) | ||
| PATTERN | ||
|
|
||
| def_node_matcher :skip_or_pending?, <<-PATTERN | ||
| (block <(send nil? {:skip :pending}) ...>) | ||
| PATTERN | ||
|
|
||
| def_node_matcher :empty_description?, '(block (send _ _) ...)' | ||
|
|
||
| def on_begin(node) | ||
| return unless several_example_groups?(node) | ||
|
|
||
| repeated_group_descriptions(node).each do |group, repeats| | ||
| add_offense(group, message: message(group, repeats)) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def repeated_group_descriptions(node) | ||
| node | ||
| .children | ||
| .select { |child| example_group?(child) } | ||
| .reject { |child| skip_or_pending?(child) } | ||
| .reject { |child| empty_description?(child) } | ||
| .group_by { |group| doc_string_and_metadata(group) } | ||
lazycoder9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .values | ||
| .reject(&:one?) | ||
| .flat_map { |groups| add_repeated_lines(groups) } | ||
| end | ||
|
|
||
| def add_repeated_lines(groups) | ||
| repeated_lines = groups.map(&:first_line) | ||
| groups.map { |group| [group, repeated_lines - [group.first_line]] } | ||
| end | ||
|
|
||
| def message(group, repeats) | ||
| format(MSG, group: group.method_name, loc: repeats) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.