Skip to content

Commit

Permalink
[Fix rubocop#3586] Handle single argument correctly in TrailingComma
Browse files Browse the repository at this point in the history
The method TrailingComma#multiline? should not return true
for a single argument (possibly spanning multiple lines)
followed by a closing bracket that's not on its own line.
  • Loading branch information
jonas054 committed Nov 12, 2018
1 parent b83ba76 commit b5b391f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#6443](https://github.com/rubocop-hq/rubocop/pull/6443): Fix an incorrect autocorrect for `Style/BracesAroundHashParameters` when the opening brace is bofore the first hash element at same line. ([@koic][])
* [#6445](https://github.com/rubocop-hq/rubocop/pull/6445): Treat `yield` and `super` like regular method calls in `Style/AlignHash`. ([@mvz][])
* [#3301](https://github.com/rubocop-hq/rubocop/issues/3301): Don't suggest or make semantic changes to the code in `Style/InfiniteLoop`. ([@jonas054][])
* [#3586](https://github.com/rubocop-hq/rubocop/issues/3586): Handle single argument spanning multiple lines in `Style/TrailingCommaInArguments`. ([@jonas054][])

## 0.60.0 (2018-10-26)

Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/cop/mixin/trailing_comma.rb
Expand Up @@ -98,10 +98,19 @@ def multiline?(node)
items = elements(node).map(&:source_range)
return false if items.empty?

return false if single_argument_not_multiline?(items, node)

items << node.loc.begin << node.loc.end
(items.map(&:first_line) + items.map(&:last_line)).uniq.size > 1
end

# A single argument with the closing bracket on the same line as the end
# of the argument is not considered multiline, even if the argument
# itself might span multiple lines.
def single_argument_not_multiline?(items, node)
items.size == 1 && !Util.begins_its_line?(node.loc.end)
end

def elements(node)
return node.children unless node.send_type?

Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -238,7 +238,7 @@
func({
@abc => 0,
@xyz => 1,
},)
})
func(
{
abc: 0,
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb
Expand Up @@ -73,6 +73,21 @@
end
end

context 'with a single argument spanning multiple lines' do
context 'when EnforcedStyleForMultiline is consistent_comma' do
let(:cop_config) { { 'EnforcedStyleForMultiline' => 'consistent_comma' } }

it 'accepts a single argument with no trailing comma' do
expect_no_offenses(<<-RUBY.strip_indent)
EmailWorker.perform_async({
subject: "hey there",
email: "foo@bar.com"
})
RUBY
end
end
end

context 'with multi-line list of values' do
context 'when EnforcedStyleForMultiline is no_comma' do
let(:cop_config) { { 'EnforcedStyleForMultiline' => 'no_comma' } }
Expand Down

0 comments on commit b5b391f

Please sign in to comment.