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

Add Style/HashNewKeywordArguments cop #11507

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

Conversation

r7kamura
Copy link
Contributor

I have heard that future versions of Ruby are likely to deprecate the ability to pass default values with keyword arguments to Hash.new.

So I thought it would be a good idea to prepare a Cop that detects such usage and autocorrects it while it is still available. Since the signature is originally Hash.new(default_value = nil), there should be no problem deprecating the keyword arguments.


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.

@r7kamura
Copy link
Contributor Author

r7kamura commented Jan 26, 2023

I am a little concerned about whether Style is the correct department. Semantically, Lint seems to be more correct, but I chose Style for now because I think Lint department is the place to put the most urgent Cop.

@r7kamura r7kamura force-pushed the feature/hash-new-keyword-arguments branch from be8c31e to 89163a4 Compare January 26, 2023 10:59
Comment on lines +43 to +45
def keyword_arguments?(argument)
argument.hash_type? && !argument.source.lstrip.start_with?('{')
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there any other good way to inspect keyword arguments?

Copy link
Member

Choose a reason for hiding this comment

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

It seems that kwargs is rounded to hash in rubocop-ast.
https://github.com/rubocop/rubocop-ast/blob/57bc6792ed83dfb2890ed1ce1eebc2580fd83aa2/lib/rubocop/ast/builder.rb#L64

I don't know if this is better, but would it be better to define a KwargsNode to replace it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the emit_kwargs option is not enabled in RuboCop's parser, I don't think the kwargs type is actually passed there, but changing this option might have too much impact.

@koic
Copy link
Member

koic commented Jan 26, 2023

Interesting. However, it is too early to judge the direction of this cop force yet. So, it should be decided after https://bugs.ruby-lang.org/issues/19236 is settled. Let's come back to this PR once https://bugs.ruby-lang.org/issues/19236 is closed.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 30, 2023

Let's mark this PR as WIP in the mean time.

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

Successfully merging this pull request may close these issues.

None yet

4 participants