diff --git a/lua/lint/linters/eslint.lua b/lua/lint/linters/eslint.lua index 4c796dcf..9cb328e2 100644 --- a/lua/lint/linters/eslint.lua +++ b/lua/lint/linters/eslint.lua @@ -1,7 +1,9 @@ -local severities = { - nil, - vim.diagnostic.severity.ERROR, - vim.diagnostic.severity.WARN, +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, } return require('lint.util').inject_cmd_exe({ @@ -14,8 +16,6 @@ return require('lint.util').inject_cmd_exe({ return 'eslint' end, args = { - '--format', - 'json', '--stdin', '--stdin-filename', function() return vim.api.nvim_buf_get_name(0) end, @@ -23,25 +23,5 @@ return require('lint.util').inject_cmd_exe({ stdin = true, stream = 'stdout', ignore_exitcode = true, - 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 + parser = require('lint.parser').from_pattern(pattern, groups, severity_map, { ['source'] = 'eslint' }), }) diff --git a/lua/lint/linters/eslint_d.lua b/lua/lint/linters/eslint_d.lua index 18724199..378f956a 100644 --- a/lua/lint/linters/eslint_d.lua +++ b/lua/lint/linters/eslint_d.lua @@ -1,7 +1,9 @@ -local severities = { - nil, - vim.diagnostic.severity.ERROR, - vim.diagnostic.severity.WARN, +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, } return require('lint.util').inject_cmd_exe({ @@ -14,8 +16,6 @@ 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, @@ -23,25 +23,5 @@ return require('lint.util').inject_cmd_exe({ stdin = true, stream = 'stdout', ignore_exitcode = true, - 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 + parser = require('lint.parser').from_pattern(pattern, groups, severity_map, { ['source'] = 'eslint_d' }), }) diff --git a/tests/eslint_d_spec.lua b/tests/eslint_d_spec.lua index a1f6cabb..d9bf668f 100644 --- a/tests/eslint_d_spec.lua +++ b/tests/eslint_d_spec.lua @@ -7,55 +7,48 @@ 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 - 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 - } - ] -}]]) - +✖ 2 problems (2 errors, 0 warnings) + ]], + vim.api.nvim_get_current_buf() + ) assert.are.same(2, #result) - local expected_1 = { code = "no-unused-vars", col = 9, - end_col = 17, + end_col = 9, 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 = 21, + end_col = 15, 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)