Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support end col for eslint #409

Merged
merged 4 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lua/lint/linters/eslint.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
local pattern = [[%s*(%d+):(%d+)%s+(%w+)%s+(.+%S)%s+(%S+)]]
local groups = { 'lnum', 'col', 'severity', 'message', 'code' }
local severity_map = {
['error'] = vim.diagnostic.severity.ERROR,
['warn'] = vim.diagnostic.severity.WARN,
['warning'] = vim.diagnostic.severity.WARN,
local severities = {
nil,
vim.diagnostic.severity.ERROR,
vim.diagnostic.severity.WARN,
}

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)
local local_eslintd = vim.fn.fnamemodify('./node_modules/.bin/eslint', ':p')
local stat = vim.loop.fs_stat(local_eslintd)
if stat then
mfussenegger marked this conversation as resolved.
Show resolved Hide resolved
return local_eslint
return local_eslintd
end
mfussenegger marked this conversation as resolved.
Show resolved Hide resolved
return 'eslint'
end,
args = {
'--format',
'json',
'--stdin',
'--stdin-filename',
function() return vim.api.nvim_buf_get_name(0) end,
},
stdin = true,
stream = 'stdout',
ignore_exitcode = true,
parser = require('lint.parser').from_pattern(pattern, groups, severity_map, { ['source'] = 'eslint' }),
parser = function(output)
local success, decodedData = pcall(vim.json.decode, output)
local diagnostics = {}

if success and decodedData ~= nil then
for _, diagnostic in ipairs(decodedData.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,
severity = severities[diagnostic.severity],
message = diagnostic.message,
code = diagnostic.ruleId
})
end
end

return diagnostics
end
})
34 changes: 27 additions & 7 deletions lua/lint/linters/eslint_d.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local pattern = [[%s*(%d+):(%d+)%s+(%w+)%s+(.+%S)%s+(%S+)]]
local groups = { 'lnum', 'col', 'severity', 'message', 'code' }
local severity_map = {
['error'] = vim.diagnostic.severity.ERROR,
['warn'] = vim.diagnostic.severity.WARN,
['warning'] = vim.diagnostic.severity.WARN,
local severities = {
nil,
vim.diagnostic.severity.ERROR,
vim.diagnostic.severity.WARN,
}

return require('lint.util').inject_cmd_exe({
Expand All @@ -16,12 +14,34 @@ return require('lint.util').inject_cmd_exe({
return 'eslint_d'
end,
args = {
'--format',
'json',
'--stdin',
'--stdin-filename',
function() return vim.api.nvim_buf_get_name(0) end,
},
stdin = true,
stream = 'stdout',
ignore_exitcode = true,
parser = require('lint.parser').from_pattern(pattern, groups, severity_map, { ['source'] = 'eslint_d' }),
parser = function(output)
local success, decodedData = pcall(vim.json.decode, output)
local diagnostics = {}

if success and decodedData ~= nil then
for _, diagnostic in ipairs(decodedData.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,
severity = severities[diagnostic.severity],
message = diagnostic.message,
code = diagnostic.ruleId
})
end
end

return diagnostics
end
})
51 changes: 29 additions & 22 deletions tests/eslint_d_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,55 @@ describe("linter.eslint_d", function()

it("can parse output", function()
local parser = require("lint.linters.eslint_d").parser
local result = parser(
[[
/directory/file.js
1:10 error 'testFunc' is defined but never used no-unused-vars
4:16 error This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain no-dupe-else-if

✖ 2 problems (2 errors, 0 warnings)
]],
vim.api.nvim_get_current_buf()
)
local result = parser([[
{
"messages": [
{
"column": 10,
"endColumn": 18,
"endLine": 1,
"line": 1,
"message": "'testFunc' is defined but never used",
"ruleId": "no-unused-vars",
"severity": 2
},
{
"column": 16,
"endColumn": 22,
"endLine": 4,
"line": 4,
"message": "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain",
"ruleId": "no-dupe-else-if",
"severity": 2
}
]
}]])

assert.are.same(2, #result)

local expected_1 = {
code = "no-unused-vars",
col = 9,
end_col = 9,
end_col = 17,
end_lnum = 0,
lnum = 0,
message = "'testFunc' is defined but never used",
severity = 1,
source = "eslint_d",
user_data = {
lsp = {
code = "no-unused-vars",
},
},
}
assert.are.same(expected_1, result[1])

local expected_2 = {
code = "no-dupe-else-if",
col = 15,
end_col = 15,
end_col = 21,
end_lnum = 3,
lnum = 3,
message = "This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain",
message =
"This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain",
severity = 1,
source = "eslint_d",
user_data = {
lsp = {
code = "no-dupe-else-if",
},
},
}
assert.are.same(expected_2, result[2])
end)
Expand Down
Loading