Skip to content

Commit

Permalink
[Fix rubocop#9691] Add configuration option InspectBlocks to Redundan…
Browse files Browse the repository at this point in the history
…tLineBreak

Set the default value for this new configuration option to `false`, since we
suspect that many users might consider the `do`..`end` variant with line
breaks more readable.

For RuboCop's own sources we set it to `true`, i.e., the same behavior as
before the parameters existed, at least for now.
  • Loading branch information
jonas054 committed Apr 10, 2021
1 parent 1e55b1a commit 76aa546
Show file tree
Hide file tree
Showing 5 changed files with 340 additions and 283 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -49,6 +49,7 @@ Layout/ClassStructure:

Layout/RedundantLineBreak:
Enabled: true
InspectBlocks: true

Layout/TrailingWhitespace:
AllowInHeredoc: false
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### New features

* [#7977](https://github.com/rubocop/rubocop/issues/7977): Add `Layout/RedundantLineBreak` cop. ([@jonas054][])
* [#9691](https://github.com/rubocop/rubocop/issues/9691): Add configuration parameter `InspectBlocks` to `Layout/RedundantLineBreak`. ([@jonas054][])

## 1.12.1 (2021-04-04)

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1106,6 +1106,7 @@ Layout/RedundantLineBreak:
Do not break up an expression into multiple lines when it fits
on a single line.
Enabled: false
InspectBlocks: false
VersionAdded: '<<next>>'

Layout/RescueEnsureAlignment:
Expand Down
36 changes: 24 additions & 12 deletions lib/rubocop/cop/layout/redundant_line_break.rb
Expand Up @@ -6,17 +6,13 @@ module Layout
# This cop checks whether certain expressions, e.g. method calls, that could fit
# completely on a single line, are broken up into multiple lines unnecessarily.
#
# @example
# @example any configuration
# # bad
# foo(
# a,
# b
# )
#
# foo(a) do |x|
# puts x
# end
#
# puts 'string that fits on ' \
# 'a single line'
#
Expand All @@ -27,12 +23,25 @@ module Layout
# # good
# foo(a, b)
#
# foo(a) { |x| puts x }
#
# puts 'string that fits on a single line'
#
# things.select { |thing| thing.cond? }.join('-')
#
# @example InspectBlocks: false (default)
# # good
# foo(a) do |x|
# puts x
# end
#
# @example InspectBlocks: true
# # bad
# foo(a) do |x|
# puts x
# end
#
# # good
# foo(a) { |x| puts x }
#
class RedundantLineBreak < Cop
include CheckAssignment

Expand Down Expand Up @@ -64,7 +73,14 @@ def autocorrect(node)
private

def offense?(node)
!single_line?(node) && !too_long?(node) && suitable_as_single_line?(node)
return false if configured_to_not_be_inspected?(node)

node.multiline? && !too_long?(node) && suitable_as_single_line?(node)
end

def configured_to_not_be_inspected?(node)
!cop_config['InspectBlocks'] && (node.block_type? ||
node.each_child_node(:block).any?(&:multiline?))
end

def suitable_as_single_line?(node)
Expand All @@ -87,10 +103,6 @@ def comment_within?(node)
end
end

def single_line?(node)
node.first_line == node.last_line
end

def too_long?(node)
lines = processed_source.lines[(node.first_line - 1)...node.last_line]
to_single_line(lines.join("\n")).length > max_line_length
Expand Down

0 comments on commit 76aa546

Please sign in to comment.