-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new cop for enum starting value #57
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f42f8c
new cop for enum starting value
faridanoosheh 456af55
fixes rubocop issues + info in default.yml and changelog
faridanoosheh 3ccf7e9
does not change CHANGELOG
faridanoosheh 33945ff
Revert "does not change CHANGELOG"
faridanoosheh f6edf46
removes version
faridanoosheh 6725eb4
only test enum with hashes + more tests
faridanoosheh 2d9702b
s
kbqkb:qkbkbq!
kbkbkbkb:kbýaq:qqqkbkbkbkd
faridanoosheh 268e7c1
:q!
faridanoosheh 23380df
resolves rubocop issues
faridanoosheh 67b42f8
fix tests
faridanoosheh c623b9f
another cop fail
faridanoosheh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Prevent the user to start from Zero with an enum. | ||
# When using a wrong value like .where(state: :patato) let Rails + MySQL do a WHERE state = 0. | ||
# It will match nothing since no record will have a 0 value. | ||
# | ||
# # bad | ||
# enum my_enum: {apple: 0, bannana: 1} | ||
# | ||
# # good | ||
# enum my_enum: {apple: 1, banana: 2} | ||
|
||
class EnumStartingValue < Base | ||
MSG = 'Prefer starting from `1` instead of `0` with `enum`.' | ||
|
||
def_node_matcher :enum?, <<~PATTERN | ||
(send nil? :enum (hash ...)) | ||
PATTERN | ||
|
||
def_node_matcher :enum_attributes, <<~PATTERN | ||
(send nil? :enum (:hash (:pair (...)$(...) )...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless enum? node | ||
|
||
add_offense(node) if start_with_zero?(enum_attributes(node)) | ||
end | ||
|
||
def start_with_zero?(node) | ||
node.children.any? do |child| | ||
value = child.value | ||
value.type == :int && value.value.zero? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::EnumStartingValue, :config do | ||
context 'without an enum' do | ||
it 'expects no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
puts 'some stuff' | ||
|
||
# Declares a model without enum | ||
class MyModel | ||
has_many :some_other_thing | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when starts from zero' do | ||
context 'without prefix' do | ||
it 'expects an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel | ||
enum my_enum: {state1: 0, state2: 2} | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer starting from `1` instead of `0` with `enum`. | ||
end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
class MyModel | ||
enum my_enum: { | ||
^^^^^^^^^^^^^^^ Prefer starting from `1` instead of `0` with `enum`. | ||
state1: 0, | ||
state2: 2 | ||
} | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with prefix' do | ||
it 'expects an offense' do | ||
expect_offense(<<~RUBY) | ||
class MyModel | ||
enum my_enum: {state1: 0, state2: 2}, _suffix: false | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer starting from `1` instead of `0` with `enum`. | ||
end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
class MyModel | ||
enum my_enum: { | ||
^^^^^^^^^^^^^^^ Prefer starting from `1` instead of `0` with `enum`. | ||
state1: 0, | ||
state2: 2 | ||
}, _prefix: true | ||
end | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'when starts from a non zero' do | ||
it 'expects no offense' do | ||
expect_no_offenses(<<~RUBY) | ||
class MyModel | ||
enum my_enum: {state1: 1, state2: 2}, _prefix: true | ||
end | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
class MyModel | ||
enum my_enum: { | ||
state1: 2, | ||
state2: 3 | ||
} | ||
end | ||
RUBY | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may sound weird, but can you add a specs to that check that no value should be 0.
Like