Skip to content

Commit 2af1e1e

Browse files
author
Jose Alvarez
committed
fix(helpers): correctly handle nil args return
1 parent f5def20 commit 2af1e1e

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

lua/null-ls/helpers.lua

+15-5
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,21 @@ M.make_builtin = function(opts)
380380
local original_extra_args = user_opts.extra_args
381381

382382
builtin_copy._opts.args = function(params)
383-
local original_args_copy = type(original_args) == "function" and original_args(params)
384-
or vim.deepcopy(original_args)
385-
or {}
386-
local extra_args_copy = type(original_extra_args) == "function" and original_extra_args(params)
387-
or vim.deepcopy(original_extra_args)
383+
local original_args_copy
384+
if type(original_args) == "function" then
385+
original_args_copy = original_args(params)
386+
else
387+
original_args_copy = vim.deepcopy(original_args)
388+
end
389+
original_args_copy = original_args_copy or {}
390+
391+
local extra_args_copy
392+
if type(original_extra_args) == "function" then
393+
extra_args_copy = original_extra_args(params)
394+
else
395+
extra_args_copy = vim.deepcopy(original_extra_args)
396+
end
397+
extra_args_copy = extra_args_copy or {}
388398

389399
-- make sure "-" stays last
390400
if original_args_copy[#original_args_copy] == "-" then

test/spec/helpers_spec.lua

+30
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,18 @@ describe("helpers", function()
909909
assert.are.same(copy._opts.args(), { "first", "second", "user_first", "user_second" })
910910
end)
911911

912+
it("should keep original args if extra_args returns nil", function()
913+
local copy = builtin.with({
914+
extra_args = function()
915+
return nil
916+
end,
917+
})
918+
919+
assert.equals(type(copy._opts.args), "function")
920+
assert.are.same(copy._opts.args(), { "first", "second" })
921+
assert.are.same(copy._opts.args(), { "first", "second" })
922+
end)
923+
912924
it("should set args to extra_args if args is nil", function()
913925
local test_opts = {
914926
method = "mockMethod",
@@ -925,6 +937,24 @@ describe("helpers", function()
925937
assert.are.same(copy._opts.args(), { "user_first", "user_second" })
926938
end)
927939

940+
it("should set args to extra_args if args returns nil", function()
941+
local test_opts = {
942+
method = "mockMethod",
943+
name = "mock-builtin",
944+
filetypes = { "lua" },
945+
generator_opts = {
946+
args = function()
947+
return nil
948+
end,
949+
},
950+
}
951+
builtin = helpers.make_builtin(test_opts)
952+
local copy = builtin.with({ extra_args = { "user_first", "user_second" } })
953+
954+
assert.equals(type(copy._opts.args), "function")
955+
assert.are.same(copy._opts.args(), { "user_first", "user_second" })
956+
end)
957+
928958
it("should extend args with extra_args, but keep '-' arg last", function()
929959
-- local test_opts = vim.deep_copy(opts) stack overflows
930960
local test_opts = {

0 commit comments

Comments
 (0)