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

Bug in StringLiteralsInInterpolation #1415

Closed
bquorning opened this issue Nov 2, 2014 · 8 comments
Closed

Bug in StringLiteralsInInterpolation #1415

bquorning opened this issue Nov 2, 2014 · 8 comments
Assignees
Labels

Comments

@bquorning
Copy link
Contributor

$ cat foo.rb
"foo" \
  "bar"

$ bin/rubocop --only StringLiteralsInInterpolation -- foo.rb
Inspecting 1 file
C

Offenses:

foo.rb:1:1: C: Prefer single-quoted strings inside interpolations.
"foo" \
^^^^^
foo.rb:2:3: C: Prefer single-quoted strings inside interpolations.
  "bar"
  ^^^^^

1 file inspected, 2 offenses detected

$ bin/rubocop --version
0.27.0

Since there isn’t any interpolation into the given string, this looks like a bug.

@jonas054 jonas054 self-assigned this Nov 2, 2014
@jonas054 jonas054 added the bug label Nov 2, 2014
@jonas054
Copy link
Collaborator

jonas054 commented Nov 2, 2014

Confirmed. That's a bug. I'll fix it.

bbatsov added a commit that referenced this issue Nov 3, 2014
…tion_bug

[Fix #1415] Handle backslash-concatenation
@bquorning
Copy link
Contributor Author

Confirmed: this cop seems to work fine now. 🍰 thanks!

@e2
Copy link
Contributor

e2 commented Nov 4, 2014

Same topic, so I'm posting here:

Tested on: @be1f42030fce (master)

This works:

foo "#{'bax'}"

... but this doesn't:

foo " #{'bax'}"

(The space causes the problem.)

a.rb:1:9: C: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
foo " #{'bax'}"
        ^^^^^

@jonas054
Copy link
Collaborator

jonas054 commented Nov 4, 2014

@e2 I have not been able to reproduce your problem. The message from rubocop indicates that you have configured with

StringLiteralsInInterpolation:
  EnforcedStyle: double_quotes

so both your examples should be reported as violations. Please double-check by putting both examples in the same file and see what you get. If there is a problem, open a new issue.

@e2
Copy link
Contributor

e2 commented Nov 4, 2014

Sorry, I should have provided everything to reproduce the problem.

Here's a script:

#!/bin/sh

url="https://raw.githubusercontent.com/thoughtbot/hound/master/config/style_guides/ruby.yml"
dir="./interpolation_test"
rev="be1f42030fce726da1689335ce3005cd508184f7"

mkdir -p "${dir}"
cd "${dir}"

[ -e ruby.yml ] || wget -O .rubocop.yml "${url}"

[ -e Gemfile ] || echo "gem 'rubocop', github: 'bbatsov/rubocop', ref: '${rev}'" > Gemfile
bundle install --quiet
bundle show rubocop

rm -f foo.rb
cat > foo.rb <<EOS
foo "#{bar}#{'all'}"
foo "#{bar} #{'all'}"
EOS

bundle exec rubocop foo.rb

Expected: 2 errors, regardless of what they are
Got: 1 error (on line 2 - the one with the space)

@vinagrito
Copy link

Just wondering, what about:
:"#{some_str.gsub('/', '_')}_some_other_string" - Not working
while this:
"#{some_str.gsub('/', '_')}_some_other_string".to_sym - Does work

@jonas054
Copy link
Collaborator

@vinagrito Did you mean, why doesn't RuboCop report the single quotes in the first example if the preference is double_quotes for StringLiteralsInInterpolation? It's because that's a symbol literal. The second example is a string literal.

It looks like this cop, and some others that deal with interpolation, miss the symbol literal case.

@vinagrito
Copy link

@jonas054 gotcha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants