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

Style/StringConcatenation autocorrect doesn't properly escape double quotes when concatenated via single quotes #8985

Closed
joe-p opened this issue Nov 1, 2020 · 2 comments · Fixed by #9139
Labels
bug good first issue Easy task, suitable for newcomers to the project help wanted

Comments

@joe-p
Copy link

joe-p commented Nov 1, 2020

Expected Behavior

When Rubocop encounters '"' + "foo" + '"' it should be autocorrected to '"foo"'

Actual Behavior

When Rubocop 1.1.0 encounters '"' + "foo" + '"' it currently autocorrects to ""foo"" thus leading to a syntax error:

test.rb:3:3: C: [Corrected] Style/StringConcatenation: Prefer string interpolation to string concatenation.
p '"' + "foo" + '"'
  ^^^^^^^^^^^^^^^^^
test.rb:3:5: E: Lint/Syntax: unexpected token tIDENTIFIER
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
p ""foo""

Steps to reproduce the problem

Create a Ruby file with the following line and run rubocop -A in its directory
'"' + "foo" + '"'

RuboCop version

$ rubocop -V
1.1.0 (using Parser 2.7.2.0, rubocop-ast 1.1.0, running on ruby 2.7.2 x86_64-linux)

Note

This is a bug specific to double quotes in single quotes. "\"" + 'foo' + "\"" is properly autocorrected to '"foo"'.

@marcandre marcandre added bug good first issue Easy task, suitable for newcomers to the project help wanted labels Nov 1, 2020
@marcandre
Copy link
Contributor

Agreed.

It should also handle '"' + "'" and either correct using %Q{...} or not correct at all.

@mfbmina
Copy link
Contributor

mfbmina commented Nov 7, 2020

I couldn't reproduce on the master branch. Maybe #8893 or #8830 fixed it.

tejasbubane added a commit to tejasbubane/rubocop that referenced this issue Dec 2, 2020
…ting invalid ruby

When single quoted strings

Closes rubocop#8985
bbatsov pushed a commit that referenced this issue Dec 3, 2020
…valid ruby

When single quoted strings

Closes #8985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Easy task, suitable for newcomers to the project help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants