From 7ac6307eed35f1a15cb4f3692fddd6fc61ba47e6 Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Tue, 17 Oct 2023 17:01:07 +0200 Subject: [PATCH] Fix parsing regression in eslint(d) (#416) Follow up to https://github.com/mfussenegger/nvim-lint/pull/409 --- lua/lint/linters/eslint.lua | 21 +++++++++++++-------- lua/lint/linters/eslint_d.lua | 27 ++++++++++++++++----------- tests/eslint_d_spec.lua | 5 +++-- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lua/lint/linters/eslint.lua b/lua/lint/linters/eslint.lua index 4c796dcf..5e69c9b4 100644 --- a/lua/lint/linters/eslint.lua +++ b/lua/lint/linters/eslint.lua @@ -1,16 +1,17 @@ local severities = { - nil, - vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN, + vim.diagnostic.severity.ERROR, } return require('lint.util').inject_cmd_exe({ cmd = function() local local_eslint = vim.fn.fnamemodify('./node_modules/.bin/eslint', ':p') local stat = vim.loop.fs_stat(local_eslint) + if stat then return local_eslint end + return 'eslint' end, args = { @@ -24,17 +25,21 @@ return require('lint.util').inject_cmd_exe({ stream = 'stdout', ignore_exitcode = true, parser = function(output) - local success, decodedData = pcall(vim.json.decode, output) - local diagnostics = {} + local success, decodedData = pcall( + vim.json.decode, output, + { luanil = { object = true, array = true } } + ) + local messages = decodedData and decodedData[1] and decodedData[1].messages or {} - if success and decodedData ~= nil then - for _, diagnostic in ipairs(decodedData.messages or {}) do + local diagnostics = {} + if success and #messages > 0 then + for _, diagnostic in ipairs(messages or {}) do table.insert(diagnostics, { source = "eslint", lnum = diagnostic.line - 1, col = diagnostic.column - 1, - end_lnum = diagnostic.endLine - 1, - end_col = diagnostic.endColumn - 1, + end_lnum = (diagnostic.endLine or diagnostic.line) - 1, + end_col = (diagnostic.endColumn or diagnostic.column) - 1, severity = severities[diagnostic.severity], message = diagnostic.message, code = diagnostic.ruleId diff --git a/lua/lint/linters/eslint_d.lua b/lua/lint/linters/eslint_d.lua index 18724199..246f23ee 100644 --- a/lua/lint/linters/eslint_d.lua +++ b/lua/lint/linters/eslint_d.lua @@ -1,16 +1,17 @@ local severities = { - nil, - vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN, + vim.diagnostic.severity.ERROR, } return require('lint.util').inject_cmd_exe({ cmd = function() - local local_eslintd = vim.fn.fnamemodify('./node_modules/.bin/eslint_d', ':p') - local stat = vim.loop.fs_stat(local_eslintd) + local local_eslint_d = vim.fn.fnamemodify('./node_modules/.bin/eslint_d', ':p') + local stat = vim.loop.fs_stat(local_eslint_d) + if stat then - return local_eslintd + return local_eslint_d end + return 'eslint_d' end, args = { @@ -24,17 +25,21 @@ return require('lint.util').inject_cmd_exe({ stream = 'stdout', ignore_exitcode = true, parser = function(output) - local success, decodedData = pcall(vim.json.decode, output) - local diagnostics = {} + local success, decodedData = pcall( + vim.json.decode, output, + { luanil = { object = true, array = true } } + ) + local messages = decodedData and decodedData[1] and decodedData[1].messages or {} - if success and decodedData ~= nil then - for _, diagnostic in ipairs(decodedData.messages or {}) do + local diagnostics = {} + if success and #messages > 0 then + for _, diagnostic in ipairs(messages or {}) do table.insert(diagnostics, { source = "eslint_d", lnum = diagnostic.line - 1, col = diagnostic.column - 1, - end_lnum = diagnostic.endLine - 1, - end_col = diagnostic.endColumn - 1, + end_lnum = (diagnostic.endLine or diagnostic.line) - 1, + end_col = (diagnostic.endColumn or diagnostic.column) - 1, severity = severities[diagnostic.severity], message = diagnostic.message, code = diagnostic.ruleId diff --git a/tests/eslint_d_spec.lua b/tests/eslint_d_spec.lua index a1f6cabb..797871ed 100644 --- a/tests/eslint_d_spec.lua +++ b/tests/eslint_d_spec.lua @@ -9,7 +9,7 @@ describe("linter.eslint_d", function() local parser = require("lint.linters.eslint_d").parser local result = parser([[ -{ +[{ "messages": [ { "column": 10, @@ -30,7 +30,8 @@ describe("linter.eslint_d", function() "severity": 2 } ] -}]]) +}] +]]) assert.are.same(2, #result)