diff --git a/rdjson_formatter/rdjson_formatter.rb b/rdjson_formatter/rdjson_formatter.rb index 2eea86b..14f1429 100644 --- a/rdjson_formatter/rdjson_formatter.rb +++ b/rdjson_formatter/rdjson_formatter.rb @@ -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 @@ -59,17 +57,7 @@ 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: { @@ -77,12 +65,28 @@ def build_diagnostic(file, offense) }, 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) diff --git a/test/rdjson_formatter/testdata/result.ok b/test/rdjson_formatter/testdata/result.ok index ba2b016..51116c8 100644 --- a/test/rdjson_formatter/testdata/result.ok +++ b/test/rdjson_formatter/testdata/result.ok @@ -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": {