Conditionally load prettier or eslint #599
Replies: 5 comments 4 replies
-
Not sure if this is exactly what you're looking for, but if the goal is to avoid registering null_ls.builtins.formatting.prettier.with({
filetype = { "css", "scss", "less", "html", "json", "yaml", "markdown", "graphql" },
condition = function()
-- check if prettier is already registered
return #require("null-ls.sources").get({ name = "prettier" }) == 0
end,
}) |
Beta Was this translation helpful? Give feedback.
-
Alright, so I finally got to the simplest form, which also doesn't require mangling filetyes 😌 -- Helper to conditionally register eslint handlers only if eslint is
-- configured. If eslint is not configured for a project, it just fails.
local function has_eslint_configured(utils)
return utils.root_has_file(".eslintrc.js")
end
null_ls.setup({
sources = {
null_ls.builtins.code_actions.eslint_d.with({ condition = has_eslint_configured }),
null_ls.builtins.diagnostics.eslint_d.with({ condition = has_eslint_configured }),
null_ls.builtins.formatting.eslint_d.with({ condition = has_eslint_configured }),
null_ls.builtins.formatting.prettier.with({
-- Only register prettier if eslint_d is not running as a formatter. This
-- can happen if it's not configured for this project, or if it can't
-- handle the current filetype.
condition = function()
return #null_ls.get_source({ name = "eslint_d", method = null_ls.methods.FORMATTING }) == 0
end,
}), Thanks again for all the help! |
Beta Was this translation helpful? Give feedback.
-
I've tried using the different configurations on this discussion, but I can't seem to handle the use case of having eslint_d handle my "javascript", "javascriptreact", "typescript", "typescriptreact" and "vue" files, but keep using prettierd for my "json" files - which eslint doesn't support according to the builtins. Any idea how I can extend this configuration to handle that? |
Beta Was this translation helpful? Give feedback.
-
The above is most of what I needed for that. My full config is here, hope it helps. |
Beta Was this translation helpful? Give feedback.
-
thanks for the summarized config, I applied it, but seems it does not auto format my file on saving. |
Beta Was this translation helpful? Give feedback.
-
I've this setup to enable
eslint
for formatting for javascript projects where its set up, falling back to prettier otherwise.However, the first source on this sample won't enable prettier for filetypes that eslint cannot handle (in projects where eslint is set up).
E.g.: for an
html
file on a project with eslint enabled, the first source here won't enableprettier
. So I have the second source, which unconditionally enables prettier for all the filetypes that eslint can't handle.This works, but it results in prettier being registered twice in, e.g.
vue
files, orhtml
files. Is there a cleaner approach to this whole setup?Beta Was this translation helpful? Give feedback.
All reactions