Class: RuboCop::Cop::Style::IndentArray

Inherits:
Cop
  • Object
show all
Includes:
AutocorrectAlignment
Defined in:
lib/rubocop/cop/style/indent_array.rb

Overview

This cops checks the indentation of the first element in an array literal where the opening bracket and the first element are on separate lines. The other elements' indentations are handled by the AlignArray cop.

Array literals shall have their first element indented one step (2 spaces) more than the start of the line where the opening bracket is.

Constant Summary

Constants included from Util

Util::ASGN_NODES, Util::EQUALS_ASGN_NODES, Util::OPERATOR_METHODS, Util::PROC_NEW_NODE, Util::SHORTHAND_ASGN_NODES

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offenses, #processed_source

Instance Method Summary collapse

Methods included from AutocorrectAlignment

#autocorrect, #check_alignment, #configured_indentation_width, #start_of_line?

Methods inherited from Cop

#add_offense, all, #autocorrect?, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, cop_name_with_namespace, cop_type, #debug?, #display_cop_names?, inherited, #initialize, #join_force?, lint?, #message, non_rails, qualified_cop_name, rails?, #relevant_file?, #support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

begins_its_line?, block_length, command?, comment_line?, const_name, first_part_of_call_chain, lambda?, lambda_or_proc?, line_range, numeric_range_size, on_node, operator?, parentheses?, proc?, range_with_surrounding_space, source_range, strip_quotes, within_node?

Methods included from PathUtil

issue_deprecation_warning, match_path?, relative_path

Constructor Details

This class inherits a constructor from RuboCop::Cop::Cop

Instance Method Details

#check_first_pair(first_pair, left_bracket) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/style/indent_array.rb', line 25

def check_first_pair(first_pair, left_bracket)
  return if first_pair.nil?
  expr = first_pair.loc.expression
  return if expr.line == left_bracket.line

  base_column = left_bracket.source_line =~ /\S/
  expected_column = base_column + configured_indentation_width
  @column_delta = expected_column - expr.column
  return if @column_delta == 0

  msg = format('Use %d spaces for indentation in an array, relative ' \
               'to the start of the line where the left bracket is.',
               configured_indentation_width)
  add_offense(first_pair, :expression, msg)
end

#check_right_bracket(node, first_pair, left_bracket) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/style/indent_array.rb', line 41

def check_right_bracket(node, first_pair, left_bracket)
  right_bracket = node.loc.end
  column = right_bracket.column
  return if right_bracket.source_line[0...column] =~ /\S/

  if first_pair && first_pair.loc.expression.line == left_bracket.line
    base_column = left_bracket.column
    expected_indentation = 'the left bracket'
  else
    base_column = left_bracket.source_line =~ /\S/
    expected_indentation =
      'the start of the line where the left bracket is'
  end
  @column_delta = base_column - column
  return if @column_delta == 0

  add_offense(right_bracket, right_bracket,
              'Indent the right bracket the same as ' +
              expected_indentation + '.')
end

#on_array(node) ⇒ Object


16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/style/indent_array.rb', line 16

def on_array(node)
  left_bracket = node.loc.begin
  return if left_bracket.nil?

  first_pair = node.children.first
  check_first_pair(first_pair, left_bracket)
  check_right_bracket(node, first_pair, left_bracket)
end