Class: RuboCop::Cop::RBS::Layout::CommentIndentation
- Inherits:
-
RBS::CopBase
- Object
- Base
- RBS::CopBase
- RuboCop::Cop::RBS::Layout::CommentIndentation
show all
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rbs/layout/comment_indentation.rb
Overview
Checks the indentation of comments in RBS.
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
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
#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(, )
next_line = ()
= correct_indentation(next_line)
column = .location.start_column
column_delta = - column
return if column_delta.zero?
token_range = location_to_range(.location)
message = format(MSG, expect: , actual: column)
add_offense(token_range, message: message) do |corrector|
line_start_pos = processed_source.buffer.line_range(.location.start_line).begin_pos
indent = range_between(line_start_pos, .location.start_pos)
corrector.replace(indent, ' ' * )
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
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
|
47
48
49
50
|
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 47
def ()
lines = processed_source.lines
lines[.location.start_line..].find { |line| !line.blank? }
end
|
#on_rbs_new_investigation ⇒ Object
23
24
25
26
27
28
|
# File 'lib/rubocop/cop/rbs/layout/comment_indentation.rb', line 23
def on_rbs_new_investigation
= processed_rbs_source.tokens.select { |token| token.type == :tLINECOMMENT }
.each_with_index do |token, |
check(token, )
end
end
|