Class: RuboCop::Cop::FactoryBot::SyntaxMethods

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, FactoryBot::Language
Defined in:
lib/rubocop/cop/factory_bot/syntax_methods.rb

Overview

Use shorthands from ‘FactoryBot::Syntax::Methods` in your specs.

Examples:

# bad
FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)

# good
create(:bar)
build(:bar)
attributes_for(:bar)

Constant Summary collapse

MSG =
'Use `%<method>s` from `FactoryBot::Syntax::Methods`.'
RESTRICT_ON_SEND =
RuboCop::FactoryBot::Language::METHODS

Constants included from FactoryBot::Language

FactoryBot::Language::METHODS

Instance Method Summary collapse

Methods included from FactoryBot::Language

#factory_bot?

Instance Method Details

#on_send(node) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubocop/cop/factory_bot/syntax_methods.rb', line 73

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

#spec_group?(node) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/factory_bot/syntax_methods.rb', line 58

def_node_matcher :spec_group?, <<~PATTERN
  (block
    (send
      {(const {nil? cbase} :RSpec) nil?}
      {
        :describe :context :feature :example_group
        :xdescribe :xcontext :xfeature
        :fdescribe :fcontext :ffeature
        :shared_examples :shared_examples_for
        :shared_context
      }
    ...)
  ...)
PATTERN