Skip to content

Commit

Permalink
[Fix #10241] Fix last_column value for JSONFormatter
Browse files Browse the repository at this point in the history
Fixes #10241.

This PR fixes `last_column` value for `JSONFormatter`.

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.

This patch to `last_column` seems to be required only for `JSONFormatter` and not required for other formatters.

It solves a potential `JSONFormatter` issue for cops that use `add_global_offense`.
  • Loading branch information
koic committed Nov 12, 2021
1 parent 6419a29 commit 965654f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
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,
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

0 comments on commit 965654f

Please sign in to comment.