Skip to content

Commit

Permalink
fix: support end col for eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
sQVe committed Oct 12, 2023
1 parent 0f54481 commit 2378779
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
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
return local_eslint
return local_eslintd
end
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 json = vim.json.decode(output) or {}
local diagnostics = {}

for _, file in ipairs(json or {}) do
for _, diagnostic in ipairs(file.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 json = vim.json.decode(output) or {}
local diagnostics = {}

for _, file in ipairs(json or {}) do
for _, diagnostic in ipairs(file.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
})
40 changes: 29 additions & 11 deletions tests/eslint_d_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@ 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 no-dupe-else-if",
ruleId = "no-dupe-else-if",
severity = 2
}
},
}
]], vim.api.nvim_get_current_buf()
)
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",
Expand All @@ -38,10 +55,11 @@ describe("linter.eslint_d", function()
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 = {
Expand Down

0 comments on commit 2378779

Please sign in to comment.