-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabricator_shorthand.rb
52 lines (48 loc) · 1.26 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
# 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
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 |identifier|
add_offense(node, message: message(identifier))
end
end
private
def message(identifier)
"Use the fabricator shorthand: `fab!(:#{identifier})`"
end
end
end
end
end