blob: 534a145b13ade5069b9667785416e7688b687ac7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# frozen_string_literal: true
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
module FactoryBot
class StrategyInCallback < RuboCop::Cop::Base
include RuboCop::FactoryBot::Language
MSG = 'Prefer inline `association` over `%{type}`. ' \
'See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories'
FORBIDDEN_METHODS = %i[build build_list build_stubbed build_stubbed_list create create_list].freeze
def_node_matcher :forbidden_factory_usage, <<~PATTERN
(block
(send nil? { :after :before } (sym _strategy))
_args
` # in all descandents
(send
{ nil? #factory_bot? }
${ #{FORBIDDEN_METHODS.map(&:inspect).join(' ')} }
(sym _factory_name)
...
)
)
PATTERN
RESTRICT_ON_SEND = FORBIDDEN_METHODS
def on_send(node)
parent = node.each_ancestor(:block).first
strategy = forbidden_factory_usage(parent)
return unless strategy
add_offense(node, message: format(MSG, type: strategy))
end
end
end
end
end
end
|