Skip to content

Commit

Permalink
refactor: respect package.json field name in cosmiconfig (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored Jan 24, 2024
1 parent 0258d43 commit 9cfdab1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
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

0 comments on commit 9cfdab1

Please sign in to comment.