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

[#11076] Add autocorrect for hash in Lint/LiteralInInterpolation #11475

Merged

Conversation

KessaPassa
Copy link
Contributor

@KessaPassa KessaPassa commented Jan 20, 2023

Fixes #11076.
This PR add autocorrect to Lint/LiteralInInterpolation for hash type.

Replace this text with a summary of the changes in your PR.
The more detailed you are, the better.


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.

@KessaPassa KessaPassa changed the title [#11076] make hash type autocorrect with test [#11076] Add autocorrect for hash in Lint/LiteralInInterpolation Jan 20, 2023
@KessaPassa KessaPassa force-pushed the feat/litelal-in-interpolation-for-hash branch 9 times, most recently from ba2d48f to 0c65b94 Compare January 27, 2023 04:08

def autocorrected_value_in_hash_for_symbol(node)
# TODO: We need to detect symbol unacceptable names more correctly
if / |"|'/.match?(node.value.to_s)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A gem unparser provides more simple (and better) way to generate code strings for autocorrect result.
Should we add it to the list of dependencies?

Suggested change
if / |"|'/.match?(node.value.to_s)
Unparser.unparse(node).gsub('"') { '\"' }

Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably that'd be an overkill for a single usage. I'm always wary of adding additional runtime deps.

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 aimed to separate codes to unparse hash from the rubocop to make its project scope more clear.
But after more research I realized that the unparser gem sometimes generate results not exact same to Ruby's one.
Thus I agree to cancel this suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

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

@KessaPassa Unparser only guarantess: parse(code) == parse(unparse(parse(code))), so it guarantees you generate concrete syntax that if parsed produces the original AST, not the same concrete syntax. If you knew the concrete syntax already there is no need to unparse in the first place ;)

@KessaPassa KessaPassa marked this pull request as ready for review January 30, 2023 04:42
@@ -114,6 +121,72 @@
it_behaves_like('literal interpolation', '%I[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[s1 s2]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '%i[ s1 s2 ]', '[\"s1\", \"s2\"]')
it_behaves_like('literal interpolation', '{"a" => "b"}', '{\"a\"=>\"b\"}')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it might be nice to have the hash-related tests in their own section. The tests for this cop are already quite messy and we should try to improve this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bbatsov thank you!
I created sctions for each type.
How about this change?
c9fdcdc

@KessaPassa KessaPassa force-pushed the feat/litelal-in-interpolation-for-hash branch from c9fdcdc to c6027b6 Compare February 24, 2023 04:51
@bbatsov bbatsov merged commit cde4bc2 into rubocop:master Feb 27, 2023
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.

Lint/LiteralInInterpolation does incorrect replacement for hashes
3 participants