Skip to content

Commit

Permalink
chore: use utils.cosmiconfig for eslint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
mochaaP committed Jan 11, 2024
1 parent a120877 commit 2d765eb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 78 deletions.
13 changes: 1 addition & 12 deletions lua/null-ls/builtins/code_actions/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,7 @@ return h.make_builtin({
end,
dynamic_command = cmd_resolver.from_node_modules(),
cwd = h.cache.by_bufnr(function(params)
return u.root_pattern(
-- https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new
"eslint.config.js",
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats
".eslintrc",
".eslintrc.js",
".eslintrc.cjs",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
"package.json"
)(params.bufname)
return u.cosmiconfig("eslint")(params.bufname)
end),
},
factory = h.generator_factory,
Expand Down
13 changes: 1 addition & 12 deletions lua/null-ls/builtins/diagnostics/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +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.root_pattern(
-- https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new
"eslint.config.js",
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats
".eslintrc",
".eslintrc.js",
".eslintrc.cjs",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
"package.json"
)(params.bufname)
return u.cosmiconfig("eslint")(params.bufname)
end),
},
factory = h.generator_factory,
Expand Down
13 changes: 1 addition & 12 deletions lua/null-ls/builtins/formatting/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +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.root_pattern(
-- https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new
"eslint.config.js",
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats
".eslintrc",
".eslintrc.js",
".eslintrc.cjs",
".eslintrc.yaml",
".eslintrc.yml",
".eslintrc.json",
"package.json"
)(params.bufname)
return u.cosmiconfig("eslint")(params.bufname)
end),
},
})
15 changes: 1 addition & 14 deletions lua/null-ls/builtins/formatting/prettier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,7 @@ return h.make_builtin({
to_stdin = true,
dynamic_command = cmd_resolver.from_node_modules(),
cwd = h.cache.by_bufnr(function(params)
return u.root_pattern(
-- https://prettier.io/docs/en/configuration.html
".prettierrc",
".prettierrc.json",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.js",
".prettierrc.cjs",
".prettierrc.toml",
"prettier.config.js",
"prettier.config.cjs",
"package.json"
)(params.bufname)
return u.cosmiconfig("prettier")(params.bufname)
end),
},
factory = h.formatter_factory,
Expand Down
15 changes: 1 addition & 14 deletions lua/null-ls/builtins/formatting/prettier_d_slim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,7 @@ return h.make_builtin({
to_stdin = true,
dynamic_command = cmd_resolver.from_node_modules(),
cwd = h.cache.by_bufnr(function(params)
return u.root_pattern(
-- https://prettier.io/docs/en/configuration.html
".prettierrc",
".prettierrc.json",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.js",
".prettierrc.cjs",
".prettierrc.toml",
"prettier.config.js",
"prettier.config.cjs",
"package.json"
)(params.bufname)
return u.cosmiconfig("prettier")(params.bufname)
end),
},
factory = h.formatter_factory,
Expand Down
15 changes: 1 addition & 14 deletions lua/null-ls/builtins/formatting/prettierd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,7 @@ return h.make_builtin({
dynamic_command = cmd_resolver.from_node_modules(),
to_stdin = true,
cwd = h.cache.by_bufnr(function(params)
return u.root_pattern(
-- https://prettier.io/docs/en/configuration.html
".prettierrc",
".prettierrc.json",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.js",
".prettierrc.cjs",
".prettierrc.toml",
"prettier.config.js",
"prettier.config.cjs",
"package.json"
)(params.bufname)
return u.cosmiconfig("prettier")(params.bufname)
end),
},
factory = h.formatter_factory,
Expand Down
26 changes: 26 additions & 0 deletions lua/null-ls/utils/cosmiconfig.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)
local patterns = {
"package.json",
".{NAME}rc",
".{NAME}rc.json",
".{NAME}rc.yaml",
".{NAME}rc.yml",
".{NAME}rc.js",
".{NAME}rc.ts",
".{NAME}rc.cjs",
"{NAME}.config.js",
"{NAME}.config.ts",
"{NAME}.config.mjs",
"{NAME}.config.cjs",
}
for i, v in ipairs(patterns) do
patterns[i] = string.gsub(v, "{NAME}", module_name)
end

return u.root_pattern(table.unpack(patterns))
end
4 changes: 4 additions & 0 deletions lua/null-ls/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,8 @@ M.make_params = function(...)
return require("null-ls.utils.make_params")(...)
end

M.cosmiconfig = function(...)
return require("null-ls.utils.cosmiconfig")(...)
end

return M

0 comments on commit 2d765eb

Please sign in to comment.