forked from rubocop/rubocop
/
comment_annotation.rb
147 lines (128 loc) · 4.53 KB
/
comment_annotation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks that comment annotation keywords are written according
# to guidelines.
#
# NOTE: With a multiline comment block (where each line is only a
# comment), only the first line will be able to register an offense, even
# if an annotation keyword starts another line. This is done to prevent
# incorrect registering of keywords (eg. `review`) inside a paragraph as an
# annotation.
#
# @example EnforcedStyle: colon (default)
# # bad
# # TODO make better
#
# # good
# # TODO: make better
#
# # bad
# # TODO:make better
#
# # good
# # TODO: make better
#
# # bad
# # fixme: does not work
#
# # good
# # FIXME: does not work
#
# # bad
# # Optimize does not work
#
# # good
# # OPTIMIZE: does not work
#
# @example EnforcedStyle: space
# # bad
# # TODO: make better
#
# # good
# # TODO make better
#
# # bad
# # fixme does not work
#
# # good
# # FIXME does not work
#
# # bad
# # Optimize does not work
#
# # good
# # OPTIMIZE does not work
class CommentAnnotation < Base
include AnnotationComment
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector
MSG_COLON_STYLE = 'Annotation keywords like `%<keyword>s` should be all ' \
'upper case, followed by a colon, and a space, ' \
'then a note describing the problem.'
MSG_SPACE_STYLE = 'Annotation keywords like `%<keyword>s` should be all ' \
'upper case, followed by a space, ' \
'then a note describing the problem.'
MISSING_NOTE = 'Annotation comment, with keyword `%<keyword>s`, is missing a note.'
def on_new_investigation
processed_source.comments.each_with_index do |comment, index|
next unless first_comment_line?(processed_source.comments, index) ||
inline_comment?(comment)
margin, first_word, colon, space, note = split_comment(comment)
next unless annotation?(comment) && !correct_annotation?(first_word, colon, space, note)
range = annotation_range(comment, margin, first_word, colon, space)
register_offense(range, note, first_word)
end
end
private
def register_offense(range, note, first_word)
message = if style == :colon
MSG_COLON_STYLE
else
MSG_SPACE_STYLE
end
add_offense(
range,
message: format(note ? message : MISSING_NOTE, keyword: first_word)
) do |corrector|
next if note.nil?
correct_offense(corrector, range, first_word)
end
end
def first_comment_line?(comments, index)
index.zero? || comments[index - 1].loc.line < comments[index].loc.line - 1
end
def inline_comment?(comment)
!comment_line?(comment.loc.expression.source_line)
end
def annotation_range(comment, margin, first_word, colon, space)
start = comment.loc.expression.begin_pos + margin.length
length = concat_length(first_word, colon, space)
range_between(start, start + length)
end
def concat_length(*args)
args.reduce(0) { |acc, elem| acc + elem.to_s.length }
end
def correct_annotation?(first_word, colon, space, note)
return correct_colon_annotation?(first_word, colon, space, note) if style == :colon
correct_space_annotation?(first_word, colon, space, note)
end
def correct_colon_annotation?(first_word, colon, space, note)
keyword?(first_word) && (colon && space && note || !colon && !note)
end
def correct_space_annotation?(first_word, colon, space, note)
keyword?(first_word) && (!colon && space && note || !colon && !note)
end
def correct_offense(corrector, range, first_word)
if style == :colon
corrector.replace(range, "#{first_word.upcase}: ")
else
corrector.replace(range, "#{first_word.upcase} ")
end
end
end
end
end
end