Skip to content

Commit

Permalink
Merge pull request #84 from thedyrt/fix-severity
Browse files Browse the repository at this point in the history
Fix severity conversion; calculate overall severity based on RuboCop fail level
  • Loading branch information
mgrachev committed Jun 7, 2023
2 parents eb901a2 + ff0e80a commit c3efa96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
32 changes: 26 additions & 6 deletions rdjson_formatter/rdjson_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# https://docs.rubocop.org/rubocop/formatters.html
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/ClassLength
class RdjsonFormatter < RuboCop::Formatter::BaseFormatter
include RuboCop::PathUtil

Expand All @@ -22,6 +22,8 @@ def file_finished(file, offenses)

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

@rdjson[:severity] = overall_severity(offenses)
end

def finished(_inspected_files)
Expand All @@ -30,6 +32,24 @@ def finished(_inspected_files)

private

def overall_severity(offenses)
if offenses.any? { |o| o.severity >= minimum_severity_to_fail }
'ERROR'
elsif offenses.all? { |o| convert_severity(o.severity) == 'INFO' }
'INFO'
else
'WARNING'
end
end

def minimum_severity_to_fail
@minimum_severity_to_fail ||= begin
# Unless given explicitly as `fail_level`, `:info` severity offenses do not fail
name = options.fetch(:fail_level, :refactor)
RuboCop::Cop::Severity.new(name)
end
end

# @param [String] file
# @param [RuboCop::Cop::Offense] offense
# @return [Hash]
Expand Down Expand Up @@ -91,12 +111,12 @@ def build_suggestions(offense)
# @param [Symbol] severity
# @return [String]
def convert_severity(severity)
case severity
when :info
case severity.to_s
when 'info', 'refactor', 'convention'
'INFO'
when :warning
when 'warning'
'WARNING'
when :error
when 'error'
'ERROR'
else
'UNKNOWN_SEVERITY'
Expand Down Expand Up @@ -136,4 +156,4 @@ def build_original_output(file, offense)
)
end
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/ClassLength
42 changes: 29 additions & 13 deletions test/rdjson_formatter/testdata/result.ok
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Style/FrozenStringLiteralComment"
},
Expand Down Expand Up @@ -55,7 +55,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Layout/SpaceAfterComma"
},
Expand Down Expand Up @@ -91,7 +91,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Layout/ExtraSpacing"
},
Expand Down Expand Up @@ -127,7 +127,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Layout/SpaceAroundOperators"
},
Expand Down Expand Up @@ -163,7 +163,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Layout/ExtraSpacing"
},
Expand Down Expand Up @@ -199,7 +199,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Style/Semicolon"
},
Expand Down Expand Up @@ -235,7 +235,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Style/RedundantReturn"
},
Expand Down Expand Up @@ -271,7 +271,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Layout/ExtraSpacing"
},
Expand Down Expand Up @@ -307,7 +307,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Naming/MethodParameterName"
},
Expand All @@ -328,7 +328,7 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "INFO",
"code": {
"value": "Naming/MethodParameterName"
},
Expand All @@ -349,11 +349,27 @@
}
}
},
"severity": "UNKNOWN_SEVERITY",
"severity": "WARNING",
"code": {
"value": "Lint/UselessAssignment"
},
"original_output": "test/rdjson_formatter/testdata/not_correctable_offenses.rb:5:5: W: Lint/UselessAssignment: Useless assignment to variable - `c`."
"original_output": "test/rdjson_formatter/testdata/not_correctable_offenses.rb:5:5: W: Lint/UselessAssignment: Useless assignment to variable - `c`.",
"suggestions": [
{
"range": {
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 14
}
},
"text": "a + b"
}
]
}
]
],
"severity": "ERROR"
}

0 comments on commit c3efa96

Please sign in to comment.