@@ -24,7 +24,10 @@ class EnumSyntax < Base
2424 MSG = 'Enum defined with keyword arguments in `%<enum>s` enum declaration. Use positional arguments instead.'
2525 MSG_OPTIONS = 'Enum defined with deprecated options in `%<enum>s` enum declaration. Remove the `_` prefix.'
2626 RESTRICT_ON_SEND = %i[ enum ] . freeze
27- OPTION_NAMES = %w[ prefix suffix scopes default ] . freeze
27+
28+ # From https://github.com/rails/rails/blob/v7.2.1/activerecord/lib/active_record/enum.rb#L231
29+ OPTION_NAMES = %w[ prefix suffix scopes default instance_methods ] . freeze
30+ UNDERSCORED_OPTION_NAMES = OPTION_NAMES . map { |option | "_#{ option } " } . freeze
2831
2932 def_node_matcher :enum? , <<~PATTERN
3033 (send nil? :enum (hash $...))
@@ -34,14 +37,6 @@ class EnumSyntax < Base
3437 (send nil? :enum $_ ${array hash} $_)
3538 PATTERN
3639
37- def_node_matcher :enum_values , <<~PATTERN
38- (pair $_ ${array hash})
39- PATTERN
40-
41- def_node_matcher :enum_options , <<~PATTERN
42- (pair $_ $_)
43- PATTERN
44-
4540 def on_send ( node )
4641 check_and_correct_keyword_args ( node )
4742 check_enum_options ( node )
@@ -52,20 +47,17 @@ def on_send(node)
5247 def check_and_correct_keyword_args ( node )
5348 enum? ( node ) do |pairs |
5449 pairs . each do |pair |
55- key , values = enum_values ( pair )
56- next unless key
50+ next if option_key? ( pair )
5751
58- correct_keyword_args ( node , key , values , pairs [ 1 ..] )
52+ correct_keyword_args ( node , pair . key , pair . value , pairs [ 1 ..] )
5953 end
6054 end
6155 end
6256
6357 def check_enum_options ( node )
6458 enum_with_options? ( node ) do |key , _ , options |
6559 options . children . each do |option |
66- name , = enum_options ( option )
67-
68- add_offense ( name , message : format ( MSG_OPTIONS , enum : enum_name_value ( key ) ) ) if name . source [ 0 ] == '_'
60+ add_offense ( option . key , message : format ( MSG_OPTIONS , enum : enum_name_value ( key ) ) ) if option_key? ( option )
6961 end
7062 end
7163 end
@@ -107,6 +99,10 @@ def enum_name(elem)
10799 end
108100 end
109101
102+ def option_key? ( pair )
103+ UNDERSCORED_OPTION_NAMES . include? ( pair . key . source )
104+ end
105+
110106 def correct_options ( options )
111107 corrected_options = options . map do |pair |
112108 name = if pair . key . source [ 0 ] == '_'
0 commit comments