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
Add Layout/LineContinuationSpacing cop #10717
Add Layout/LineContinuationSpacing cop #10717
Conversation
1e0aa74
to
cfc3dd5
Compare
corrector.replace(range, correction) | ||
end | ||
|
||
def string_literal_ranges(ast) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3657121
to
d3d8413
Compare
I love this cop as the inconsistent spacing has been bugging me for a while. One small question, though - won't it be better to just make this some number config? If we stick to the styles I'd suggest naming them "single_space" and "no_space" as "zero" and "one" are kind of weird for style names. :-) |
I also thought about the number config, but then thought that I don’t want to write specs for how it works if someone chooses e.g. 20 spaces 😆 I think really only 0 and 1 are valid values. I personally would hate to use 0, but I have seen it used in the wild and added it up front instead of waiting for the inevitable feature request. We have a lot of cops with |
I would actually like feedback on two specific things:
|
b9348e2
to
73762d0
Compare
I think the name if fine. I thought we might have just added this to the other cop, but I think in this case it's better to have 2 of them.
I think it should be a Layout cop. |
When a line is too long, you can make it continue onto the next line by ending the first line with a backslash. This new cop checks how many spaces are in front of that backslash. By default it will allow only exactly one space, but the cop can be configured to instead allow exactly zero spaces.
73762d0
to
27c4e06
Compare
Looks good to me! |
Follow up rubocop/rubocop#10717 and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/rails/enum_hash.rb:23:80: C: [Corrected] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\ ^ lib/rubocop/cop/rails/order_by_id.rb:26:58: C: [Corrected] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Do not use the `id` column for ordering. '\ ^ spec/rubocop/cop/rails/schema_comment_spec.rb:39:77: C: [Corrected] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when `add_column` has `comment` option'\ ^ spec/rubocop/cop/rails/schema_comment_spec.rb:108:76: C: [Corrected] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when `t.integer` has `comment` option'\ ^ ```
Follow up rubocop/rubocop#10717, rubocop/rubocop#10715, and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/performance/chain_array_allocation.rb:50:69: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Use unchained `%<method>s` and `%<second_method>s!` '\ ^ lib/rubocop/cop/performance/chain_array_allocation.rb:51:78: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. '(followed by `return array` if required) instead of chaining '\ ^ lib/rubocop/cop/performance/collection_literal_in_loop.rb:35:70: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Avoid immutable %<literal_class>s literals in loops. '\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:61:56: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "#{autocorrect_hash_expression(node)}."\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:71:59: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "Use `##{autocorrect_method(node)}` instead of "\ ^ spec/rubocop/cop/performance/constant_regexp_spec.rb:61:84: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when regexp contains interpolated constant and '\ ^ spec/rubocop/cop/performance/redundant_match_spec.rb:89:7: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. ' surrounding method' do ^ 115 files inspected, 7 offenses detected, 6 offenses autocorrectable RuboCop failed! ```
Not sure if it should be here or in the issue First of all thanks for this. It will help gain clarity in 99% of the cases. Though: It may be a bad practice, and if it is I guess it is what it is TLTR: on my end I like to have spaces at the beginning of the lines that continue a previous non-finished sentence: # This is
# a multi line
# comment so I keep 2 spaces until my sentence is finished.
# But this sentence is a new one so I start it with 1 space instead of 2.
"And same" \
" goes for strings." \
"I like to leave an extra" \
" space at the beginning of my line if I'm in the middle" \
" of a sentence." IDK if we could consider an option for this use-case or again, if it's just me / a bad practice |
Fair, I understand. I get that it would've significantly increase the complexity of the implementation Thanks again @bquorning (and sorry for wrong thread 😂 , I tried but somehow managed to miss the right one) |
lol |
Follow up rubocop/rubocop#10717, rubocop/rubocop#10715, and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/performance/chain_array_allocation.rb:50:69: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Use unchained `%<method>s` and `%<second_method>s!` '\ ^ lib/rubocop/cop/performance/chain_array_allocation.rb:51:78: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. '(followed by `return array` if required) instead of chaining '\ ^ lib/rubocop/cop/performance/collection_literal_in_loop.rb:35:70: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Avoid immutable %<literal_class>s literals in loops. '\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:61:56: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "#{autocorrect_hash_expression(node)}."\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:71:59: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "Use `##{autocorrect_method(node)}` instead of "\ ^ spec/rubocop/cop/performance/constant_regexp_spec.rb:61:84: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when regexp contains interpolated constant and '\ ^ spec/rubocop/cop/performance/redundant_match_spec.rb:89:7: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. ' surrounding method' do ^ 115 files inspected, 7 offenses detected, 6 offenses autocorrectable RuboCop failed! ```
Follow up rubocop/rubocop#10717, rubocop/rubocop#10715, and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/performance/chain_array_allocation.rb:50:69: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Use unchained `%<method>s` and `%<second_method>s!` '\ ^ lib/rubocop/cop/performance/chain_array_allocation.rb:51:78: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. '(followed by `return array` if required) instead of chaining '\ ^ lib/rubocop/cop/performance/collection_literal_in_loop.rb:35:70: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Avoid immutable %<literal_class>s literals in loops. '\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:61:56: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "#{autocorrect_hash_expression(node)}."\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:71:59: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "Use `##{autocorrect_method(node)}` instead of "\ ^ spec/rubocop/cop/performance/constant_regexp_spec.rb:61:84: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when regexp contains interpolated constant and '\ ^ spec/rubocop/cop/performance/redundant_match_spec.rb:89:7: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. ' surrounding method' do ^ 115 files inspected, 7 offenses detected, 6 offenses autocorrectable RuboCop failed! ```
Follow up rubocop/rubocop#10717, rubocop/rubocop#10715, and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/performance/chain_array_allocation.rb:50:69: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Use unchained `%<method>s` and `%<second_method>s!` '\ ^ lib/rubocop/cop/performance/chain_array_allocation.rb:51:78: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. '(followed by `return array` if required) instead of chaining '\ ^ lib/rubocop/cop/performance/collection_literal_in_loop.rb:35:70: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Avoid immutable %<literal_class>s literals in loops. '\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:61:56: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "#{autocorrect_hash_expression(node)}."\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:71:59: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "Use `##{autocorrect_method(node)}` instead of "\ ^ spec/rubocop/cop/performance/constant_regexp_spec.rb:61:84: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when regexp contains interpolated constant and '\ ^ spec/rubocop/cop/performance/redundant_match_spec.rb:89:7: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. ' surrounding method' do ^ 115 files inspected, 7 offenses detected, 6 offenses autocorrectable RuboCop failed! ```
Follow up rubocop/rubocop#10717, rubocop/rubocop#10715, and suppresses the following offenses: ```console Offenses: lib/rubocop/cop/performance/chain_array_allocation.rb:50:69: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Use unchained `%<method>s` and `%<second_method>s!` '\ ^ lib/rubocop/cop/performance/chain_array_allocation.rb:51:78: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. '(followed by `return array` if required) instead of chaining '\ ^ lib/rubocop/cop/performance/collection_literal_in_loop.rb:35:70: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. MSG = 'Avoid immutable %<literal_class>s literals in loops. '\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:61:56: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "#{autocorrect_hash_expression(node)}."\ ^ lib/rubocop/cop/performance/inefficient_hash_search.rb:71:59: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. "Use `##{autocorrect_method(node)}` instead of "\ ^ spec/rubocop/cop/performance/constant_regexp_spec.rb:61:84: C: [Correctable] Layout/LineContinuationSpacing: Use one space in front of backslash. it 'does not register an offense when regexp contains interpolated constant and '\ ^ spec/rubocop/cop/performance/redundant_match_spec.rb:89:7: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. ' surrounding method' do ^ 115 files inspected, 7 offenses detected, 6 offenses autocorrectable RuboCop failed! ```
Fixes the 1st issue mentioned in #6420.
Style/LineEndConcatenation
recommends that we should use\
instead of+
or<<
to concatenate strings across multiple lines. But I am still seeing wildly inconsistent formatting of these multiline strings. This newLayout/LineContinuationSpacing
cop checks that there is consistently one space before a\
at the end of a line. (Can be configured to zero spaces)Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.