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 suggestions with multiple corrections #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions rdjson_formatter/rdjson_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,45 @@

# @param [RuboCop::Cop::Offense] offense
# @return [Array{Hash}]
def build_suggestions(offense)

Check warning on line 92 in rdjson_formatter/rdjson_formatter.rb

View workflow job for this annotation

GitHub Actions / test-skip-install-and-use-bundler

[rubocop] reported by reviewdog 🐶 Cyclomatic complexity for build_suggestions is too high. [12/7] Raw Output: rdjson_formatter/rdjson_formatter.rb:92:3: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for build_suggestions is too high. [12/7]

Check warning on line 92 in rdjson_formatter/rdjson_formatter.rb

View workflow job for this annotation

GitHub Actions / test-skip-install-and-use-bundler

[rubocop] reported by reviewdog 🐶 Perceived complexity for build_suggestions is too high. [12/8] Raw Output: rdjson_formatter/rdjson_formatter.rb:92:3: C: Metrics/PerceivedComplexity: Perceived complexity for build_suggestions is too high. [12/8]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has two Rubocop metrics based warnings since it's a larger method now. Perhaps determining the positions/range and sorted corrections could each be extracted into a private method?

We don't see to have tests for this other than integration tests through Rubocop CLI. If its easier to write a unit test that would be fine but is there anyway we could update the test data with samples that would trigger the warnings before this change and then include it here to demonstrate that the fix is working as expected?

range, text = offense.corrector.as_replacements[0]

[
{
range: {
start: {
line: range.begin.line,
column: range.begin.column + 1 # rubocop is 0-origin, reviewdog is 1-origin
},
end: {
line: range.end.line,
column: range.end.column + 1
}
return [] unless offense.correctable? && offense.corrector

source_buffer = offense.location.source_buffer
corrections = offense.corrector.as_replacements
return [] if corrections.empty?

min_begin_pos = corrections.map { |range, _| range.begin_pos }.min
max_end_pos = corrections.map { |range, _| range.end_pos }.max
merged_range = Parser::Source::Range.new(source_buffer, min_begin_pos, max_end_pos)

corrected_text = ''
current_pos = min_begin_pos

sorted_corrections = corrections.sort_by { |range, _| range.begin_pos }

sorted_corrections.each do |range, replacement_text|
next if range.end_pos < min_begin_pos || range.begin_pos > max_end_pos

corrected_text += source_buffer.source[current_pos...range.begin_pos] if current_pos < range.begin_pos
corrected_text += replacement_text.to_s
current_pos = range.end_pos
end

corrected_text += source_buffer.source[current_pos...max_end_pos] if current_pos < max_end_pos

[{
range: {
start: {
line: merged_range.line,
column: merged_range.column + 1 # rubocop is 0-origin, reviewdog is 1-origin
},
text: text
}
]
end: {
line: merged_range.last_line,
column: merged_range.last_column + 1
}
},
text: corrected_text
}]
end

# https://github.com/reviewdog/reviewdog/blob/1d8f6d6897dcfa67c33a2ccdc2ea23a8cca96c8c/proto/rdf/reviewdog.proto
Expand Down