Skip to content

Commit

Permalink
[Fix #7628] Include trailing comma for single arg (#7630)
Browse files Browse the repository at this point in the history
  • Loading branch information
ty-porter authored and bbatsov committed Jan 6, 2020
1 parent cc43348 commit 9cfac0a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@
* [#7602](https://github.com/rubocop-hq/rubocop/pull/7602): Ensure proper handling of Ruby 2.7 syntax. ([@drenmi][])
* [#7620](https://github.com/rubocop-hq/rubocop/issues/7620): Fix a false positive for `Migration/DepartmentName` when a disable comment contains a plain comment. ([@koic][])
* [#7616](https://github.com/rubocop-hq/rubocop/issues/7616): Fix an incorrect autocorrect for `Style/MultilineWhenThen` for when statement with then is an array or a hash. ([@koic][])
* [#7628](https://github.com/rubocop-hq/rubocop/issues/7628): Fix an incorrect autocorrect for `Layout/MultilineBlockLayout` removing trailing comma with single argument. ([@pawptart][])
* [#7627](https://github.com/rubocop-hq/rubocop/issues/7627): Fix a false negative for `Migration/DepartmentName` when there is space around `:` (e.g. `# rubocop : disable`). ([@koic][])

### Changes
Expand Down Expand Up @@ -4316,3 +4317,4 @@
[@jethroo]: https://github.com/jethroo
[@mangara]: https://github.com/mangara
[@pirj]: https://github.com/pirj
[@pawptart]: https://github.com/pawptart
19 changes: 14 additions & 5 deletions lib/rubocop/cop/layout/multiline_block_layout.rb
Expand Up @@ -97,7 +97,8 @@ def args_on_beginning_line?(node)
def line_break_necessary_in_args?(node)
needed_length = node.source_range.column +
node.source.lines.first.length +
block_arg_string(node.arguments).length + PIPE_SIZE
block_arg_string(node, node.arguments).length +
PIPE_SIZE
needed_length > max_line_length
end

Expand All @@ -115,7 +116,8 @@ def autocorrect_arguments(corrector, node)
newlines: false
).end_pos
range = range_between(node.loc.begin.end.begin_pos, end_pos)
corrector.replace(range, " |#{block_arg_string(node.arguments)}|")
corrector.replace(range,
" |#{block_arg_string(node, node.arguments)}|")
end

def autocorrect_body(corrector, node, block_body)
Expand All @@ -131,14 +133,21 @@ def autocorrect_body(corrector, node, block_body)
"\n #{' ' * block_start_col}")
end

def block_arg_string(args)
args.children.map do |arg|
def block_arg_string(node, args)
arg_string = args.children.map do |arg|
if arg.mlhs_type?
"(#{block_arg_string(arg)})"
"(#{block_arg_string(node, arg)})"
else
arg.source
end
end.join(', ')
arg_string += ',' if include_trailing_comma?(node.arguments)
arg_string
end

def include_trailing_comma?(args)
arg_count = args.each_descendant(:arg).to_a.size
arg_count == 1 && args.source.include?(',')
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cop/layout/multiline_block_layout_spec.rb
Expand Up @@ -259,4 +259,39 @@ def f
end
RUBY
end

it 'does not auto-correct a trailing comma when only one argument ' \
'is present' do
new_source = autocorrect_source(<<~RUBY)
def f
X.map do |
a,
|
end
end
RUBY
expect(new_source).to eq(<<~RUBY)
def f
X.map do |a,|
end
end
RUBY
end

it 'auto-corrects nested parens correctly' do
new_source = autocorrect_source(<<~RUBY)
def f
X.map do |
(((a), b), c)
|
end
end
RUBY
expect(new_source).to eq(<<~RUBY)
def f
X.map do |(((a), b), c)|
end
end
RUBY
end
end

0 comments on commit 9cfac0a

Please sign in to comment.