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 new Style/StringConcatenation cop #8378

Merged
merged 1 commit into from Jul 23, 2020

Conversation

fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Jul 21, 2020

https://rubystyle.guide/#string-interpolation

# bad
email_with_name = user.name + ' <' + user.email + '>'

# good
email_with_name = "#{user.name} <#{user.email}>"

Closes #5067

  1. Another good name for this cop, I think, is StringConcatenation.
  2. Made it Safe: false, because there can be very rare cases where it can give false positives.
  3. It may be also a good idea to add some config that indicates after which amount of terms to add an offense.

@@ -39,7 +39,7 @@ def move_comment(eol_comment:, node:, corrector:)

text = eol_comment.loc.expression.source
corrector.insert_before(node,
text + "\n" + (' ' * node.loc.keyword.column))
"#{text}\n#{(' ' * node.loc.keyword.column)}")
Copy link
Contributor

@marcandre marcandre Jul 21, 2020

Choose a reason for hiding this comment

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

Would be nice if the autocorrection dropped those (), or maybe should be done by another cop?

RUBY

expect_correction(<<~RUBY)
users = ("\#{user.name} \#{user.email}") * 5
Copy link
Contributor

@marcandre marcandre Jul 21, 2020

Choose a reason for hiding this comment

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

Would be nice to drop these outer () too, but maybe a different cop should do this?

@marcandre
Copy link
Contributor

Great PR (as usual 😆)

config/default.yml Outdated Show resolved Hide resolved
@bbatsov
Copy link
Collaborator

bbatsov commented Jul 21, 2020

Another good name for this cop, I think, is StringConcatenation.

I think I like this name better. Might also be a good idea to have the ability to autocorrect using format, but that's not a big deal.

@fatkodima fatkodima changed the title Add new Style/PreferInterpolation cop Add new Style/StringConcatenation cop Jul 21, 2020
@fatkodima
Copy link
Contributor Author

Changed name to Style/StringConcatenation.

Would be nice to drop these outer () too, but maybe a different cop should do this?

Yes, to not complicate the actual implementation and also this is needed in many other cops, so I think it should be better handled by a different cop (named like Style/RedundantParentheses, for example). I will try to create one in the future, if no one gets ahead of me.

@marcandre
Copy link
Contributor

marcandre commented Jul 21, 2020

I think it should be better handled by a different cop (named like Style/RedundantParentheses, for example). I will try to create one in the future, if no one gets ahead of me.

Agreed. I created #8379 to be sure we don't forget 👍

@bbatsov bbatsov merged commit 7795df4 into rubocop:master Jul 23, 2020
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.

[New Cop] Style/PreferInterpolation
3 participants