-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabricator_shorthand.rb
62 lines (57 loc) · 1.53 KB
/
fabricator_shorthand.rb
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
module RuboCop
module Cop
module Discourse
# When fabricating a record without custom attributes, we can use the
# fabricator shorthand as long as the identifier matches the fabricator
# name.
#
# @example
#
# # bad
# fab!(:user) { Fabricate(:user) }
#
# # good
# fab!(:user)
#
# When using custom attributes or the identifier doesn't match, the
# shorthand can't be used.
#
# @example
#
# # good
# fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
#
# # good
# fab!(:another_user) { Fabricate(:user) }
class FabricatorShorthand < Base
extend AutoCorrector
include IgnoredNode
def_node_matcher :offending_fabricator?, <<-MATCHER
(block
$(send nil? :fab!
(sym $_identifier))
(args)
(send nil? :Fabricate
(sym $_identifier)))
MATCHER
def on_block(node)
offending_fabricator?(node) do |expression, _identifier|
add_offense(
node,
message: message(expression.source)
) do |corrector|
next if part_of_ignored_node?(node)
corrector.replace(node, expression.source)
end
ignore_node(node)
end
end
private
def message(expression)
"Use the fabricator shorthand: `#{expression}`"
end
end
end
end
end