Class: RuboCop::Cop::RBS::Layout::CommentIndentation

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

Overview

Examples:

default

# bad
  # comment here
def foo: () -> void

# good
# comment here
def foo: () -> void

Constant Summary collapse

MSG =
"Incorrect indentation detected (column %<expect>s instead of %<actual>s)."

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

#on_rbs_new_investigationObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 20

def on_rbs_new_investigation
  indent_start_lines = Set.new
  indent_end_lines = Set.new
  processed_rbs_source.decls.each do |decl|
    walk_decl(decl) do |d|
      indent_start_lines << d.location.start_line
      indent_end_lines << d.location.end_line
    end
  end

  expected_width = 0
  processed_rbs_source.tokens.each do |token|
    case token.type
    when :kMODULE, :kCLASS, :kINTERFACE
      next unless indent_start_lines.include?(token.location.start_line)

      expected_width += 2
    when :kEND
      next unless indent_end_lines.include?(token.location.start_line)

      expected_width -= 2
    when :tLINECOMMENT
      if token.location.start_column != expected_width
        token_range = location_to_range(token.location)
        message = format(MSG, expect: expected_width, actual: token.location.start_column)
        add_offense(token_range, message: message) do |corrector|
          line_start_pos = processed_source.buffer.line_range(token.location.start_line).begin_pos
          indent = range_between(line_start_pos, token.location.start_pos)
          corrector.replace(indent, ' ' * expected_width)
        end
      end
    end
  end
end

#walk_decl(decl, &block) ⇒ Object



55
56
57
58
59
60
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 55

def walk_decl(decl, &block)
  if decl.respond_to?(:members)
    yield decl
    decl.members.each { |member| walk_decl(member, &block) }
  end
end