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

Add hlint #106

Merged
merged 7 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Other dedicated linters that are built-in are:
| [Flake8][13] | `flake8` |
| [Golangci-lint][16] | `golangcilint` |
| [hadolint][28] | `hadolint` |
| [hlint][32] | `hlint` |
| [HTML Tidy][12] | `tidy` |
| [Inko][17] | `inko` |
| [Languagetool][5] | `languagetool` |
Expand Down Expand Up @@ -304,6 +305,7 @@ nvim --headless --noplugin -u tests/minimal.vim -c "PlenaryBustedDirectory tests
[29]: https://github.com/stylelint/stylelint
[30]: https://github.com/KDE/clazy
[31]: https://github.com/Kampfkarren/selene
[32]: https://github.com/ndmitchell/hlint
[null-ls]: https://github.com/jose-elias-alvarez/null-ls.nvim
[plenary]: https://github.com/nvim-lua/plenary.nvim
[ansible-lint]: https://docs.ansible.com/lint.html
Expand Down
26 changes: 26 additions & 0 deletions lua/lint/linters/hlint.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local severities = {
error = vim.lsp.protocol.DiagnosticSeverity.Error,
warning = vim.lsp.protocol.DiagnosticSeverity.Warning,
suggestion = vim.lsp.protocol.DiagnosticSeverity.Hint,
}

return {
cmd = "hlint",
args = { "--json" },
parser = function(output)
local diagnostics = {}
local items = #output > 0 and vim.fn.json_decode(output) or {}
for _, item in ipairs(items) do
table.insert(diagnostics, {
range = {
["start"] = { line = item.startLine, character = item.startColumn },
["end"] = { line = item.endLine, character = item.endColumn },
},
severity = severities[item.severity:lower()],
source = "hlint",
message = item.hint,
})
end
return diagnostics
end,
}
59 changes: 59 additions & 0 deletions tests/hlint_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
describe("linter.hlint", function()
it("can parse an error", function()
-- Main.hs
--
-- (first
--
-- hlint Main.hs --json
local parser = require("lint.linters.hlint").parser
local result = parser([[
[{"module":[],"decl":[],"severity":"Error","hint":"Parse error: possibly incorrect indentation or mismatched brackets","file":"Main.hs","startLine":2,"startColumn":1,"endLine":2,"endColumn":1,"from":" (first\n> \n","to":null,"note":[],"refactorings":"[]"}]
]])
assert.are.same(#result, 1)
local expected = {
range = {
["start"] = {
character = 1,
line = 2,
},
["end"] = {
character = 1,
line = 2,
},
},
severity = vim.lsp.protocol.DiagnosticSeverity.Error,
source = "hlint",
message = "Parse error: possibly incorrect indentation or mismatched brackets",
}
assert.are.same(result[1], expected)
end)

it("can parse a warning", function()
-- Main.hs
--
-- concat (map f x)
--
-- hlint Main.hs --json
local parser = require("lint.linters.hlint").parser
local result = parser([[
[{"module":["Main"],"decl":[],"severity":"Warning","hint":"Use concatMap","file":"Main.hs","startLine":1,"startColumn":1,"endLine":1,"endColumn":17,"from":"concat (map f x)","to":"concatMap f x","note":[],"refactorings":"[Replace {rtype = Expr, pos = SrcSpan {startLine = 1, startCol = 1, endLine = 1, endCol = 17}, subts = [(\"f\",SrcSpan {startLine = 1, startCol = 13, endLine = 1, endCol = 14}),(\"x\",SrcSpan {startLine = 1, startCol = 15, endLine = 1, endCol = 16})], orig = \"concatMap f x\"}]"}]
]])
assert.are.same(#result, 1)
local expected = {
range = {
["start"] = {
character = 1,
line = 1,
},
["end"] = {
character = 17,
line = 1,
},
},
severity = vim.lsp.protocol.DiagnosticSeverity.Warning,
source = "hlint",
message = "Use concatMap",
}
assert.are.same(result[1], expected)
end)
end)