Skip to content

Commit

Permalink
fix(typescript): improve eslint and prettier loading (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: Uzair Aftab <uzaaft@outlook.com>
  • Loading branch information
mehalter and Uzaaft committed Jun 11, 2023
1 parent 569f4f8 commit 39835ba
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions lua/astrocommunity/pack/typescript/typescript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
{
Expand All @@ -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,
Expand Down

0 comments on commit 39835ba

Please sign in to comment.