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

Let ConsistentParenthesesStyle :omit_parentheses ignore trait + omitted hash value #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thejonroberts
Copy link

Fix #135

When ConsistentParenthesesStyle is :omit_parentheses, we ignore factory calls that have an omitted hash value (ruby 3.1 shorthand syntax). Parentheses are required if the arguments end with the key of the omitted value. (e.g. create(:user, name:))

However, if a trait was used, (e.g. create(:user, :trait, name:)) omit_hash_value? matcher did not match it, and thus it would be an offense and corrected to remove the parentheses - potentially causing a syntax error.

This change adds a wildcard to the omit_hash_value? matcher to allow any number of symbols at the start of the factory call (before the hash values).


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • [ ] Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@thejonroberts thejonroberts requested a review from a team as a code owner November 21, 2024 04:57
@@ -79,7 +79,7 @@ class ConsistentParenthesesStyle < ::RuboCop::Cop::Base
def_node_matcher :omit_hash_value?, <<~PATTERN
(send
#factory_call? %FACTORY_CALLS
{sym str send lvar}
{sym str send lvar} _*
Copy link
Author

Choose a reason for hiding this comment

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

First time working with AST. So there may be some implications here that I am missing. The intention is to allow any number of traits to be used before the hash values in the calls.

@pirj pirj force-pushed the 135-parentheses-style-hash-syntax-shorthand branch from 1ceefd7 to 8fea49c Compare November 21, 2024 08:31
When ConsistentParenthesesStyle is :omit_parentheses, we ignore factory
calls that have an omitted hash value (ruby 3.1 shorthand syntax).
Parentheses are required if the arguments end with the key of the
omitted value. (e.g. `create(:user, name:)`)
However, if a trait was used, (e.g. `create(:user, :trait, name:)`)
omit_hash_value? matcher did not match it, and thus would remove the
parentheses, potentially causing a syntax error.

This change adds a wildcard to the omit_hash_value? matcher to allow
any number of symbols/traits at the start of the pattern
(before the hash values).

see rubocop#135
@pirj pirj force-pushed the 135-parentheses-style-hash-syntax-shorthand branch from 8fea49c to 45968e9 Compare November 21, 2024 08:38
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Just perfect, thank you! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants