Skip to content

Commit

Permalink
feat(parser): add a common SARIF parser and use it for checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoschcau committed Jan 4, 2025
1 parent 1fea92f commit 1044c35
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 63 deletions.
12 changes: 4 additions & 8 deletions lua/lint/linters/checkstyle.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local format = '[%tRROR] %f:%l:%c: %m, [%tRROR] %f:%l: %m, [%tARN] %f:%l:%c: %m, [%tARN] %f:%l: %m, [%tNFO] %f:%l:%c: %m, [%tNFO] %f:%l: %m'

local M

local function config()
Expand All @@ -10,14 +8,12 @@ local function config()
end

M = {
cmd = 'checkstyle',
args = {'-c', config},
cmd = "checkstyle",
args = { "-f", "sarif", "-c", config },
ignore_exitcode = true,
parser = require('lint.parser').from_errorformat(format, {
source = 'checkstyle',
}),
parser = require("lint.parser").for_sarif({}, { default_end_col = "+1" }),
-- use the bundled Google style by default
config_file = '/google_checks.xml'
config_file = "/google_checks.xml",
}

return M
69 changes: 69 additions & 0 deletions lua/lint/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,75 @@ local severity_by_qftype = {
N = vd.severity.HINT,
}

---@class lint.SarifOptions
---@field default_end_col "+1" | "eol" the default end column, "+1" (defaults to the end of the line)

---Return a parse function for the Static Analysis Results Interchange Format (SARIF).
---https://sarifweb.azurewebsites.net/
---@param skeleton? table<string, any> | vim.Diagnostic default values
---@param opts? lint.SarifOptions SARIF-related options
---@return fun(output: string, bufnr: number): vim.Diagnostic[] parser a SARIF parser
function M.for_sarif(skeleton, opts)
skeleton = skeleton or {}
skeleton.severity = skeleton.severity or vd.severity.ERROR

opts = opts or {}
opts.default_end_col = opts.default_end_col or "eol"
local default_end_col = opts.default_end_col == "eol" and 999999 or nil

local severities = {
error = vd.severity.ERROR,
warning = vd.severity.WARN,
note = vd.severity.INFO,
}

---@param output string the output of the tool
---@param linter_bufnr number the number of the buffer the linter ran on
---@return vim.Diagnostic[] the diagnostics
return function(output, linter_bufnr)
local diagnostics = {}

local decoded = vim.json.decode(output) or {}

local run = decoded.runs and decoded.runs[1] or {}

local source = run.tool and run.tool.driver and run.tool.driver.name

local results = run.results or {}

for _, result in ipairs(results) do
for _, location in ipairs(result.locations) do
local file_bufnr =
vim.uri_to_bufnr(vim.uri_from_fname(vim.fs.abspath(location.physicalLocation.artifactLocation.uri)))

-- TODO: This check can be removed, once nvim-lint supports multiple
-- buffers.
-- https://github.com/mfussenegger/nvim-lint/issues/716
if linter_bufnr == file_bufnr then
local region = location.physicalLocation.region

table.insert(
diagnostics,
vim.tbl_extend("keep", {
bufnr = file_bufnr,
lnum = region.startLine - 1,
end_lnum = region.endLine and region.endLine - 1,
col = region.startColumn and region.startColumn - 1 or 0,
end_col = region.endColumn and region.endColumn - 2 or default_end_col,
severity = severities[result.level],
message = result.message.text,
source = source,
code = result.ruleId,
}, skeleton or {})
)
end
end
end

return diagnostics
end
end

---Return a parse function that uses an errorformat to parse the output.
---@param efm string Format following |errorformat|
---@param skeleton table<string, any> | vim.Diagnostic default values
Expand Down
55 changes: 0 additions & 55 deletions spec/checkstyle_spec.lua

This file was deleted.

0 comments on commit 1044c35

Please sign in to comment.