Skip to content

Commit

Permalink
[Fix rubocop#1901] Properly correct comments that are missing a note
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosenblum committed May 19, 2015
1 parent 7c7bc72 commit f74cfd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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)

Expand Down
29 changes: 19 additions & 10 deletions lib/rubocop/cop/style/comment_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions spec/rubocop/cop/style/comment_annotation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f74cfd3

Please sign in to comment.