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

[Fix #1343] False negatives for Rails/EnumSyntax #1348

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

Earlopain
Copy link
Contributor

Fix #1343. I don't believe there is any issue with just looking at all value types. It just translates foo: bar into :foo, bar.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@@ -107,6 +96,10 @@ def enum_name(elem)
end
end

def option_key?(pair)
pair.key.source[0] == '_'
Copy link
Member

@koic koic Sep 1, 2024

Choose a reason for hiding this comment

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

Hmm, wouldn't this approach result in false negatives for cases like enum _key, {val1: 0, val2: 1}?
It might be more appropriate to exclude only the key names that are meaningful as options, such as _prefix, _suffix, _scopes, _default, and _instance_methods. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess that's right, yeah.

I wonder if anyone would actually have a db schema where this would matter but it's easy to support and rails doesn't have a problem with it.

@@ -25,6 +25,7 @@ class EnumSyntax < Base
MSG_OPTIONS = 'Enum defined with deprecated options in `%<enum>s` enum declaration. Remove the `_` prefix.'
RESTRICT_ON_SEND = %i[enum].freeze
OPTION_NAMES = %w[prefix suffix scopes default].freeze
Copy link
Member

Choose a reason for hiding this comment

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

The following could be added at this point :-)

Suggested change
OPTION_NAMES = %w[prefix suffix scopes default].freeze
OPTION_NAMES = %w[prefix suffix scopes default instance_methods].freeze

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. Looks like I somehow linked an older commit in the other PR.

I've added it. There is now also validate but that only works with the new syntax and shouldn't be considered here.

@@ -24,7 +24,10 @@ class EnumSyntax < Base
MSG = 'Enum defined with keyword arguments in `%<enum>s` enum declaration. Use positional arguments instead.'
MSG_OPTIONS = 'Enum defined with deprecated options in `%<enum>s` enum declaration. Remove the `_` prefix.'
RESTRICT_ON_SEND = %i[enum].freeze
OPTION_NAMES = %w[prefix suffix scopes default].freeze

# From https://github.com/rails/rails/blob/5c0b7496ab32c25c80f6d1bdc8b32ec6f75ce1e4/activerecord/lib/active_record/enum.rb#L222
Copy link
Member

Choose a reason for hiding this comment

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

I like this comment.
Can you tweak to the following? This clarifies the reference version.

Suggested change
# From https://github.com/rails/rails/blob/5c0b7496ab32c25c80f6d1bdc8b32ec6f75ce1e4/activerecord/lib/active_record/enum.rb#L222
# From https://github.com/rails/rails/blob/v7.2.1/activerecord/lib/active_record/enum.rb#L231

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll do tag references for these in the future, that's much better 👍

@koic
Copy link
Member

koic commented Sep 3, 2024

I apologize for not explaining myself clearly.

If you are going to address _instance_methods in this PR, please squash the commits into one.

Or, If it seems better to separate the commits due to the user impact, it might also make sense to have a separate changelog entry, so it would be better to open it as a separate PR.

Could you handle it in the way you prefer?

I don't believe there is any issue with just looking at all value types.
It just translates `foo: bar` into `:foo, bar`.

Additionally, add `instance_methods` as a rails option. Not doing this would
result autocorrect treating it as a enum column
@Earlopain
Copy link
Contributor Author

I squashed, seems fine to just do here. This only really matters with the previous change anyways, since it now specifically looks at what the option key looks like.

@Earlopain Earlopain changed the title [Fix #1343] False negatives for `Rails/EnumSyntax´ [Fix #1343] False negatives for Rails/EnumSyntax Sep 3, 2024
@koic koic merged commit b59cecf into rubocop:master Sep 3, 2024
14 checks passed
@koic
Copy link
Member

koic commented Sep 3, 2024

Thanks!

@Earlopain Earlopain deleted the enum-syntax-non-literal branch September 3, 2024 15:42
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.

Rails/EnumSyntax fails to detect non-literal mapping Hashes
2 participants