Skip to content

Commit

Permalink
[Fix #9890] Make Colon After Comment Annotation Configurable (#9899)
Browse files Browse the repository at this point in the history
Make the separator for the Style/CommentAnnotation cop configurable. By default, the enforced style will be the current behaviour of annotation keyword followed by a colon (`:`), and a space.
  • Loading branch information
gregfletch committed Jul 1, 2021
1 parent 60125ad commit 8333e8f
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5722,3 +5722,4 @@
[@timlkelly]: https://github.com/timlkelly
[@AirWick219]: https://github.com/AirWick219
[@markburns]: https://github.com/markburns
[@gregfletch]: https://github.com/gregfletch
@@ -0,0 +1 @@
* [#9890](https://github.com/rubocop/rubocop/issues/9890): Make colon after comment annotation configurable. ([@gregfletch][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3150,6 +3150,7 @@ Style/CommentAnnotation:
- HACK
- REVIEW
- NOTE
RequireColon: true

Style/CommentedKeyword:
Description: 'Do not place comments on the same line as certain keywords.'
Expand Down
56 changes: 50 additions & 6 deletions lib/rubocop/cop/style/comment_annotation.rb
Expand Up @@ -12,7 +12,7 @@ module Style
# incorrect registering of keywords (eg. `review`) inside a paragraph as an
# annotation.
#
# @example
# @example RequireColon: true (default)
# # bad
# # TODO make better
#
Expand All @@ -36,14 +36,36 @@ module Style
#
# # good
# # OPTIMIZE: does not work
#
# @example RequireColon: false
# # 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 RangeHelp
extend AutoCorrector

MSG = 'Annotation keywords like `%<keyword>s` should be all ' \
'upper case, followed by a colon, and a space, ' \
'then a note describing the problem.'
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
Expand All @@ -63,13 +85,15 @@ def on_new_investigation
private

def register_offense(range, note, first_word)
message = requires_colon? ? MSG_COLON_STYLE : MSG_SPACE_STYLE

add_offense(
range,
message: format(note ? MSG : MISSING_NOTE, keyword: first_word)
message: format(note ? message : MISSING_NOTE, keyword: first_word)
) do |corrector|
next if note.nil?

corrector.replace(range, "#{first_word.upcase}: ")
correct_offense(corrector, range, first_word)
end
end

Expand All @@ -92,8 +116,28 @@ def concat_length(*args)
end

def correct_annotation?(first_word, colon, space, note)
return correct_colon_annotation?(first_word, colon, space, note) if requires_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)
return corrector.replace(range, "#{first_word.upcase}: ") if requires_colon?

corrector.replace(range, "#{first_word.upcase} ")
end

def requires_colon?
cop_config['RequireColon']
end
end
end
end
Expand Down

0 comments on commit 8333e8f

Please sign in to comment.