Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when highlights contain multibyte characters #9649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9649](https://github.com/rubocop/rubocop/pull/9649): Fix when highlights contain multibyte characters. ([@osyo-manga][])
6 changes: 4 additions & 2 deletions lib/rubocop/formatter/clang_style_formatter.rb
Expand Up @@ -49,8 +49,10 @@ def report_line(location)
end

def report_highlighted_area(highlighted_area)
output.puts("#{' ' * highlighted_area.begin_pos}" \
"#{'^' * highlighted_area.size}")
space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
source_area = highlighted_area.source
output.puts("#{' ' * Unicode::DisplayWidth.of(space_area)}" \
"#{'^' * Unicode::DisplayWidth.of(source_area)}")
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/formatter/tap_formatter.rb
Expand Up @@ -37,8 +37,10 @@ def report_line(location)
end

def report_highlighted_area(highlighted_area)
output.puts("# #{' ' * highlighted_area.begin_pos}" \
"#{'^' * highlighted_area.size}")
space_area = highlighted_area.source_buffer.slice(0...highlighted_area.begin_pos)
source_area = highlighted_area.source
output.puts("# #{' ' * Unicode::DisplayWidth.of(space_area)}" \
"#{'^' * Unicode::DisplayWidth.of(source_area)}")
end

def report_offense(file, offense)
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/formatter/clang_style_formatter_spec.rb
Expand Up @@ -123,5 +123,27 @@
.to include(': [Corrected] This is a message.')
end
end

context 'when the source contains multibyte characters' do
let(:source) do
<<~RUBY
do_something("あああ", ["いいい"])
RUBY
end

it 'displays text containing the offending source line' do
location = source_range(source.index('[')..source.index(']'))

cop.add_offense(nil, location: location, message: 'message 1')
formatter.report_file('test', cop.offenses)

expect(output.string)
.to eq <<~OUTPUT
test:1:21: C: message 1
do_something("あああ", ["いいい"])
^^^^^^^^^^
OUTPUT
end
end
end
end
29 changes: 29 additions & 0 deletions spec/rubocop/formatter/tap_formatter_spec.rb
Expand Up @@ -143,4 +143,33 @@
end
end
end

describe '#report_file', :config do
let(:cop_class) { RuboCop::Cop::Cop }
let(:output) { StringIO.new }

before { cop.send(:begin_investigation, processed_source) }

context 'when the source contains multibyte characters' do
let(:source) do
<<~RUBY
do_something("あああ", ["いいい"])
RUBY
end

it 'displays text containing the offending source line' do
location = source_range(source.index('[')..source.index(']'))

cop.add_offense(nil, location: location, message: 'message 1')
formatter.report_file('test', cop.offenses)

expect(output.string)
.to eq <<~OUTPUT
# test:1:21: C: message 1
# do_something("あああ", ["いいい"])
# ^^^^^^^^^^
OUTPUT
end
end
end
end