Class: RungerStyle::MultilineHashValueIndentation

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Defined in:
lib/runger_style/cops/default/multiline_hash_value_indentation.rb

Constant Summary collapse

MSG =
'Hash value should be indented by two spaces relative to its key.'

Instance Method Summary collapse

Instance Method Details

#on_pair(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/runger_style/cops/default/multiline_hash_value_indentation.rb', line 9

def on_pair(node)
  key_node = node.key
  value_node = node.value

  # Only check if the value starts on a line after the key ends.
  if key_node.loc.expression.last_line != value_node.source_range.line

    key_column = key_node.source_range.column
    expected_value_column = key_column + 2
    actual_value_column = value_node.source_range.column

    if actual_value_column != expected_value_column
      add_offense(value_node, message: MSG) do |corrector|
        buffer = value_node.source_range.source_buffer
        # Get the entire line where the value starts.
        line_range = buffer.line_range(value_node.source_range.line)
        # Create a range covering just the indentation of that line.
        indentation_range =
          Parser::Source::Range.new(
            buffer,
            line_range.begin_pos,
            value_node.source_range.begin_pos,
          )
        # Replace the existing indentation with the expected number of spaces.
        corrector.replace(indentation_range, ' ' * expected_value_column)
      end
    end
  end
end