-
-
Notifications
You must be signed in to change notification settings - Fork 290
Add RSpec/Rails/InferredSpecType cop
#1365
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
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,135 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RuboCop | ||
| module Cop | ||
| module RSpec | ||
| module Rails | ||
| # Identifies redundant spec type. | ||
| # | ||
| # After setting up rspec-rails, you will have enabled | ||
| # `config.infer_spec_type_from_file_location!` by default in | ||
| # spec/rails_helper.rb. This cop works in conjunction with this config. | ||
| # If you disable this config, disable this cop as well. | ||
| # | ||
| # @safety | ||
| # This cop is marked as unsafe because | ||
| # `config.infer_spec_type_from_file_location!` may not be enabled. | ||
| # | ||
| # @example | ||
| # # bad | ||
| # # spec/models/user_spec.rb | ||
| # RSpec.describe User, type: :model do | ||
| # end | ||
| # | ||
| # # good | ||
| # # spec/models/user_spec.rb | ||
| # RSpec.describe User do | ||
| # end | ||
| # | ||
| # # good | ||
| # # spec/models/user_spec.rb | ||
| # RSpec.describe User, type: :common do | ||
| # end | ||
| # | ||
| # @example `Inferences` configuration | ||
| # # .rubocop.yml | ||
| # # RSpec/InferredSpecType: | ||
| # # Inferences: | ||
| # # services: service | ||
| # | ||
| # # bad | ||
| # # spec/services/user_spec.rb | ||
| # RSpec.describe User, type: :service do | ||
| # end | ||
| # | ||
| # # good | ||
| # # spec/services/user_spec.rb | ||
| # RSpec.describe User do | ||
| # end | ||
| # | ||
| # # good | ||
| # # spec/services/user_spec.rb | ||
| # RSpec.describe User, type: :common do | ||
| # end | ||
| class InferredSpecType < Base | ||
| extend AutoCorrector | ||
|
|
||
| MSG = 'Remove redundant spec type.' | ||
|
|
||
| # @param [RuboCop::AST::BlockNode] node | ||
| def on_block(node) | ||
| return unless example_group?(node) | ||
|
|
||
| pair_node = describe_with_type(node) | ||
| return unless pair_node | ||
| return unless inferred_type?(pair_node) | ||
|
|
||
| removable_node = detect_removable_node(pair_node) | ||
| add_offense(removable_node) do |corrector| | ||
| autocorrect(corrector, removable_node) | ||
| end | ||
| end | ||
| alias on_numblock on_block | ||
|
|
||
| private | ||
|
|
||
| # @!method describe_with_type(node) | ||
| # @param [RuboCop::AST::BlockNode] node | ||
| # @return [RuboCop::AST::PairNode, nil] | ||
| def_node_matcher :describe_with_type, <<~PATTERN | ||
| (block | ||
| (send #rspec? #ExampleGroups.all | ||
| ... | ||
| (hash <$(pair (sym :type) sym) ...>) | ||
| ) | ||
| ... | ||
| ) | ||
| PATTERN | ||
|
|
||
| # @param [RuboCop::AST::Corrector] corrector | ||
| # @param [RuboCop::AST::Node] node | ||
| def autocorrect(corrector, node) | ||
| corrector.remove( | ||
| node.location.expression.with( | ||
| begin_pos: node.left_sibling.location.expression.end_pos | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| # @param [RuboCop::AST::PairNode] node | ||
| # @return [RuboCop::AST::Node] | ||
| def detect_removable_node(node) | ||
| if node.parent.pairs.size == 1 | ||
| node.parent | ||
| else | ||
| node | ||
| end | ||
| end | ||
|
|
||
| # @return [String] | ||
| def file_path | ||
| processed_source.file_path | ||
| end | ||
|
|
||
| # @param [RuboCop::AST::PairNode] node | ||
| # @return [Boolean] | ||
| def inferred_type?(node) | ||
| inferred_type_from_file_path.inspect == node.value.source | ||
| end | ||
|
|
||
| # @return [Symbol, nil] | ||
| def inferred_type_from_file_path | ||
| inferences.find do |prefix, type| | ||
| break type.to_sym if file_path.include?("spec/#{prefix}/") | ||
| end | ||
| end | ||
|
|
||
| # @return [Hash] | ||
| def inferences | ||
| cop_config['Inferences'] || {} | ||
| end | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.