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 #10241] Fix last_column value for JSONFormatter #10242

Merged
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
1 change: 1 addition & 0 deletions changelog/fix_last_column_number_for_json_formatter.md
@@ -0,0 +1 @@
* [#10242](https://github.com/rubocop/rubocop/pull/10242): Fix `last_column` value for `JSONFormatter`. ([@koic][])
5 changes: 4 additions & 1 deletion lib/rubocop/formatter/json_formatter.rb
Expand Up @@ -59,12 +59,15 @@ def hash_for_offense(offense)
end

# TODO: Consider better solution for Offense#real_column.
# The minimum value of `start_column: real_column` is 1.
# So, the minimum value of `last_column` should be 1.
# And non-zero value of `last_column` should be used as is.
def hash_for_location(offense)
{
start_line: offense.line,
start_column: offense.real_column,
last_line: offense.last_line,
last_column: offense.last_column,
last_column: offense.last_column.zero? ? 1 : offense.last_column,

Choose a reason for hiding this comment

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

Sorry, I'm not familiar with the Rubocop codebase.

This change prevents a situation start_column==1, last_column==0 from happening, but can something like start_column==5, last_column==4 still happen?

length: offense.location.length,
# `line` and `column` exist for compatibility.
# Use `start_line` and `start_column` instead.
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/formatter/json_formatter_spec.rb
Expand Up @@ -166,7 +166,7 @@
start_line: 1,
start_column: 1,
last_line: 1,
last_column: 0,
last_column: 1,
length: 0,
line: 1,
column: 1
Expand Down