Skip to content

Commit 5020eab

Browse files
authored
Merge #4299 oxlint
2 parents aa42d75 + e852d6d commit 5020eab

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

lsp/oxlint.lua

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@
1010
--- ```sh
1111
--- npm i -g oxlint
1212
--- ```
13+
---
14+
--- Type-aware linting will automatically be enabled if `tsgolint` exists in your
15+
--- path and your `.oxlintrc.json` contains the string "typescript".
16+
---
17+
--- The default `on_attach` function provides an `:LspOxlintFixAll` command which
18+
--- can be used to fix all fixable diagnostics. See the `eslint` config entry for
19+
--- an example of how to use this to automatically fix all errors on write.
1320

14-
local util = require 'lspconfig.util'
21+
local function oxlint_conf_mentions_typescript(root_dir)
22+
local fn = vim.fs.joinpath(root_dir, '.oxlintrc.json')
23+
for line in io.lines(fn) do
24+
if line:find('typescript') then
25+
return true
26+
end
27+
end
28+
return false
29+
end
1530

1631
---@type vim.lsp.Config
1732
return {
1833
cmd = { 'oxlint', '--lsp' },
1934
filetypes = {
2035
'javascript',
2136
'javascriptreact',
22-
'javascript.jsx',
2337
'typescript',
2438
'typescriptreact',
25-
'typescript.tsx',
2639
'vue',
2740
'svelte',
2841
'astro',
2942
},
43+
root_markers = { '.oxlintrc.json' },
3044
workspace_required = true,
3145
on_attach = function(client, bufnr)
3246
vim.api.nvim_buf_create_user_command(bufnr, 'LspOxlintFixAll', function()
@@ -39,24 +53,26 @@ return {
3953
desc = 'Apply Oxlint automatic fixes',
4054
})
4155
end,
42-
root_dir = function(bufnr, on_dir)
43-
local fname = vim.api.nvim_buf_get_name(bufnr)
56+
settings = {
57+
-- run = 'onType',
58+
-- configPath = nil,
59+
-- tsConfigPath = nil,
60+
-- unusedDisableDirectives = 'allow',
61+
-- typeAware = false,
62+
-- disableNestedConfig = false,
63+
-- fixKind = 'safe_fix',
64+
},
65+
before_init = function(init_params, config)
66+
local settings = config.settings or {}
67+
if settings.typeAware == nil and vim.fn.executable('tsgolint') == 1 then
68+
local ok, res = pcall(oxlint_conf_mentions_typescript, config.root_dir)
69+
if ok and res then
70+
settings = vim.tbl_extend('force', settings, { typeAware = true })
71+
end
72+
end
73+
local init_options = config.init_options or {}
74+
init_options.settings = vim.tbl_extend('force', init_options.settings or {} --[[@as table]], settings)
4475

45-
-- Oxlint resolves configuration by walking upward and using the nearest config file
46-
-- to the file being processed. We therefore compute the root directory by locating
47-
-- the closest `.oxlintrc.json` (or `package.json` fallback) above the buffer.
48-
local root_markers = util.insert_package_json({ '.oxlintrc.json' }, 'oxlint', fname)[1]
49-
on_dir(vim.fs.dirname(vim.fs.find(root_markers, { path = fname, upward = true })[1]))
76+
init_params.initializationOptions = init_options
5077
end,
51-
init_options = {
52-
settings = {
53-
-- ['run'] = 'onType',
54-
-- ['configPath'] = nil,
55-
-- ['tsConfigPath'] = nil,
56-
-- ['unusedDisableDirectives'] = 'allow',
57-
-- ['typeAware'] = false,
58-
-- ['disableNestedConfig'] = false,
59-
-- ['fixKind'] = 'safe_fix',
60-
},
61-
},
6278
}

0 commit comments

Comments
 (0)