Skip to content

Conversation

@Earlopain
Copy link
Contributor

Fix #14502

This only escapes # when it looks like interpolation, not all the time. If it turns out to not be interpolation after all, Style/RedundantStringEscape will take care of it.


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.

Comment on lines 308 to 328
it 'registers an offense for `#$`' do
expect_offense(<<~'RUBY')
"foo" + '#$bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<~'RUBY')
"foo\#$bar"
RUBY
end

it 'registers an offense for `#@`' do
expect_offense(<<~'RUBY')
"foo" + '#@bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<~'RUBY')
"foo\#@bar"
RUBY
end

it 'registers an offense for `#@@`' do
expect_offense(<<~'RUBY')
"foo" + '#@@bar'
^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY

expect_correction(<<~'RUBY')
"foo\#@@bar"
RUBY
end
Copy link
Member

@koic koic Sep 3, 2025

Choose a reason for hiding this comment

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

Good test cases. That's my two cents. Can you arrange them by scope, starting with the narrowest and ending with the widest?

Suggested change
it 'registers an offense for `#$`' do
expect_offense(<<~'RUBY')
"foo" + '#$bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#$bar"
RUBY
end
it 'registers an offense for `#@`' do
expect_offense(<<~'RUBY')
"foo" + '#@bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#@bar"
RUBY
end
it 'registers an offense for `#@@`' do
expect_offense(<<~'RUBY')
"foo" + '#@@bar'
^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#@@bar"
RUBY
end
it 'registers an offense for `#@`' do
expect_offense(<<~'RUBY')
"foo" + '#@bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#@bar"
RUBY
end
it 'registers an offense for `#@@`' do
expect_offense(<<~'RUBY')
"foo" + '#@@bar'
^^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#@@bar"
RUBY
end
it 'registers an offense for `#$`' do
expect_offense(<<~'RUBY')
"foo" + '#$bar'
^^^^^^^^^^^^^^^ Prefer string interpolation to string concatenation.
RUBY
expect_correction(<<~'RUBY')
"foo\#$bar"
RUBY
end

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, I moved globals to be last.

…ion` when a single-quoted string contains interpolation

This only escapes `#` when it looks like interpolation, not all the time.
If it turns out to not be interpolation after all, `Style/RedundantStringEscape` will take care of it.
@Earlopain Earlopain force-pushed the string-concant-single-quotes-interpolation branch from 8cdf802 to 996eeec Compare September 3, 2025 10:11
@bbatsov bbatsov merged commit ca1b813 into rubocop:master Sep 3, 2025
22 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 3, 2025

Thanks!

@Earlopain Earlopain deleted the string-concant-single-quotes-interpolation branch September 5, 2025 06:54
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/StringConcatenation doesn't respect single quotes when autocorrecting

3 participants