How to set jsonls to hide the errors for comments?

I’m using jsonls as a language server for JSON file. It works fine without having comments in JSON file like tsconfig.json in which jsonls shows a lot of errors like this;

Also, I know that JSON doesn’t allow comments in it, but tsconfg.json originally had comments when it was generated.

Is there anyone who knows how to prevent these errors by configuring something?

The original tsconfig.json was very likely in JSON5 format which allows comments.

I don’t know if the language server supports JSON5 or if you need to install support for the corresponding filetype.

Hope this helps.

:set filetype=jsonc might help

I was annoyed by this and copied some logic I had for ignoring some tsserver errors. It looks something like this:

local opd = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics)
require("lspconfig").jsonls.setup({
        capabilities = capabilities,
        filetypes = { "json", "jsonc", "json5" },
        init_options = {
                provideFormatter = false,
        },
        handlers = {
                ["textDocument/publishDiagnostics"] = function(err, result, ctx, config)
                        -- jsonls doesn't really support json5
                        -- remove some annoying errors
                        if string.match(result.uri, "%.json5$", -6) and result.diagnostics ~= nil then
                                local idx = 1
                                while idx <= #result.diagnostics do
                                        -- "Comments are not permitted in JSON."
                                        if result.diagnostics[idx].code == 521 then
                                                table.remove(result.diagnostics, idx)
                                        else
                                                idx = idx + 1
                                        end
                                end
                        end
                        opd(err, result, ctx, config)
                end,
        },
})

From my dotfiles

1 Like