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
Incorrect auto-correction for Style/StringLiterals
cop
#11123
Comments
Style/StringLiterals
cop
Had a look at the source code and it seems to me that the problem is somewhere here: rubocop/lib/rubocop/cop/util.rb Line 125 in e2f5f20
Switching the order of the original_string = "{\"[\\\"*\\\"]\"".freeze
irb(main):038:0> original_string.gsub('\"', '"').gsub('\\') { '\\\\' }
=> "{\"[\"*\"]\""
irb(main):039:0> original_string.gsub('\\') { '\\\\' }.gsub('\"', '"')
=> "{\"[\\\"*\\\"]\"" but not sure if it doesn't break anything else. Will clone the project and run the test suite to see. |
Running the specs - this breaks a few examples in |
@konieczkow if you want to open a PR for this it seems to me the issue is actually that Indeed, rubocop/lib/rubocop/cop/correctors/string_literal_corrector.rb Lines 13 to 15 in 7007fb1
While rubocop/lib/rubocop/cop/style/quoted_symbols.rb Lines 72 to 77 in 7007fb1
It seems that on top of your fix, changing those lines to using # Using eval to replicate the behavior of `#str_content`
str = if hash_colon_key?(node)
correct_quotes(eval(node.source))
else
# strip leading `:`
":#{correct_quotes(eval(node.source[1..]))}"
end But I'm not sure whether using |
…yle/StringLiterals [Fix #11123 ] Fix auto correction bug for Style/StringLiterals
Hi there, thanks to all the people working on Rubocop.
I noticed a weird behaviour when applying an autocorrection to the following code.
This gets correctly marked as violating
Style/StringLiterals
but when I run rubocop with-a
(which I assume means it will correct all the cops as long as it is safe to do so) I get this as the output:It seems to me that the autocorrection is too greedy and swollows too many escape characters?
Expected behavior
I would expect rubocop to either correctly escape the code to:
or it should say that the cop is unsafe and can be used with
-A
, instead of-a
.Actual behavior
Autocorrecting:
produces:
Steps to reproduce the problem
Create an empty ruby file, add the following piece of code to it:
run:
check the file again.
RuboCop version
The text was updated successfully, but these errors were encountered: