Module: Rubocop::Cop::Style::AutocorrectAlignment
- Included in:
- AlignArray, AlignParameters
- Defined in:
- lib/rubocop/cop/style/autocorrect_alignment.rb
Overview
This module does auto-correction of nodes that should just be moved to the left or to the right, amount being determined by the instance variable @column_delta.
Instance Method Summary collapse
Instance Method Details
#autocorrect(node) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubocop/cop/style/autocorrect_alignment.rb', line 23 def autocorrect(node) # We can't use the instance variable inside the lambda. That would # just give each lambda the same reference and they would all get # the last value of @column_delta. A local variable fixes the # problem. column_delta = @column_delta @corrections << lambda do |corrector| expr = node.loc.expression if column_delta > 0 corrector.replace(expr, expr.source.gsub(/^/, ' ' * column_delta)) else offset = 0 expr.source.each_line do |line| b = expr.begin_pos + offset if offset == 0 range = Parser::Source::Range.new(expr.source_buffer, b + column_delta, b + line.length) corrector.replace(range, line) else range = Parser::Source::Range.new(expr.source_buffer, b, b + line.length) corrector.replace(range, line[-column_delta..-1]) end offset += line.length end end end end |
#check_alignment(items) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/rubocop/cop/style/autocorrect_alignment.rb', line 10 def check_alignment(items) items.each_cons(2) do |prev, current| if current.loc.line > prev.loc.line && start_of_line?(current.loc) @column_delta = items.first.loc.column - current.loc.column convention(current, :expression) if @column_delta != 0 end end end |
#start_of_line?(loc) ⇒ Boolean
19 20 21 |
# File 'lib/rubocop/cop/style/autocorrect_alignment.rb', line 19 def start_of_line?(loc) loc.expression.source_line[0...loc.column] =~ /^\s*$/ end |