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
Add new RSpec/FactoryBot/SyntaxMethods
cop.
#1215
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--markup markdown | ||
--hide-void-return | ||
--tag safety:"Cop Safety Information" |
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
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
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,107 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module FactoryBot | ||
# Use shorthands from `FactoryBot::Syntax::Methods` in your specs. | ||
# | ||
# @safety | ||
# The auto-correction is marked as unsafe because the cop | ||
# cannot verify whether you already include | ||
# `FactoryBot::Syntax::Methods` in your test suite. | ||
# | ||
# If you're using Rails, add the following configuration to | ||
# `spec/support/factory_bot.rb` and be sure to require that file in | ||
# `rails_helper.rb`: | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# RSpec.configure do |config| | ||
# config.include FactoryBot::Syntax::Methods | ||
# end | ||
# ---- | ||
# | ||
# If you're not using Rails: | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# RSpec.configure do |config| | ||
# config.include FactoryBot::Syntax::Methods | ||
# | ||
# config.before(:suite) do | ||
# FactoryBot.find_definitions | ||
# end | ||
# end | ||
# ---- | ||
# | ||
# @example | ||
# # bad | ||
# FactoryBot.create(:bar) | ||
# FactoryBot.build(:bar) | ||
# FactoryBot.attributes_for(:bar) | ||
# | ||
# # good | ||
# create(:bar) | ||
# build(:bar) | ||
# attributes_for(:bar) | ||
# | ||
class SyntaxMethods < Base | ||
extend AutoCorrector | ||
include InsideExampleGroup | ||
include RangeHelp | ||
include RuboCop::RSpec::FactoryBot::Language | ||
|
||
MSG = 'Use `%<method>s` from `FactoryBot::Syntax::Methods`.' | ||
|
||
RESTRICT_ON_SEND = %i[ | ||
attributes_for | ||
attributes_for_list | ||
attributes_for_pair | ||
build | ||
build_list | ||
build_pair | ||
build_stubbed | ||
build_stubbed_list | ||
build_stubbed_pair | ||
create | ||
create_list | ||
create_pair | ||
generate | ||
generate_list | ||
null | ||
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. TIL ❤️ 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.
|
||
null_list | ||
null_pair | ||
].to_set.freeze | ||
|
||
def on_send(node) | ||
return unless factory_bot?(node.receiver) | ||
return unless inside_example_group?(node) | ||
|
||
message = format(MSG, method: node.method_name) | ||
|
||
add_offense(crime_scene(node), message: message) do |corrector| | ||
corrector.remove(offense(node)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def crime_scene(node) | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
range_between( | ||
node.loc.expression.begin_pos, | ||
node.loc.selector.end_pos | ||
) | ||
end | ||
|
||
def offense(node) | ||
range_between( | ||
node.loc.expression.begin_pos, | ||
node.loc.selector.begin_pos | ||
) | ||
end | ||
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helps you identify whether a given node | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# is within an example group or not. | ||
module InsideExampleGroup | ||
private | ||
|
||
def inside_example_group?(node) | ||
leoarnold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return example_group?(node) if example_group_root?(node) | ||
|
||
root = node.ancestors.find { |parent| example_group_root?(parent) } | ||
|
||
example_group?(root) | ||
end | ||
|
||
def example_group_root?(node) | ||
node.parent.nil? || example_group_root_with_siblings?(node.parent) | ||
end | ||
|
||
def example_group_root_with_siblings?(node) | ||
node.begin_type? && node.parent.nil? | ||
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
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
module FactoryBot | ||
# Contains node matchers for common FactoryBot DSL. | ||
module Language | ||
extend RuboCop::NodePattern::Macros | ||
|
||
# @!method factory_bot?(node) | ||
def_node_matcher :factory_bot?, <<~PATTERN | ||
(const {nil? cbase} {:FactoryGirl :FactoryBot}) | ||
PATTERN | ||
end | ||
end | ||
end | ||
end |
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.
We didn't use that notion in the past, but it's quite a useful trick.