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



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

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_expected_style(styles) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 38

def check_expected_style(styles)
  if styles.size > 1
    ambiguous_style_detected(*styles)
  else
    correct_style_detected
  end
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 23

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
  styles =
    detected_styles(actual_column, offset, left_parenthesis, left_brace)

  if @column_delta.zero?
    check_expected_style(styles)
  else
    incorrect_style_detected(styles, first, left_parenthesis)
  end
end

#detected_styles(actual_column, offset, left_parenthesis, left_brace) ⇒ Object



56
57
58
59
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 56

def detected_styles(actual_column, offset, left_parenthesis, left_brace)
  base_column = actual_column - configured_indentation_width - offset
  detected_styles_for_column(base_column, left_parenthesis, left_brace)
end

#detected_styles_for_column(column, left_parenthesis, left_brace) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 61

def detected_styles_for_column(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



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

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(styles, first, left_parenthesis) ⇒ Object



74
75
76
77
78
79
# File 'lib/rubocop/cop/mixin/array_hash_indentation.rb', line 74

def incorrect_style_detected(styles, first, left_parenthesis)
  add_offense(first, :expression,
              message(base_description(left_parenthesis))) do
    ambiguous_style_detected(*styles)
  end
end