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 #11802] Improve handling of [] and () with percent symbol arrays #12037

Merged

Conversation

jasondoc3
Copy link
Contributor

Addresses #11802, but also accounts for ()


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.

@@ -8,7 +8,7 @@ class UndefinedConfig < Base
ALLOWED_CONFIGURATIONS = %w[
Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude
].freeze
RESTRICT_ON_SEND = %i[[] fetch].freeze
RESTRICT_ON_SEND = [:[], :fetch].freeze
Copy link
Member

Choose a reason for hiding this comment

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

If bracket do not contain spaces, they should not be changed.

So, only the following case that need escaping can be changed from percent literal style to bracket style:

%i[[ ]] #=> [:"[", :"]"]

This case where brackets do not contain spaces should not be changed:

%i[[]]  #=> [:[]]

That is, it must be checked for spaces in brackets. Can you update the entire changes in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@koic I think that makes sense to me!

But one thing that I notice is that while %i[[] one] does not currently raise an offense, rubocop still autocorrects [:[], :one] to %i[\[\] one], which feels inconsistent 🤔 Any thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

When EnforcedStyle: percent, %i[[] one] (no spaces in the brackets) will need to adjust the implementation if necessary to be accepted.

@jasondoc3 jasondoc3 force-pushed the consider_brackets_as_bad_for_style_symbol_array branch from f9fc6e5 to 1920852 Compare July 11, 2023 21:36
# %i[foo\ bar baz\ quux]
#
# # bad (contains [] with spaces)
# %i[foo\ \[ \]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd remove the escaped space after foo to make this example clearer.

# %i[foo\ \[ \]]
#
# # bad (contains () with spaces)
# %i(foo\ \( \))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd remove the escaped space after foo to make this example clearer.

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 21, 2023

@jasondoc3 ping :-)

@jasondoc3 jasondoc3 force-pushed the consider_brackets_as_bad_for_style_symbol_array branch from 31b5b8b to 636cff8 Compare July 21, 2023 16:32
@jasondoc3
Copy link
Contributor Author

@bbatsov Removed the escaped spaces! I think I addressed the concerns of @koic, but if not I can give another look 👀

@@ -57,13 +66,24 @@ def on_array(node)

private

def symbols_contain_spaces?(node)
def complex_content?(node)
delimiters = ['[', ']', '(', ')']
Copy link
Member

Choose a reason for hiding this comment

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

@jasondoc3 It could be a constant instead of a variable :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@jasondoc3 jasondoc3 force-pushed the consider_brackets_as_bad_for_style_symbol_array branch from 636cff8 to 80acfbd Compare July 21, 2023 18:03
@koic koic merged commit 765ed7d into rubocop:master Jul 21, 2023
28 checks passed
@koic
Copy link
Member

koic commented Jul 21, 2023

Thanks!

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.

None yet

3 participants