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

Checks the indentation of comments in RBS.

Examples:

# 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

documentation_url, #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(comment_token, _comment_index) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 30

def check(comment_token, _comment_index)
  next_line = line_after_comment(comment_token)
  correct_comment_indentation = correct_indentation(next_line)
  column = comment_token.location.start_column

  column_delta = correct_comment_indentation - column
  return if column_delta.zero?

  token_range = location_to_range(comment_token.location)
  message = format(MSG, expect: correct_comment_indentation, actual: column)
  add_offense(token_range, message: message) do |corrector|
    line_start_pos = processed_source.buffer.line_range(comment_token.location.start_line).begin_pos
    indent = range_between(line_start_pos, comment_token.location.start_pos)
    corrector.replace(indent, ' ' * correct_comment_indentation)
  end
end

#correct_indentation(next_line) ⇒ Object



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

def correct_indentation(next_line)
  return 0 unless next_line

  indentation_of_next_line = next_line =~ /\S/
  indentation_of_next_line + if less_indented?(next_line)
                               2
                             else
                               0
                             end
end

#less_indented?(line) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 63

def less_indented?(line)
  /\A\s*end\b/.match?(line)
end

#line_after_comment(comment) ⇒ Object



47
48
49
50
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 47

def line_after_comment(comment)
  lines = processed_source.lines
  lines[comment.location.start_line..].find { |line| !line.blank? }
end

#on_rbs_new_investigationObject



23
24
25
26
27
28
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 23

def on_rbs_new_investigation
  comments = processed_rbs_source.tokens.select { |token| token.type == :tLINECOMMENT }
  comments.each_with_index do |token, comment_index|
    check(token, comment_index)
  end
end