-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix #1238] Add Rails/EnumKeywordArgs cop
Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3.
- Loading branch information
1 parent
ada5c28
commit 3437956
Showing
4 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Looks for enums written with keyword arguments. | ||
# | ||
# Defining enums with keyword arguments is deprecated | ||
# and will be removed in Rails 7.3. | ||
# | ||
# Positional arguments should be used instead: | ||
# | ||
# @example | ||
# # bad | ||
# enum status: { active: 0, archived: 1 }, _prefix: true | ||
# | ||
# # good | ||
# enum :status, { active: 0, archived: 1 }, prefix: true | ||
# | ||
class EnumKeywordArgs < Base | ||
extend AutoCorrector | ||
|
||
MSG_ARGS = 'Enum defined with keyword arguments found in `%<enum>s` enum declaration. Use positional arguments instead.' | ||
MSG_OPTS = 'Enum defined with deprecated options found in `%<enum>s` enum declaration. Use options without the `_` prefix.' | ||
RESTRICT_ON_SEND = %i[enum].freeze | ||
|
||
def_node_matcher :enum?, <<~PATTERN | ||
(send nil? :enum (hash $...)) | ||
PATTERN | ||
|
||
def_node_matcher :enum_with_keyword_args?, <<~PATTERN | ||
(send nil? :enum $_ ${array hash} $_) | ||
PATTERN | ||
|
||
def_node_matcher :enum_values, <<~PATTERN | ||
(pair $_ ${array hash}) | ||
PATTERN | ||
|
||
def_node_matcher :enum_options, <<~PATTERN | ||
(pair $_ $_) | ||
PATTERN | ||
|
||
def on_send(node) | ||
enum?(node) do |pairs| | ||
pairs.each do |pair| | ||
key, array = enum_values(pair) | ||
if key | ||
add_offense(array, message: format(MSG_ARGS, enum: enum_name(key))) do |corrector| | ||
corrected_options = pairs[1..].map do |pair| | ||
name = if pair.key.source[0] == "_" | ||
pair.key.source[1..] | ||
else | ||
pair.key.source | ||
end | ||
|
||
"#{name}: #{pair.value.source}" | ||
end.join(", ") | ||
corrected_options = ", " + corrected_options unless corrected_options.empty? | ||
|
||
corrector.replace(node, "enum #{source(key)}, #{array.source}#{corrected_options}") | ||
end | ||
end | ||
end | ||
end | ||
|
||
enum_with_keyword_args?(node) do |key, _, options| | ||
options.children.each do |option| | ||
name, value = enum_options(option) | ||
if name.source[0] == "_" | ||
add_offense(name, message: format(MSG_OPTS, enum: enum_name(key))) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::EnumKeywordArgs, :config do | ||
context 'when keyword arguments are used' do | ||
context 'with %i[] syntax' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum status: { active: 0, archived: 1 }, _prefix: true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments found in `status` enum declaration. Use positional arguments instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with options prefixed with `_`' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum :status, { active: 0, archived: 1 }, _prefix: true, _suffix: true | ||
^^^^^^^ Enum defined with deprecated options found in `status` enum declaration. Use options without the `_` prefix. | ||
^^^^^^^ Enum defined with deprecated options found in `status` enum declaration. Use options without the `_` prefix. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the enum name is a string' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum "status" => { active: 0, archived: 1 } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments found in `status` enum declaration. Use positional arguments instead. | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when the enum name is not a literal' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
enum KEY => { active: 0, archived: 1 } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments found in `KEY` enum declaration. Use positional arguments instead. | ||
RUBY | ||
end | ||
end | ||
|
||
it 'autocorrects' do | ||
expect_offense(<<~RUBY) | ||
enum status: { active: 0, archived: 1 } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments found in `status` enum declaration. Use positional arguments instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
enum :status, { active: 0, archived: 1 } | ||
RUBY | ||
end | ||
|
||
it 'autocorrects options too' do | ||
expect_offense(<<~RUBY) | ||
enum status: { active: 0, archived: 1 }, _prefix: true, _suffix: true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Enum defined with keyword arguments found in `status` enum declaration. Use positional arguments instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
enum :status, { active: 0, archived: 1 }, prefix: true, suffix: true | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when positional arguments are used' do | ||
it 'does not register an offense' do | ||
expect_no_offenses('enum :status, { active: 0, archived: 1 }, prefix: true') | ||
end | ||
end | ||
end |