class RuboCop::Cop::Layout::ArrayAlignment

Here we check if the elements of a multi-line array literal are aligned.

@example EnforcedStyle: with_first_element (default)

# good

array = [1, 2, 3,
         4, 5, 6]
array = ['run',
         'forrest',
         'run']

# bad

array = [1, 2, 3,
  4, 5, 6]
array = ['run',
     'forrest',
     'run']

@example EnforcedStyle: with_fixed_indentation

# good

array = [1, 2, 3,
  4, 5, 6]

# bad

array = [1, 2, 3,
         4, 5, 6]

Constants

ALIGN_ELEMENTS_MSG
FIXED_INDENT_MSG

Public Instance Methods

on_array(node) click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 46
def on_array(node)
  return if node.children.size < 2
  return if node.parent&.masgn_type?

  check_alignment(node.children, base_column(node, node.children))
end

Private Instance Methods

autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 55
def autocorrect(corrector, node)
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
end
base_column(node, args) click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 67
def base_column(node, args)
  if fixed_indentation?
    lineno = target_method_lineno(node)
    line = node.source_range.source_buffer.source_line(lineno)
    indentation_of_line = /\S.*/.match(line).begin(0)
    indentation_of_line + configured_indentation_width
  else
    display_column(args.first.source_range)
  end
end
fixed_indentation?() click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 63
def fixed_indentation?
  cop_config['EnforcedStyle'] == 'with_fixed_indentation'
end
message(_range) click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 59
def message(_range)
  fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG
end
target_method_lineno(node) click to toggle source
# File lib/rubocop/cop/layout/array_alignment.rb, line 78
def target_method_lineno(node)
  node.loc.line
end