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/LineContinuationLeadingSpace cop #10715
Conversation
d270f75
to
0a9e6fc
Compare
0a9e6fc
to
cfcd96f
Compare
What's the reasoning for something like this?
Naming is hard. :D I'm okay with the proposed name, but @dvandersluis and @pirj might have better ideas.
I'm on the fence about this one, but I'm leaning towards |
I prefer trailing whitespace myself, but I'm wondering if we shouldn't make this configurable (leading or trailing). |
Since the cop is triggered by leading spaces, maybe |
I guess something like rubocop/spec/rubocop/cli/options_spec.rb Lines 1491 to 1494 in 023e0ea
I had a really hard time getting this cop to select correct ranges etc., and I think I would need some help in making it configurable. Perhaps first: @pirj can you help me get numbers on “real world offenses” with leading vs trailing spaces? |
cfcd96f
to
9016f48
Compare
Here you go:
Leading spaces on line continuation
Total 1019 lines (not occurrences!). Out of those, I'd say the ones with Personally, more leaning towards trailing spaces, I can't find many justifiable examples for the leading space style. |
Trailing spaces on continuation are in a majority with 4200 total lines (not occurrences!). Too long to list for a GitHub comment. |
Thank you @pirj 🙏🏼 I agree, where the leading whitespace serves as indentation, a heredoc would often be a better choice. |
Sounds like another cop idea to me. ;-) Let's keep the proposed cop simple and just agree on the final name & department. As mentioned before it seems to me that's mostly a layout cop, even if it's about the spacing inside multi-line string literals. I'm okay with the current name. |
I like @pirj’s suggestion better :-) |
Use trailing spaces instead of leading spaces in strings broken over multiple lines (by a backslash).
9016f48
to
caf13b4
Compare
Fine by me. Thanks for working on this! |
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#10715 and suppresses the following offense: ```console lib/rubocop/ast/node_pattern/compiler/binding.rb:24:31: C: Layout/LineContinuationLeadingSpace: Move leading spaces to the end of previous line. " union and can't be used outside that union" ^ 162 files inspected, 1 offense detected ```
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 4th issue mentioned in #6420.
When a string is continued over multiple lines, this cop will disallow spaces at the beginning of line n+1 if line n is a “string continuation”.
Topics for discussion:Should we allow space at the beginning of line n+1 if line n ends with a space?Who can come up with a better cop name?IsStyle
the correct department?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.