Module: RuboCop::Cop::ArrayHashIndentation

Included in:
Style::IndentArray, Style::IndentHash
Defined in:
lib/rubocop/cop/mixin/array_hash_indentation.rb

Overview

Common code for indenting literal arrays and hashes.

Instance Method Summary collapse

Instance Method Details

#base_column(left_brace, left_parenthesis) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 47

def base_column(left_brace, left_parenthesis)
  if style == brace_alignment_style
    left_brace.column
  elsif left_parenthesis && style == :special_inside_parentheses
    left_parenthesis.column + 1
  else
    left_brace.source_line =~ /\S/
  end
end

#check_first(first, left_brace, left_parenthesis, offset) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 24

def check_first(first, left_brace, left_parenthesis, offset)
  actual_column = first.source_range.column
  expected_column = base_column(left_brace, left_parenthesis) +
                    configured_indentation_width + offset
  @column_delta = expected_column - actual_column

  if @column_delta.zero?
    # which column was actually used as 'base column' for indentation?
    # (not the column which we think should be the 'base column',
    # but the one which has actually been used for that purpose)
    base_column = actual_column - configured_indentation_width - offset
    styles = detected_styles(base_column, left_parenthesis, left_brace)
    if styles.size > 1
      ambiguous_style_detected(*styles)
    else
      correct_style_detected
    end
  else
    incorrect_style_detected(actual_column, offset, first,
                             left_parenthesis, left_brace)
  end
end

#detected_styles(column, left_parenthesis, left_brace) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 57

def detected_styles(column, left_parenthesis, left_brace)
  styles = []
  if column == (left_brace.source_line =~ /\S/)
    styles << :consistent
    styles << :special_inside_parentheses unless left_parenthesis
  end
  if left_parenthesis && column == left_parenthesis.column + 1
    styles << :special_inside_parentheses
  end
  styles << brace_alignment_style if column == left_brace.column
  styles
end

#each_argument_node(node, type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 8

def each_argument_node(node, type)
  _receiver, _method_name, *args = *node
  left_parenthesis = node.loc.begin
  return unless left_parenthesis

  args.each do |arg|
    on_node(type, arg, :send) do |type_node|
      left_brace = type_node.loc.begin
      if left_brace && left_brace.line == left_parenthesis.line
        yield type_node, left_parenthesis
        ignore_node(type_node)
      end
    end
  end
end

#incorrect_style_detected(column, offset, first, left_parenthesis, left_brace) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 70

def incorrect_style_detected(column, offset, first, left_parenthesis,
                             left_brace)
  add_offense(first, :expression,
              message(base_description(left_parenthesis))) do
    base_column = column - configured_indentation_width - offset
    styles = detected_styles(base_column, left_parenthesis, left_brace)
    ambiguous_style_detected(*styles)
  end
end