Skip to content

Commit

Permalink
Retract support for multiline chaining of blocks.
Browse files Browse the repository at this point in the history
Fixes rubocop#393 by removing the solution for rubocop#346.
  • Loading branch information
jonas054 committed Aug 2, 2013
1 parent 13e062d commit 39e267d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [#400](https://github.com/bbatsov/rubocop/issues/400) - Fix bug concerning nested defs in `EmptyLineBetweenDefs` cop.
* [#399](https://github.com/bbatsov/rubocop/issues/399) - Allow assignment inside blocks in `AssignmentInCondition` cop.
* Fix bug in favor_modifier.rb regarding missed offences after else etc.
* [#393](https://github.com/bbatsov/rubocop/issues/393) - Retract support for multiline chaining of blocks (which fixed [#346](https://github.com/bbatsov/rubocop/issues/346)), thus rejecting issue 346.

## 0.10.0 (17/07/2013)

Expand Down
30 changes: 10 additions & 20 deletions lib/rubocop/cop/lint/block_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ def initialize

def on_block(node)
return if already_processed_node?(node)
method, = *node
start_node = method.loc.expression.source =~ /\n/ ? method : node
check_block_alignment(start_node.loc.expression, node.loc)
check_block_alignment(node, node)
end

def on_and(node)
return if already_processed_node?(node)

_left, right = *node
if right.type == :block
check_block_alignment(node.loc.expression, right.loc)
check_block_alignment(node, right)
@inspected_blocks << right
end
end
Expand Down Expand Up @@ -93,7 +91,7 @@ def process_block_assignment(begin_node, other_node)
return if already_processed_node?(block_node)

@inspected_blocks << block_node
check_block_alignment(begin_node.loc.expression, block_node.loc)
check_block_alignment(begin_node, block_node)
end

def find_block_node(node)
Expand All @@ -117,24 +115,16 @@ def find_block_or_send_node(send_node)
end
end

def check_block_alignment(start_loc, block_loc)
match = start_loc.source.match(/\n(\s*)((end)?\.\S+)\Z/)
if match
start_line = start_loc.line + start_loc.source.count("\n")
start_column = match.captures[0].length
start_source = match.captures[1]
else
start_line = start_loc.line
start_column = start_loc.column
start_source = start_loc.source.lines.to_a.first.chomp
end
end_loc = block_loc.end
if block_loc.begin.line != end_loc.line &&
start_column != end_loc.column
def check_block_alignment(start_node, block_node)
start_loc = start_node.loc.expression
end_loc = block_node.loc.end
if block_node.loc.begin.line != end_loc.line &&
start_loc.column != end_loc.column
add_offence(:warning,
end_loc,
sprintf(MSG, end_loc.line, end_loc.column,
start_source, start_line, start_column))
start_loc.source.lines.to_a.first.chomp,
start_loc.line, start_loc.column))
end
end

Expand Down
16 changes: 14 additions & 2 deletions spec/rubocop/cops/lint/block_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module Lint
end

context 'when the method part is a call chain that spans several lines' do
it 'accepts end aligned with first character of line where do is' do
it 'registers offences for pretty multiline chaining' do
src = ['def foo(bar)',
' bar.get_stuffs',
' .reject do |stuff| ',
Expand All @@ -111,7 +111,10 @@ module Lint
' end',
'end']
inspect_source(cop, src)
expect(cop.offences.map(&:message)).to eq([])
expect(cop.offences.map(&:message))
.to eq(['end at 10, 6 is not aligned with bar.get_stuffs at 2, 2',
'end at 7, 6 is not aligned with bar.get_stuffs at 2, 2',
'end at 5, 6 is not aligned with bar.get_stuffs at 2, 2'])
end

it 'registers offences for misaligned ends' do
Expand All @@ -129,6 +132,15 @@ module Lint
inspect_source(cop, src)
expect(cop.offences).to have(3).items
end

it 'accepts end indented as the start of the block' do
src = ['my_object.chaining_this_very_long_method(with_a_parameter)',
' .and_one_with_a_block do',
' do_something',
'end']
inspect_source(cop, src)
expect(cop.offences).to be_empty
end
end

context 'when variables of a mass assignment spans several lines' do
Expand Down

0 comments on commit 39e267d

Please sign in to comment.