Skip to content

Commit

Permalink
[Fix rubocop#2719] ConditionalAssignment handles end alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Feb 9, 2016
1 parent 0c0f76b commit 94f2e13
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* `Style/MultilineBlockLayout` can correct offenses which involve argument destructuring. ([@alexdowad][])
* `Style/SpaceAroundKeyword` checks `super` nodes with no args. ([@alexdowad][])
* `Style/SpaceAroundKeyword` checks `defined?` nodes. ([@alexdowad][])
* [#2719](https://github.com/bbatsov/rubocop/issues/2719): `Style/ConditionalAssignment` handles correcting the alignment of `end`. ([@rrosenblum][])

### Bug fixes

Expand Down
30 changes: 24 additions & 6 deletions lib/rubocop/cop/style/conditional_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module Style
# ConditionalAssignment Cop.
module ConditionalAssignmentHelper
EQUAL = '='.freeze
END_ALIGNMENT = 'Lint/EndAlignment'.freeze
ALIGN_WITH = 'AlignWith'.freeze
KEYWORD = 'keyword'.freeze

# `elsif` branches show up in the `node` as an `else`. We need
# to recursively iterate over all `else` branches and consider all
Expand Down Expand Up @@ -55,6 +58,16 @@ def lhs(node)
end
end

def indent(cop, source)
if cop.config[END_ALIGNMENT] &&
cop.config[END_ALIGNMENT][ALIGN_WITH] &&
cop.config[END_ALIGNMENT][ALIGN_WITH] == KEYWORD
' ' * source.length
else
''
end
end

private

def expand_elsif(node, elsif_branches = [])
Expand Down Expand Up @@ -185,11 +198,11 @@ def autocorrect(node)
else
case node.loc.keyword.source
when IF
IfCorrector.correct(node)
IfCorrector.correct(self, node)
when UNLESS
UnlessCorrector.correct(node)
UnlessCorrector.correct(self, node)
else
CaseCorrector.correct(node)
CaseCorrector.correct(self, node)
end
end
end
Expand Down Expand Up @@ -296,7 +309,7 @@ class IfCorrector
class << self
include ConditionalAssignmentHelper

def correct(node)
def correct(cop, node)
_condition, if_branch, else_branch = *node
if_branch = tail(if_branch)
_variable, *_operator, if_assignment = *if_branch
Expand All @@ -311,6 +324,7 @@ def correct(node)
correct_branches(corrector, elsif_branches)
corrector.replace(else_branch.source_range,
else_assignment.source)
corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
end
end
end
Expand All @@ -321,7 +335,7 @@ class CaseCorrector
class << self
include ConditionalAssignmentHelper

def correct(node)
def correct(cop, node)
_condition, *when_branches, else_branch = *node
else_branch = tail(else_branch)
when_branches = expand_when_branches(when_branches)
Expand All @@ -333,6 +347,9 @@ def correct(node)
correct_branches(corrector, when_branches)
corrector.replace(else_branch.source_range,
else_assignment.source)

corrector.insert_before(node.loc.end,
indent(cop, lhs(else_branch)))
end
end
end
Expand All @@ -343,7 +360,7 @@ class UnlessCorrector
class << self
include ConditionalAssignmentHelper

def correct(node)
def correct(cop, node)
_condition, else_branch, if_branch = *node
if_branch = tail(if_branch)
else_branch = tail(else_branch)
Expand All @@ -355,6 +372,7 @@ def correct(node)
corrector.replace(if_branch.source_range, if_assignment.source)
corrector.replace(else_branch.source_range,
else_assignment.source)
corrector.insert_before(node.loc.end, indent(cop, lhs(if_branch)))
end
end
end
Expand Down
Loading

0 comments on commit 94f2e13

Please sign in to comment.