Skip to content

Commit

Permalink
[Fix rubocop#3230] Add smart highlighting for Style/AsciiComments c…
Browse files Browse the repository at this point in the history
…op (rubocop#3279)

This change adds smarter highlighting for the `Style/AsciiComments`
cop. It will highlight the first consecutive range of non-ascii
characters in the comments, rather than the whole expression.
  • Loading branch information
Drenmi authored and Neodelf committed Oct 15, 2016
1 parent a4e7eb8 commit 334be7a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
* [#3247](https://github.com/bbatsov/rubocop/issues/3247): Ensure whitespace after beginning of block in `Style/BlockDelimiters`. ([@tjwp][])
* [#2941](https://github.com/bbatsov/rubocop/issues/2941): Make sure `Lint/UnneededDisable` can do auto-correction. ([@jonas054][])

### Changes

* [#3230](https://github.com/bbatsov/rubocop/issues/3230): Improve highlighting for `Style/AsciiComments` cop. ([@drenmi][])

## 0.41.1 (2016-06-26)

### Bug fixes
Expand Down
22 changes: 21 additions & 1 deletion lib/rubocop/cop/style/ascii_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@ class AsciiComments < Cop

def investigate(processed_source)
processed_source.comments.each do |comment|
add_offense(comment, :expression) unless comment.text.ascii_only?
unless comment.text.ascii_only?
add_offense(comment, first_offense_range(comment))
end
end
end

private

def first_offense_range(comment)
expression = comment.loc.expression
first_offense = first_non_ascii_chars(comment.text)

start_position = expression.begin_pos +
comment.text.index(first_offense)
end_position = start_position + first_offense.length

Parser::Source::Range.new(comment.loc.expression.source_buffer,
start_position, end_position)
end

def first_non_ascii_chars(string)
string.match(/[^[:ascii:]]+/).to_s
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/html_formatter/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ <h1 class="title">RuboCop Inspection Report</h1>
<span class="message">Use only ascii symbols in comments.</span>
</div>

<pre><code> <span class="highlight convention"># “Test encoding issues by using curly quotes”</span></code></pre>
<pre><code> # <span class="highlight convention"></span>Test encoding issues by using curly quotes”</code></pre>

</div>

Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/ascii_comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
['# encoding: utf-8',
'# 这是什么?'])
expect(cop.offenses.size).to eq(1)
expect(cop.highlights).to eq(['这是什么?'])
expect(cop.messages)
.to eq(['Use only ascii symbols in comments.'])
end

it 'registers an offense for commentes with mixed chars' do
inspect_source(cop,
['# encoding: utf-8',
'# foo ∂ bar'])
expect(cop.offenses.size).to eq(1)
expect(cop.highlights).to eq(['∂'])
expect(cop.messages)
.to eq(['Use only ascii symbols in comments.'])
end
Expand Down

0 comments on commit 334be7a

Please sign in to comment.