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

Preserve multiline semantics on Style/SymbolArray and Style/WordArray #10784

Merged
merged 1 commit into from Jul 7, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Jul 4, 2022

To fix #10455.

Example:

# Before
-%w(
- foo
- bar
- baz
-)
+["foo", "bar", "baz"]

# After
-%w(
- foo
- bar
- baz
-)
+[
+  "foo",
+  "bar",
+  "baz"
+]

To mimic the original whitespace semantics, I decided to extract three types of whitespace and reuse them in autocorrection.

# Before
"%w[\n  foo\n  bar\n  baz\n]\n"
#   ^^^^   ^^^^          ^^
#   |      |             `-- whitespace_trailing
#   |      `-- whitespace_between
#   `-- whitespace_leading

# After
"[\n  'foo',\n  'bar',\n  'baz'\n]\n"
# ^^^^      ^^^^      ^^^^     ^^
# |         |         |        `-- whitespace_trailing
# |         `------------ whitespace_between
# `-- whitespace_leading

Note that this commit also changes the offense message like this (on multiline case):

# Before
%w(
^^^ Use `[
  'foo',
  'bar'
]` for an array of words.
  foo
  bar
)

# After
%w(
^^^ Use an array literal `[...]` for an array of words.
  foo
  bar
)

This means that the auto-corrected code no longer appears in the offense message (on multiline case). I think this is a reasonable change because this format is already used for percent styles:

'Use %w or %W for an array of words.'`.


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.

@@ -39,7 +39,7 @@ class SymbolArray < Base
minimum_target_ruby_version 2.0

PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
ARRAY_MSG = 'Use `%<prefer>s` for an array of symbols.'
ARRAY_MSG = 'Use `[...]` for an array of symbols.'
Copy link
Member

Choose a reason for hiding this comment

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

Can you tweak the message?

Suggested change
ARRAY_MSG = 'Use `[...]` for an array of symbols.'
ARRAY_MSG = 'Use array brackets `[...]` for an array of symbols.'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, thank you for pointing that out. I too was afraid that this [...] might be a bit confusing.

@@ -44,7 +44,7 @@ class WordArray < Base
extend AutoCorrector

PERCENT_MSG = 'Use `%w` or `%W` for an array of words.'
ARRAY_MSG = 'Use `%<prefer>s` for an array of words.'
ARRAY_MSG = 'Use `[...]` for an array of words.'
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

Suggested change
ARRAY_MSG = 'Use `[...]` for an array of words.'
ARRAY_MSG = 'Use array brackets`[...]` for an array of words.'

@r7kamura r7kamura force-pushed the feature/preserve-array-line-break branch from 022fa36 to 7758a4c Compare July 4, 2022 09:22
…ray`

To fix rubocop#10455.

Note that this commit also changes the offense message like this:

```ruby
\# Before
%w(
^^^ Use `[
  'foo',
  'bar'
]` for an array of words.
  foo
  bar
)

\# After
%w(
^^^ Use `[...]` for an array of words.
  foo
  bar
)
```

This means that the auto-corrected code no longer appears in the offense message.
I think this is a reasonable change because the message to enforce the percent styles (`'Use `%w` or `%W` for an array of words.'`) is already using this format.
@r7kamura r7kamura force-pushed the feature/preserve-array-line-break branch from a3d6672 to 074f294 Compare July 4, 2022 21:24
@bbatsov bbatsov merged commit d1cb20e into rubocop:master Jul 7, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 7, 2022

Looks good. Thanks!

@r7kamura r7kamura deleted the feature/preserve-array-line-break branch July 7, 2022 20:13
ydah added a commit to ydah/rubocop-rails that referenced this pull request Oct 19, 2022
Similar issue: rubocop/rubocop#10784

Before
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b.to_f * 12.0` instead of `if a.present?
+  a
+else
+  b
+end`.
  a
else
  b
end
```

After
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b` instead of `if a.present? ... end`.
  a
else
  b.to_f + 12.0
end
```
ydah added a commit to ydah/rubocop-rails that referenced this pull request Oct 19, 2022
Similar issue: rubocop/rubocop#10784

Before
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b.to_f * 12.0` instead of `if a.present?
+  a
+else
+  b
+end`.
  a
else
  b
end
```

After
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b` instead of `if a.present? ... end`.
  a
else
  b
end
```
ydah added a commit to ydah/rubocop-rails that referenced this pull request Oct 20, 2022
Similar issue: rubocop/rubocop#10784

Before
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b.to_f * 12.0` instead of `if a.present?
+  a
+else
+  b
+end`.
  a
else
  b
end
```

After
```diff
if a.present?
+^^^^^^^^^^^^^ Use `a.presence || b` instead of `if a.present? ... end`.
  a
else
  b
end
```
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.

Style/{Symbol,Word}Array should preserve multiline semantics when autocorrecting
4 participants