Skip to content

Commit

Permalink
Fix parsing regression in eslint(d) (#416)
Browse files Browse the repository at this point in the history
Follow up to #409
  • Loading branch information
sQVe authored Oct 17, 2023
1 parent 6d596b8 commit 7ac6307
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
21 changes: 13 additions & 8 deletions lua/lint/linters/eslint.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand Down
27 changes: 16 additions & 11 deletions lua/lint/linters/eslint_d.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/eslint_d_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("linter.eslint_d", function()
local parser = require("lint.linters.eslint_d").parser

local result = parser([[
{
[{
"messages": [
{
"column": 10,
Expand All @@ -30,7 +30,8 @@ describe("linter.eslint_d", function()
"severity": 2
}
]
}]])
}]
]])

assert.are.same(2, #result)

Expand Down

0 comments on commit 7ac6307

Please sign in to comment.