Skip to content

Commit

Permalink
Support offenses with no location (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcallewaert authored Sep 19, 2024
1 parent d89ceac commit 5e23bb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
30 changes: 17 additions & 13 deletions rdjson_formatter/rdjson_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def started(_target_files)

def file_finished(file, offenses)
offenses.each do |offense|
next if offense.location == RuboCop::Cop::Offense::NO_LOCATION

@rdjson[:diagnostics] << build_diagnostic(file, offense)
end

Expand Down Expand Up @@ -59,30 +57,36 @@ def build_diagnostic(file, offense)
diagnostic = {
message: message,
location: {
path: convert_path(file),
range: {
start: {
line: offense.location.begin.line,
column: offense.location.begin.column + 1
},
end: {
line: offense.location.end.line,
column: offense.location.end.column + 1
}
}
path: convert_path(file)
},
severity: convert_severity(offense.severity),
code: {
value: code
},
original_output: build_original_output(file, offense)
}
diagnostic[:location][:range] = build_range(offense) if offense.location != RuboCop::Cop::Offense::NO_LOCATION

diagnostic[:suggestions] = build_suggestions(offense) if offense.correctable? && offense.corrector

diagnostic
end

# @param [RuboCop::Cop::Offense] offense
# @return [Hash]
def build_range(offense)
{
start: {
line: offense.location.begin.line,
column: offense.location.begin.column + 1
},
end: {
line: offense.location.end.line,
column: offense.location.end.column + 1
}
}
end

# @param [RuboCop::Cop::Offense] offense
# @return [Array{Hash}]
def build_suggestions(offense)
Expand Down
11 changes: 11 additions & 0 deletions test/rdjson_formatter/testdata/result.ok
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@
}
]
},
{
"message": "Empty file detected.",
"location": {
"path": "test/rdjson_formatter/testdata/global_offenses.rb"
},
"severity": "WARNING",
"code": {
"value": "Lint/EmptyFile"
},
"original_output": "test/rdjson_formatter/testdata/global_offenses.rb:1:1: W: Lint/EmptyFile: Empty file detected."
},
{
"message": "Method parameter must be at least 3 characters long.",
"location": {
Expand Down

0 comments on commit 5e23bb6

Please sign in to comment.