From 39e267d59a76ba7e3b083d3c426d22544fd0e765 Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Fri, 2 Aug 2013 17:04:43 +0200 Subject: [PATCH] Retract support for multiline chaining of blocks. Fixes #393 by removing the solution for #346. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/block_alignment.rb | 30 +++++++------------ .../rubocop/cops/lint/block_alignment_spec.rb | 16 ++++++++-- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0da69f7e7791..edf104eb66d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/block_alignment.rb b/lib/rubocop/cop/lint/block_alignment.rb index dac816c15610..54f08357d6c7 100644 --- a/lib/rubocop/cop/lint/block_alignment.rb +++ b/lib/rubocop/cop/lint/block_alignment.rb @@ -21,9 +21,7 @@ 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) @@ -31,7 +29,7 @@ def on_and(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 @@ -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) @@ -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 diff --git a/spec/rubocop/cops/lint/block_alignment_spec.rb b/spec/rubocop/cops/lint/block_alignment_spec.rb index 828cd97002ae..1350e4be7dce 100644 --- a/spec/rubocop/cops/lint/block_alignment_spec.rb +++ b/spec/rubocop/cops/lint/block_alignment_spec.rb @@ -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| ', @@ -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 @@ -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