Skip to content

Commit

Permalink
Fix end alignment of blocks that spawn on two lines
Browse files Browse the repository at this point in the history
This fixes rubocop#327
  • Loading branch information
edzhelyov committed Jul 3, 2013
1 parent e7c706e commit 18dca44
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [#321](https://github.com/bbatsov/rubocop/issues/321) - Ignore variables whose name start with `_` in `ShadowingOuterLocalVariable`
* [#322](https://github.com/bbatsov/rubocop/issues/322) - Fix exception of `UnusedLocalVariable` and `ShadowingOuterLocalVariable` when inspecting keyword splat argument
* [#316](https://github.com/bbatsov/rubocop/issues/316) - Correct nested postfix unless in `MultilineIfThen`
* [#327](https://github.com/bbatsov/rubocop/issues/327) - Fix false offences for block expression that spawn on two lines in `EndAlignment`

## 0.9.0 (01/07/2013)

Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/lint/end_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def process_block_assignment(begin_node, block_node)
end
end
if block_node.type == :block
# Align with the expression that is on the same line
# where the block is defined
return if block_is_on_next_line?(begin_node, block_node)

@inspected_blocks << block_node
check_block_alignment(begin_node.loc.expression, block_node.loc)
end
Expand Down Expand Up @@ -175,6 +179,10 @@ def check(node)
def already_processed_node?(node)
@inspected_blocks.include?(node)
end

def block_is_on_next_line?(begin_node, block_node)
begin_node.loc.line != block_node.loc.line
end
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cops/lint/end_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ module Lint
expect(cop.offences.size).to eq(1)
end

context 'when the block is defined on the next line' do
it 'accepts end aligned with the block expression' do
inspect_source(cop,
['variable =',
' a_long_method_that_dont_fit_on_the_line do |v|',
' v.foo',
' end'
])
expect(cop.offences).to be_empty
end

it 'registers an offences for mismatched end alignment' do
inspect_source(cop,
['variable =',
' a_long_method_that_dont_fit_on_the_line do |v|',
' v.foo',
'end'
])
expect(cop.offences.size).to eq(1)
end
end

it 'accepts end aligned with an instance variable' do
inspect_source(cop,
['@variable = test do |ala|',
Expand Down

0 comments on commit 18dca44

Please sign in to comment.