From 39835baf8410b869a527fcf27bee33b38ceec977 Mon Sep 17 00:00:00 2001 From: Micah Halter Date: Sun, 11 Jun 2023 16:41:58 -0400 Subject: [PATCH] fix(typescript): improve eslint and prettier loading (#231) Co-authored-by: Uzair Aftab --- .../pack/typescript/typescript.lua | 61 ++++++++++++++----- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/lua/astrocommunity/pack/typescript/typescript.lua b/lua/astrocommunity/pack/typescript/typescript.lua index 2c13825cb..a000db78a 100644 --- a/lua/astrocommunity/pack/typescript/typescript.lua +++ b/lua/astrocommunity/pack/typescript/typescript.lua @@ -15,6 +15,27 @@ local function on_file_remove(args) end end +local function check_json_key_exists(filename, key) + -- Open the file in read mode + local file = io.open(filename, "r") + if not file then + return false -- File doesn't exist or cannot be opened + end + + -- Read the contents of the file + local content = file:read "*all" + file:close() + + -- Parse the JSON content + local json = vim.fn.json_decode(content) + if type(json) ~= "table" then + return false -- Invalid JSON format + end + + -- Check if the key exists in the JSON object + return json[key] ~= nil +end + return { { import = "astrocommunity.pack.json" }, { @@ -31,31 +52,43 @@ return { }, { "jay-babu/mason-null-ls.nvim", + opts = function(_, opts) opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "prettierd", "eslint_d" }) - if not opts.handlers then opts.handlers = {} end + local has_prettier = function(util) + return check_json_key_exists(vim.fn.getcwd() .. "/package.json", "prettier") + or util.root_has_file ".prettierrc" + or util.root_has_file ".prettierrc.json" + or util.root_has_file ".prettierrc.yml" + or util.root_has_file ".prettierrc.yaml" + or util.root_has_file ".prettierrc.json5" + or util.root_has_file ".prettierrc.js" + or util.root_has_file ".prettierrc.cjs" + or util.root_has_file "prettierrc.config.js" + or util.root_has_file "prettierrc.config.cjs" + or util.root_has_file ".prettierrc.toml" + end + + local has_eslint = function(util) + return util.root_has_file ".eslintrc.js" + or util.root_has_file ".eslintrc.cjs" + or util.root_has_file ".eslintrc.yaml" + or util.root_has_file ".eslintrc.yml" + or util.root_has_file ".eslintrc.json" + or check_json_key_exists(vim.fn.getcwd() .. "/package.json", "eslintConfig") + end + opts.handlers.prettierd = function() local null_ls = require "null-ls" - null_ls.register(null_ls.builtins.formatting.prettierd.with { - condition = function(util) - return util.root_has_file "package.json" - or util.root_has_file ".prettierrc" - or util.root_has_file ".prettierrc.json" - or util.root_has_file ".prettierrc.js" - end, - }) + null_ls.register(null_ls.builtins.formatting.prettierd.with { condition = has_prettier }) end opts.handlers.eslint_d = function() local null_ls = require "null-ls" null_ls.register(null_ls.builtins.diagnostics.eslint_d.with { - condition = function(util) - return util.root_has_file "package.json" - or util.root_has_file ".eslintrc.json" - or util.root_has_file ".eslintrc.js" - end, + condition = function(util) return (has_eslint(util) or not has_prettier(util)) end, }) end end,