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

refactor: respect package.json field name in cosmiconfig #67

Merged
merged 1 commit into from
Jan 24, 2024
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: 1 addition & 1 deletion lua/null-ls/builtins/code_actions/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ return h.make_builtin({
end,
dynamic_command = cmd_resolver.from_node_modules(),
cwd = h.cache.by_bufnr(function(params)
return u.cosmiconfig("eslint")(params.bufname)
return u.cosmiconfig("eslint", "eslintConfig")(params.bufname)
end),
},
factory = h.generator_factory,
Expand Down
2 changes: 1 addition & 1 deletion lua/null-ls/builtins/diagnostics/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ return h.make_builtin({
on_output = handle_eslint_output,
dynamic_command = cmd_resolver.from_node_modules(),
cwd = h.cache.by_bufnr(function(params)
return u.cosmiconfig("eslint")(params.bufname)
return u.cosmiconfig("eslint", "eslintConfig")(params.bufname)
end),
},
factory = h.generator_factory,
Expand Down
2 changes: 1 addition & 1 deletion lua/null-ls/builtins/formatting/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ return h.make_builtin({
dynamic_command = cmd_resolver.from_node_modules(),
check_exit_code = { 0, 1 },
cwd = h.cache.by_bufnr(function(params)
return u.cosmiconfig("eslint")(params.bufname)
return u.cosmiconfig("eslint", "eslintConfig")(params.bufname)
end),
},
})
20 changes: 17 additions & 3 deletions lua/null-ls/utils/cosmiconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ local u = require("null-ls.utils")
-- Create the default root_pattern for tools using cosmiconfig.
-- https://github.com/cosmiconfig/cosmiconfig#usage-for-end-users
---@param module_name string The module name.
return function(module_name)
---@param pkg_json_field_name? string The field name in package.json.
return function(module_name, pkg_json_field_name)
local patterns = {
"package.json",
".{NAME}rc",
".{NAME}rc.json",
".{NAME}rc.yaml",
Expand All @@ -22,5 +22,19 @@ return function(module_name)
patterns[i] = string.gsub(v, "{NAME}", module_name)
end

return u.root_pattern(unpack(patterns))
return function(...)
local pkg_json_dir = u.root_pattern("package.json")(...)
if pkg_json_dir then
local pkg_json_path = u.path.join(pkg_json_dir, "package.json")
if vim.fn.filereadable(pkg_json_path) then
local pkg_json = vim.json.decode(vim.fn.readblob(pkg_json_path))
local field = pkg_json_field_name or module_name
if vim.tbl_contains(vim.tbl_keys(pkg_json), field) then
return pkg_json_dir
end
end
end

return u.root_pattern(unpack(patterns))(...)
end
end
Loading