/ nvim / lua / lsp / servers / eslint.lua
eslint.lua
 1  return {
 2    {
 3      'neovim/nvim-lspconfig',
 4      opts = {
 5        codelens = {
 6          enabled = true,
 7        },
 8        servers = {
 9          eslint = {
10            settings = {
11              -- helps eslint find the eslintrc when it's placed in a subfolder instead of the cwd root
12              workingDirectories = { mode = 'auto' },
13              format = true,
14              showDocumentation = {
15                enable = true,
16              },
17              command = { 'vscode-eslint-language-server', '--stdio' },
18              init_options = {
19                configurationSection = { 'html', 'css', 'javascript' },
20                embeddedLanguages = {
21                  css = true,
22                  javascript = true,
23                },
24                provideFormatter = true,
25              },
26              filetypes = {
27                'javascript',
28                'javascriptreact',
29                'javascript.jsx',
30                'typescript',
31                'typescriptreact',
32                'typescript.tsx',
33                'astro',
34              },
35            },
36          },
37        },
38        setup = {
39          eslint = function()
40            local function get_client(buf)
41              return require('lazyvim.util').lsp.get_clients({ name = 'eslint', bufnr = buf })[1]
42            end
43  
44            local formatter = require('lazyvim.util').lsp.formatter({
45              name = 'eslint: lsp',
46              primary = true,
47              priority = 200,
48              filter = 'eslint',
49            })
50  
51            -- Use EslintFixAll on Neovim < 0.10.0
52            if not pcall(require, 'vim.lsp._dynamic') then
53              formatter.name = 'eslint: EslintFixAll'
54              formatter.sources = function(buf)
55                local client = get_client(buf)
56                return client and { 'eslint' } or {}
57              end
58              formatter.format = function(buf)
59                local client = get_client(buf)
60                if client then
61                  local diag = vim.diagnostic.get(buf, {
62                    namespace = vim.lsp.diagnostic.get_namespace(client.id),
63                  })
64                  if #diag > 0 then
65                    vim.cmd('EslintFixAll')
66                  end
67                end
68              end
69            end
70  
71            -- register the formatter with LazyVim
72            require('lazyvim.util').format.register(formatter)
73          end,
74        },
75      },
76    },
77  }