Skip to content

Commit

Permalink
Merge pull request rubocop#765 from jonas054/fix_trailing_comma_heredoc
Browse files Browse the repository at this point in the history
[Fix rubocop#764] Handle heredocs in TrailingComma
  • Loading branch information
bbatsov committed Jan 26, 2014
2 parents 2e0fc81 + e5f7326 commit f82a7d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

* [#762](https://github.com/bbatsov/rubocop/issues/762): Support Rainbow gem both 1.99.x and 2.x. ([@yujinakayama][])

### Bugs fixed

* [#764](https://github.com/bbatsov/rubocop/issues/764): Handle heredocs in `TrailingComma`. ([@jonas054][])

## 0.17.0 (25/01/2014)

### New features
Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/cop/style/trailing_comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def check_literal(node, kind)
def check(node, items, kind, begin_pos, end_pos)
sb = items.first.loc.expression.source_buffer
after_last_item = Parser::Source::Range.new(sb, begin_pos, end_pos)

return if heredoc?(after_last_item.source)

comma_offset = after_last_item.source =~ /,/
should_have_comma = style == :comma && multiline?(node)
if comma_offset
Expand All @@ -60,6 +63,10 @@ def check(node, items, kind, begin_pos, end_pos)
end
end

def heredoc?(source_after_last_item)
source_after_last_item =~ /\w/
end

# Returns true if the node has round/square/curly brackets.
def brackets?(node)
!node.loc.end.nil?
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/style/trailing_comma_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@
' )'])
expect(cop.offences).to be_empty
end

it 'accepts comma inside a heredoc' +
' parameters at the end' do
inspect_source(cop, ['route(help: {',
" 'auth' => <<-HELP.chomp",
',',
'HELP',
'})'])
expect(cop.offences).to be_empty
end
end

context 'when EnforcedStyleForMultiline is comma' do
Expand Down Expand Up @@ -195,6 +205,17 @@
' )'])
expect(cop.offences).to be_empty
end

it 'accepts missing comma after a heredoc' do
# A heredoc that's the last item in a literal or parameter list can not
# have a trailing comma. It's a syntax error.
inspect_source(cop, ['route(help: {',
" 'auth' => <<-HELP.chomp",
'...',
'HELP',
'},)']) # We still need a comma after the hash.
expect(cop.offences).to be_empty
end
end
end
end

0 comments on commit f82a7d2

Please sign in to comment.