Class: RuboCop::Cop::RBS::Layout::IndentationWidth

Inherits:
RBS::CopBase
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rbs/layout/indentation_width.rb

Overview

Examples:

default

# bad
class Foo
def foo: () -> void
end

# good
class Foo
  def foo: () -> void
end

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

#investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_def, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#check(decl, expect:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/rbs/layout/indentation_width.rb', line 40

def check(decl, expect:)
  line_start_pos = line_start_pos(decl)
  actual = @first_char_columns[decl.location.start_line - 1]
  if actual != expect
    range = range_between(line_start_pos, line_start_pos + actual)
    message = "Use #{expect} (not #{actual}) spaces for indentation."
    add_offense(range, message: message) do |corrector|
      corrector.replace(range, ' ' * expect)
    end
  end
end

#check_indentation(decl, expect:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rbs/layout/indentation_width.rb', line 29

def check_indentation(decl, expect:)
  if decl.respond_to?(:members)
    check(decl, expect: expect)
    decl.members.each do |member|
      check_indentation(member, expect: expect + 2)
    end
  else
    check(decl, expect: expect)
  end
end

#line_start_pos(decl) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/rbs/layout/indentation_width.rb', line 52

def line_start_pos(decl)
  rindex = processed_source.raw_source.rindex(/\R/, decl.location.start_pos)
  if rindex
    rindex + 1
  else
    0
  end
end

#on_rbs_new_investigationObject



20
21
22
23
24
25
26
27
# File 'lib/rubocop/cop/rbs/layout/indentation_width.rb', line 20

def on_rbs_new_investigation
  @first_char_columns = processed_source.raw_source.each_line.map do |line|
    line.index(/[^[:space:]]/) || 0
  end
  processed_rbs_source.decls.each do |decl|
    check_indentation(decl, expect: 0)
  end
end