-
-
Notifications
You must be signed in to change notification settings - Fork 288
Add autocorrect support for RSpec/MultipleExpectations
#2143
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
Open
gildesmarais
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
gildesmarais:feat/autocorrect-agg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,9 @@ module RSpec | |
| # end | ||
| # end | ||
| # | ||
| class MultipleExpectations < Base | ||
| class MultipleExpectations < Base # rubocop:disable Metrics/ClassLength | ||
| extend AutoCorrector | ||
|
|
||
| MSG = 'Example has too many expectations [%<total>d/%<max>d].' | ||
|
|
||
| ANYTHING = ->(_node) { true } | ||
|
|
@@ -76,6 +78,13 @@ class MultipleExpectations < Base | |
|
|
||
| # @!method aggregate_failures?(node) | ||
| def_node_matcher :aggregate_failures?, <<~PATTERN | ||
| (send _ _ <{ (sym :aggregate_failures) (hash <(pair (sym :aggregate_failures) _) ...>) } ...>) | ||
| PATTERN | ||
| # @!method metadata_present?(node) | ||
| def_node_matcher :metadata_present?, '(send _ _ <{sym hash} ...>)' | ||
|
|
||
| # @!method aggregate_failures_definition?(node) | ||
| def_node_matcher :aggregate_failures_definition?, <<~PATTERN | ||
| (block { | ||
| (send _ _ <(sym :aggregate_failures) ...>) | ||
| (send _ _ ... (hash <(pair (sym :aggregate_failures) %1) ...>)) | ||
|
|
@@ -110,12 +119,14 @@ def example_with_aggregate_failures?(example_node) | |
| node_with_aggregate_failures = find_aggregate_failures(example_node) | ||
| return false unless node_with_aggregate_failures | ||
|
|
||
| aggregate_failures?(node_with_aggregate_failures, TRUE_NODE) | ||
| aggregate_failures_definition?(node_with_aggregate_failures, | ||
| TRUE_NODE) | ||
| end | ||
|
|
||
| def find_aggregate_failures(example_node) | ||
| example_node.send_node.each_ancestor(:block) | ||
| .find { |block_node| aggregate_failures?(block_node, ANYTHING) } | ||
| example_node.send_node.each_ancestor(:block).find do |block_node| | ||
| aggregate_failures_definition?(block_node, ANYTHING) | ||
| end | ||
| end | ||
|
|
||
| def find_expectation(node, &block) | ||
|
|
@@ -137,12 +148,54 @@ def flag_example(node, expectation_count:) | |
| total: expectation_count, | ||
| max: max_expectations | ||
| ) | ||
| ) | ||
| ) do |corrector| | ||
| autocorrect_metadata(corrector, node.send_node) | ||
| end | ||
| end | ||
|
|
||
| def max_expectations | ||
| Integer(cop_config.fetch('Max', 1)) | ||
| end | ||
|
|
||
| def autocorrect_metadata(corrector, node) | ||
| return if aggregate_failures?(node) | ||
|
|
||
| if metadata_present?(node) | ||
| add_hash_metadata(corrector, node) | ||
| else | ||
| add_symbol_metadata(corrector, node) | ||
| end | ||
| end | ||
|
|
||
| def add_symbol_metadata(corrector, node) | ||
| if node.arguments.empty? | ||
| # Handle cases like `it { ... }` vs `it(...) { ... }` | ||
| loc, str = if node.loc.begin | ||
| [node.loc.begin, ':aggregate_failures'] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add test coverage for this line, please? |
||
| else | ||
| [node.loc.selector, '(:aggregate_failures)'] | ||
| end | ||
| corrector.insert_after(loc, str) | ||
| else | ||
| corrector.insert_after(node.last_argument, | ||
| ', :aggregate_failures') | ||
| end | ||
| end | ||
|
|
||
| def add_hash_metadata(corrector, node) | ||
| if (hash_node = node.arguments.reverse.find(&:hash_type?)) | ||
| if hash_node.pairs.empty? | ||
| corrector.insert_before(hash_node.loc.end, | ||
| ' aggregate_failures: true ') | ||
|
Comment on lines
+188
to
+189
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add test coverage for this line too, please? |
||
| else | ||
| corrector.insert_after(hash_node.pairs.last, | ||
| ', aggregate_failures: true') | ||
| end | ||
| else | ||
| corrector.insert_after(node.last_argument, | ||
| ', aggregate_failures: true') | ||
| 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.