From f58dcc132764b027471b9e7809765666dc3e9ecd Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 18 May 2015 22:40:49 -0400 Subject: [PATCH] [Fix #1901] Properly correct comments that are missing a note --- CHANGELOG.md | 1 + lib/rubocop/cop/style/comment_annotation.rb | 29 ++++++++++++------- .../cop/style/comment_annotation_spec.rb | 9 ++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97424e4fab5e..75f457a00310 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ * [#1899](https://github.com/bbatsov/rubocop/issues/1899): Be careful about comments when auto-correcting in `BracesAroundHashParameters`. ([@jonas054][]) * [#1897](https://github.com/bbatsov/rubocop/issues/1897): Don't report that semicolon separated statements can be converted to modifier form in `IfUnlessModifier` (and don't auto-correct them). ([@jonas054][]) * [#1644](https://github.com/bbatsov/rubocop/issues/1644): Don't search the entire file system when a folder is named `,` (fix for jruby and rbx). ([@rrosenblum][]) +* [#1901](https://github.com/bbatsov/rubocop/issues/1901): Properly fix comments that are missing a note. ([@rrosenblum][]) ## 0.31.0 (05/05/2015) diff --git a/lib/rubocop/cop/style/comment_annotation.rb b/lib/rubocop/cop/style/comment_annotation.rb index c2cf7e65399c..2432c8937e51 100644 --- a/lib/rubocop/cop/style/comment_annotation.rb +++ b/lib/rubocop/cop/style/comment_annotation.rb @@ -13,25 +13,34 @@ class CommentAnnotation < Cop def investigate(processed_source) processed_source.comments.each do |comment| - margin, first_word, colon, space, note = split_comment(comment) + _margin, first_word, colon, space, note = split_comment(comment) next unless annotation?(comment) && !correct_annotation?(first_word, colon, space, note) - start = comment.loc.expression.begin_pos + margin.length - length = first_word.length + colon.to_s.length + space.to_s.length - range = Parser::Source::Range.new(processed_source.buffer, - start, - start + length) - add_offense(range, range) + add_offense(comment, :expression) end end private - def autocorrect(range) + def autocorrect(comment) + margin, first_word, colon, space, note = split_comment(comment) + start = comment.loc.expression.begin_pos + margin.length + lambda do |corrector| - annotation_keyword = range.source.split(/:?\s+/).first - corrector.replace(range, annotation_keyword.upcase << ': ') + if note.nil? + start += first_word.length + range = Parser::Source::Range.new(comment, + start, + comment.loc.expression.end_pos) + corrector.remove(range) + else + length = first_word.length + colon.to_s.length + space.to_s.length + range = Parser::Source::Range.new(comment.loc.expression.source, + start, + start + length) + corrector.replace(range, "#{first_word.upcase}: ") + end end end diff --git a/spec/rubocop/cop/style/comment_annotation_spec.rb b/spec/rubocop/cop/style/comment_annotation_spec.rb index 78d93e194dd5..7acce99fa9ea 100644 --- a/spec/rubocop/cop/style/comment_annotation_spec.rb +++ b/spec/rubocop/cop/style/comment_annotation_spec.rb @@ -39,9 +39,9 @@ it 'marks the annotation keyword' do inspect_source(cop, '# TODO:make better') formatter.report_file('t', cop.offenses) - expect(output.string).to eq(["t:1:3: C: #{described_class::MSG}", + expect(output.string).to eq(["t:1:1: C: #{described_class::MSG}", '# TODO:make better', - ' ^^^^^', + '^^^^^^^^^^^^^^^^^^', ''].join("\n")) end end @@ -71,6 +71,11 @@ expect(cop.offenses.size).to eq(1) end + it 'autocorrects when missing the note' do + corrected = autocorrect_source(cop, '# HACK:') + expect(corrected).to eq('# HACK') + end + it 'accepts upper case keyword with colon, space and note' do inspect_source(cop, '# REVIEW: not sure about this') expect(cop.offenses).to be_empty