Class: RuboCop::Cop::RBS::Layout::EndAlignment

Inherits:
RBS::CopBase
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rbs/layout/end_alignment.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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rbs/layout/end_alignment.rb', line 36

def check(decl, expect:)
  return if decl.location.start_line == decl.location.end_line

  end_loc = decl.location[:end]
  actual = end_loc.start_column
  if actual != expect
    line_start_pos = processed_source.raw_source.rindex(/\R/, end_loc.start_pos) + 1
    range = range_between(end_loc.start_pos, end_loc.end_pos)
    source = decl.location.source.each_line.first.strip
    message = "`end` at #{end_loc.start_line}, #{end_loc.start_column} is not aligned with " \
              "`#{source}` at #{decl.location.start_line}, #{decl.location.start_column}."
    add_offense(range, message: message) do |corrector|
      whitespace = range_between(line_start_pos, end_loc.start_pos)
      corrector.replace(whitespace, ' ' * expect)
    end
  end
end

#check_indentation(decl, expect:) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/rbs/layout/end_alignment.rb', line 26

def check_indentation(decl, expect:)
  if decl.location.key?(:end)
    # module, class and interface
    check(decl, expect: expect)
    decl.members.each do |member|
      check_indentation(member, expect: expect + 2)
    end
  end
end

#on_rbs_new_investigationObject



20
21
22
23
24
# File 'lib/rubocop/cop/rbs/layout/end_alignment.rb', line 20

def on_rbs_new_investigation
  processed_rbs_source.decls.each do |decl|
    check_indentation(decl, expect: 0)
  end
end