Skip to content

Commit

Permalink
Extract indentations methods to AutocorrectAlignment
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed Aug 10, 2015
1 parent ac4f351 commit bf406a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 38 deletions.
10 changes: 10 additions & 0 deletions lib/rubocop/cop/mixin/autocorrect_alignment.rb
Expand Up @@ -6,10 +6,20 @@ module Cop
# the left or to the right, amount being determined by the instance
# variable @column_delta.
module AutocorrectAlignment
SPACE = ' '.freeze

def configured_indentation_width
config.for_cop('IndentationWidth')['Width']
end

def indentation(node)
offset(node) + (SPACE * configured_indentation_width)
end

def offset(node)
SPACE * node.loc.column
end

def check_alignment(items, base_column = nil)
base_column ||= items.first.loc.column unless items.empty?
prev_line = -1
Expand Down
51 changes: 21 additions & 30 deletions lib/rubocop/cop/style/parallel_assignment.rb
Expand Up @@ -21,7 +21,6 @@ module Style
# b = 2
# c = 3
class ParallelAssignment < Cop
include AutocorrectAlignment
include IfNode

MSG = 'Do not use parallel assignment.'
Expand Down Expand Up @@ -56,11 +55,11 @@ def autocorrect(node)
lambda do |corrector|
assignment_corrector =
if modifier_statement?(node.parent)
ModifierCorrector.new(node, configured_indentation_width)
ModifierCorrector.new(node, config)
elsif rescue_modifier?(node.parent)
RescueCorrector.new(node, configured_indentation_width)
RescueCorrector.new(node, config)
else
GenericCorrector.new(node, configured_indentation_width)
GenericCorrector.new(node, config)
end

corrector.replace(assignment_corrector.correction_range,
Expand Down Expand Up @@ -98,35 +97,27 @@ def rescue_modifier?(node)

# An internal class for correcting parallel assignment
class GenericCorrector
attr_reader :correction, :correction_range
include AutocorrectAlignment

def initialize(node, indentation_width)
attr_reader :config, :node, :correction, :correction_range

def initialize(node, config)
@node = node
@indentation_width = indentation_width
@config = config
end

def correction
"#{assignment.join("\n#{base_indentation}")}"
"#{assignment.join("\n#{offset(node)}")}"
end

def correction_range
@node.loc.expression
node.loc.expression
end

protected

def base_indentation
' ' * @node.loc.column
end

def extra_indentation
base_indentation + (' ' * indentation_width)
end

attr_reader :indentation_width

def assignment
left, right = *@node
left, right = *node
l_vars = extract_sources(left)
r_vars = extract_sources(right)
groups = l_vars.zip(r_vars)
Expand All @@ -144,39 +135,39 @@ def extract_sources(node)
# protected by rescue
class RescueCorrector < GenericCorrector
def correction
_node, rescue_clause = *@node.parent
_node, rescue_clause = *node.parent
_, _, rescue_result = *rescue_clause

"begin\n" <<
extra_indentation << assignment.join("\n#{extra_indentation}") <<
"\n#{base_indentation}rescue\n" <<
extra_indentation << rescue_result.loc.expression.source <<
"\n#{base_indentation}end"
indentation(node) << assignment.join("\n#{indentation(node)}") <<
"\n#{offset(node)}rescue\n" <<
indentation(node) << rescue_result.loc.expression.source <<
"\n#{offset(node)}end"
end

def correction_range
@node.parent.loc.expression
node.parent.loc.expression
end
end

# An internal class for correcting parallel assignment
# guarded by if, unless, while, or until
class ModifierCorrector < GenericCorrector
def correction
parent = @node.parent
parent = node.parent

modifier_range =
Parser::Source::Range.new(parent.loc.expression.source_buffer,
parent.loc.keyword.begin_pos,
parent.loc.expression.end_pos)

"#{modifier_range.source}\n" <<
extra_indentation << assignment.join("\n#{extra_indentation}") <<
"\n#{base_indentation}end"
indentation(node) << assignment.join("\n#{indentation(node)}") <<
"\n#{offset(node)}end"
end

def correction_range
@node.parent.loc.expression
node.parent.loc.expression
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions lib/rubocop/cop/style/rescue_modifier.rb
Expand Up @@ -44,14 +44,6 @@ def autocorrect(node)
def modifier?(node)
@modifier_locations.include?(node.loc.keyword)
end

def indentation(node)
offset(node) + (' ' * configured_indentation_width)
end

def offset(node)
' ' * node.loc.column
end
end
end
end
Expand Down

0 comments on commit bf406a7

Please sign in to comment.