Skip to content
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 11 commits into from
Jun 22, 2023
Merged

new cop for enum starting value #57

merged 11 commits into from
Jun 22, 2023

Conversation

faridanoosheh
Copy link
Contributor

It is in response to this issue: #56

@Bhacaz Bhacaz self-requested a review June 20, 2023 12:04
Copy link
Collaborator

@Bhacaz Bhacaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entry in the changelog should be under # main

CHANGELOG.md Outdated Show resolved Hide resolved
@faridanoosheh faridanoosheh requested a review from Bhacaz June 21, 2023 11:58
Copy link
Collaborator

@Bhacaz Bhacaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some error when trying it our project. Sometimes, the enum value is a constant, example

undefined method `value' for s(:const, nil, :SomeConstant):RuboCop::AST::ConstNode
/Users/jfbastien/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/bundler/gems/rubocop-petal-f6edf46de432/lib/rubocop/cop/rails/enum_starting_value.rb:35:in `block in start_with_zero?'
/Users/jfbastien/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/bundler/gems/rubocop-petal-f6edf46de432/lib/rubocop/cop/rails/enum_starting_value.rb:34:in `any?'
/Users/jfbastien/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/bundler/gems/rubocop-petal-f6edf46de432/lib/rubocop/cop/rails/enum_starting_value.rb:34:in `start_with_zero?'
/Users/jfbastien/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/bundler/gems/rubocop-petal-f6edf46de432/lib/rubocop/cop/rails/enum_starting_value.rb:30:in `on_send'

it 'expects an offense' do
expect_offense(<<~RUBY)
class MyModel
enum my_enum: {state1: 0, state2: 2}
Copy link
Collaborator

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

enum my_enum: {state1: 1, state2: 0}

@faridanoosheh faridanoosheh requested a review from Bhacaz June 22, 2023 13:11
Copy link
Collaborator

@Bhacaz Bhacaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks

@Bhacaz Bhacaz merged commit a2ecd2d into main Jun 22, 2023
@Bhacaz Bhacaz deleted the CopForEnumStartingValue branch June 22, 2023 13:25
@Bhacaz Bhacaz linked an issue Jun 22, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum values start with 1
2 participants