Skip to content

Commit

Permalink
[Fix rubocop#2912]Fix ForceEqualSignAlignment check
Browse files Browse the repository at this point in the history
We should check whether a line is aligned with the following line if the preceding line is not an assignment statement.

This fixes rubocop#2912
  • Loading branch information
akihiro17 committed Apr 23, 2016
1 parent 603df7f commit 8f5dc42
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* [#3039](https://github.com/bbatsov/rubocop/issues/3039): Accept `match` without a receiver in `Performance/EndWith`. ([@lumeet][])
* [#3039](https://github.com/bbatsov/rubocop/issues/3039): Accept `match` without a receiver in `Performance/StartWith`. ([@lumeet][])
* [#3048](https://github.com/bbatsov/rubocop/issues/3048): `Lint/NestedMethodDefinition` shouldn't flag methods defined on Structs. ([@owst][])
* [#2912](https://github.com/bbatsov/rubocop/issues/2912): Check whether a line is aligned with the following line if the preceding line is not an assignment. ([@akihiro17][])

### Changes

Expand Down Expand Up @@ -2126,3 +2127,4 @@
[@bolshakov]: https://github.com/bolshakov
[@jastkand]: https://github.com/jastkand
[@graemeboy]: https://github.com/graemeboy
[@akihiro17]: https://github.com/akihiro17
29 changes: 22 additions & 7 deletions lib/rubocop/cop/style/extra_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,31 @@ def check_tokens(ast, t1, t2)
end

def check_assignment(token)
# minus 2 is because pos.line is zero-based
line = processed_source.lines[token.pos.line - 2]
return if aligned_assignment?(token.pos, line)

preceding = @asgn_lines.include?(token.pos.line - 1)
align_with = preceding ? 'preceding' : 'following'
message = format(MSG_UNALIGNED_ASGN, align_with)
assignment_line = ''
message = ''
if should_aligned_with_preceding_line?(token)
assignment_line = preceding_line(token)
message = format(MSG_UNALIGNED_ASGN, 'preceding')
else
assignment_line = following_line(token)
message = format(MSG_UNALIGNED_ASGN, 'following')
end
return if aligned_assignment?(token.pos, assignment_line)
add_offense(token.pos, token.pos, message)
end

def should_aligned_with_preceding_line?(token)
@asgn_lines.include?(token.pos.line - 1)
end

def preceding_line(token)
processed_source.lines[token.pos.line - 2]
end

def following_line(token)
processed_source.lines[token.pos.line]
end

def check_other(t1, t2, ast)
return if t1.pos.line != t2.pos.line
return if t2.pos.begin_pos - 1 <= t1.pos.end_pos
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/extra_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@
expect(cop.offenses.size).to eq(0)
end

it 'aligns the first assignment with the following assingment' do
inspect_source(cop, ['# comment',
'a = 1',
'bb = 2'])
expect(cop.offenses.size).to eq(0)
end

it 'autocorrects consecutive assignments which are not aligned' do
new_source = autocorrect_source(cop, ['a = 1',
'bb = 2',
Expand Down

0 comments on commit 8f5dc42

Please sign in to comment.