-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/FactoryBot/SyntaxMethods
cop.
`FactoryBot` provides a mixin called [`FactoryBot::Syntax::Methods'](https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec) which allows to replace ```ruby FactoryBot.create(:bar) FactoryBot.build(:bar) FactoryBot.attributes_for(:bar) ``` with ``` create(:bar) build(:bar) attributes_for(:bar) ``` The auto-correction is unsafe as there is no way to tell just from syntax whether a given example group includes the mixin or not.
- Loading branch information
Showing
13 changed files
with
290 additions
and
27 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
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 | ||
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) | ||
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 | ||
# is within an example group or not. | ||
module InsideExampleGroup | ||
private | ||
|
||
def inside_example_group?(node) | ||
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.