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/StringLiterals ignores interpolation in multi line strings #2549

Closed
maia opened this issue Dec 28, 2015 · 10 comments
Closed

Style/StringLiterals ignores interpolation in multi line strings #2549

maia opened this issue Dec 28, 2015 · 10 comments

Comments

@maia
Copy link

maia commented Dec 28, 2015

Rubocop seems to check multi-lined strings one line by the other, and if there's no interpolation in the current line it will report. I do believe that all lines of a multi-line string should use double-quoted strings if there's an interpolation in at least one line:

$ cat test.rb
"This is a multi-line string using interpolation #{Time.now.utc} "\
"but rubocop thinks not all lines should use double quotes."

$ rubocop test.rb --only Style/StringLiterals
Inspecting 1 file
C

Offenses:

test.rb:2:1: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
"but rubocop thinks not all lines should use double quotes."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected

Is this an incorrect report by rubocop, or is it suggested to mix single and double quotes in multi-line strings?

@alexdowad
Copy link
Contributor

This has already been brought up on this bug tracker before. The RC developers like the current style. If you would like to code up a PR to make this configurable, it will be appreciated.

@maia
Copy link
Author

maia commented Dec 28, 2015

Thanks. I've searched the issues for StringLiterals and didn't find anything related to multi-line strings. Can you point me to the issue? I'd like to read the pro's for the current style, as I didn't even think that mixing quotes in a single string is even allowed:

"This #{Time.now}"\
'feels very wrong'

As for your suggestion, I've just taken a quick look at the code and don't think I could add a configuration option, as it seems as if the checked node is not the entire multi-lined string but each single line is checked one by one. Unfortunately I don't have enough knowledge to alter that behavior.

@alexdowad
Copy link
Contributor

@maia It may have been in the style guide repo.

When you have string literals which are concatenated like this:

"A string" \
'another one'

...they are parsed as a dstr node with one str node under it for each line.

So you could add an on_dstr hook to StringLiterals to make it catch those nodes. Check if all the children are str nodes, and if not, just return. Otherwise, if they are all str, then process them and add offenses as is appropriate.

Then, call ignore_node(node) on each of the str nodes. The on_str hook (which is in StringHelp) already skips nodes which are marked as "ignored", so you can avoid double-processing of the nodes that way.

@maia
Copy link
Author

maia commented Dec 30, 2015

@alexdowad Thanks, I didn't find it in the style guide repo, so maybe the current style is not considered best practice. As for the PR, in case I can find a few spare hours to dig deeper in the internals of rubocop, I will make an attempt, but please don't count on it.

@alexdowad
Copy link
Contributor

I think we can add a config parameter to enforce the use of consistent quote types for multiline-continuation strings. Question: what should the new config parameter be called?

@maia
Copy link
Author

maia commented Jan 1, 2016

How about TreatMultiLinesAsGroup or ConsistentQuotesInMultiLines ?

@jonas054
Copy link
Collaborator

jonas054 commented Jan 2, 2016

ConsistentQuotesInMultilines sounds good. Note that we treat multiline as a single word in configuration, e.g. Style/MultilineMethodCallIndentation.

@alexdowad
Copy link
Contributor

Just pushed a fix for this one to my open PR. The new config parameter is called ConsistentQuotesInMultiline, as suggested.

@maia
Copy link
Author

maia commented Jan 5, 2016

That's great news, thanks a lot!

@bbatsov bbatsov closed this as completed in 4ea9849 Jan 7, 2016
@maia
Copy link
Author

maia commented Jan 7, 2016

Thanks a lot!

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

No branches or pull requests

3 participants