forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_hash.rb
More file actions
98 lines (85 loc) · 2.47 KB
/
enum_hash.rb
File metadata and controls
98 lines (85 loc) · 2.47 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Looks for enums written with array syntax.
#
# When using array syntax, adding an element in a
# position other than the last causes all previous
# definitions to shift. Explicitly specifying the
# value for each key prevents this from happening.
#
# @example
# # bad
# enum :status, [:active, :archived]
#
# # good
# enum :status, { active: 0, archived: 1 }
#
# # bad
# enum status: [:active, :archived]
#
# # good
# enum status: { active: 0, archived: 1 }
#
class EnumHash < Base
extend AutoCorrector
MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. Use hash syntax instead.'
RESTRICT_ON_SEND = %i[enum].freeze
def_node_matcher :enum_with_array?, <<~PATTERN
(send nil? :enum $_ ${array} ...)
PATTERN
def_node_matcher :enum_with_old_syntax?, <<~PATTERN
(send nil? :enum (hash $...))
PATTERN
def_node_matcher :array_pair?, <<~PATTERN
(pair $_ $array)
PATTERN
def on_send(node)
target_rails_version >= 7.0 && enum_with_array?(node) do |key, array|
add_offense(array, message: message(key)) do |corrector|
corrector.replace(array, build_hash(array))
end
end
enum_with_old_syntax?(node) do |pairs|
pairs.each do |pair|
key, array = array_pair?(pair)
next unless key
add_offense(array, message: message(key)) do |corrector|
corrector.replace(array, build_hash(array))
end
end
end
end
private
def message(key)
format(MSG, enum: enum_name(key))
end
def enum_name(key)
case key.type
when :sym, :str
key.value
else
key.source
end
end
def source(elem)
case elem.type
when :str
elem.value.dump
when :sym
elem.value.inspect
else
elem.source
end
end
def build_hash(array)
hash = array.children.each_with_index.map do |elem, index|
"#{source(elem)} => #{index}"
end.join(', ')
"{#{hash}}"
end
end
end
end
end