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

Prevent Lint/LiteralInInterpolation from removing necessary interpolation #8921

Merged

Conversation

knu
Copy link
Contributor

@knu knu commented Oct 22, 2020

%W and %I split the content into words before expansion treating each interpolation as a word component.

For example, the following array literal evaluates to ["-H", "Cookie: a=1; b=2", "https://example.com/"].

args = %W[-H #{"Cookie: a=1; b=2"} https://example.com/]

However, Lint/LiteralInInterpolation will wrongly auto-correct it to this:

args = %W[-H Cookie: a=1; b=2 https://example.com/]

Which gives a completely different set of elements: ["-H", "Cookie:", "a=1;", "b=2", "https://example.com/"].

This fix teaches the cop not to expand an interpolation when the expanded value contains a space character.

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@knu knu force-pushed the conditionally_keep_interpolation_in_word_literals branch 4 times, most recently from 87adeac to e69f287 Compare October 22, 2020 16:53
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 23, 2020

The proposed changes look good to me. I wonder if we shouldn't expand the cop's documentation a bit to make it clearer which are the cases that the cop would not touch.

CHANGELOG.md Outdated
@@ -4,6 +4,7 @@

### New features

* [#8921](https://github.com/rubocop-hq/rubocop/pull/8921): Prevent `Lint/LiteralInInterpolation` from removing necessary interpolation in `%W[]` and `%I[]` literals. ([@knu][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

That should go under "Bugs fixed".

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 fIxed it, and rebased this PR on the latest master.

…tion

%W and %I split the content into words before expansion treating each
interpolation as a word component.

For example, the following array literal evaluates to
`["-H", "Cookie: a=1; b=2", "https://example.com/"]`.

```ruby
args = %W[-H #{"Cookie: a=1; b=2"} https://example.com/]
```

However, Lint/LiteralInInterpolation will wrongly auto-correct it to
this:

```ruby
args = %W[-H Cookie: a=1; b=2 https://example.com/]
```

Which gives a completely different set of elements:
`["-H", "Cookie:", "a=1;", "b=2", "https://example.com/"]`.

This fix teaches the cop not to expand an interpolation when the
expanded value contains a space character.
@knu knu force-pushed the conditionally_keep_interpolation_in_word_literals branch from e69f287 to 2bb4490 Compare October 29, 2020 12:05
@bbatsov bbatsov merged commit 1bb5354 into rubocop:master Oct 29, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 29, 2020

Thanks!

@knu knu deleted the conditionally_keep_interpolation_in_word_literals branch October 29, 2020 13:22
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

2 participants