Skip to content

Commit

Permalink
Merge pull request #1710 from lumeet/1579_fix_block_alignment_ignored
Browse files Browse the repository at this point in the history
[Fix #1579] Block equality check in BlockAlignment
  • Loading branch information
bbatsov committed Mar 18, 2015
2 parents 81e9685 + 2d05bb3 commit 2d9c4d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* Handle symbols in `Lint/Void`. ([@bbatsov][])
* [#1695](https://github.com/bbatsov/rubocop/pull/1695): Fix bug with `--auto-gen-config` and `SpaceInsideBlockBraces`. ([@meganemura][])
* Correct issues with whitespace around multi-line lambda arguments. ([@zvkemp][])
* [#1579](https://github.com/bbatsov/rubocop/issues/1579): Fix handling of similar-looking blocks in `BlockAlignment`. ([@lumeet][])

### Changes

Expand Down
19 changes: 5 additions & 14 deletions lib/rubocop/cop/lint/block_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,19 @@ class BlockAlignment < Cop

MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d%s'

def initialize(config = nil, options = nil)
super
@inspected_blocks = []
end

def on_block(node)
return if already_processed_node?(node)
return if ignored_node?(node)
check_block_alignment(node, node)
end

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

_left, right = *node
return unless right.type == :block

check_block_alignment(node, right)
@inspected_blocks << right
ignore_node(right)
end

alias_method :on_or, :on_and
Expand Down Expand Up @@ -74,9 +69,9 @@ def check_assignment(begin_node, other_node)
block_node)
return
end
return if already_processed_node?(block_node)
return if ignored_node?(block_node)

@inspected_blocks << block_node
ignore_node(block_node)
check_block_alignment(begin_node, block_node)
end

Expand Down Expand Up @@ -140,10 +135,6 @@ def alt_start_msg(match, start_loc, do_loc, indentation_of_do_line)
def message
end

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
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/block_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,16 @@
expect(cop.messages)
.to eq(['`end` at 3, 2 is not aligned with `var1, var2` at 1, 0'])
end

context 'when multiple similar-looking blocks have misaligned ends' do
it 'registers an offense for each of them' do
inspect_source(cop,
['a = test do',
' end',
'b = test do',
' end'
])
expect(cop.offenses.size).to eq 2
end
end
end

0 comments on commit 2d9c4d4

Please sign in to comment.