Skip to content

Commit 70f764b

Browse files
committed
fix(util): Util.merge now skips nil args
1 parent 3769461 commit 70f764b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lua/lazy/core/util.lua

+4-7
Original file line numberDiff line numberDiff line change
@@ -396,22 +396,19 @@ end
396396
---@param ... T
397397
---@return T
398398
function M.merge(...)
399-
local values = { ... }
400-
local ret = values[1]
401-
399+
local ret = select(1, ...)
402400
if ret == vim.NIL then
403401
ret = nil
404402
end
405-
406-
for i = 2, #values, 1 do
407-
local value = values[i]
403+
for i = 2, select("#", ...) do
404+
local value = select(i, ...)
408405
if can_merge(ret) and can_merge(value) then
409406
for k, v in pairs(value) do
410407
ret[k] = M.merge(ret[k], v)
411408
end
412409
elseif value == vim.NIL then
413410
ret = nil
414-
else
411+
elseif value ~= nil then
415412
ret = value
416413
end
417414
end

tests/core/util_spec.lua

+21-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ describe("util", function()
101101
input = { { a = 1 }, { b = 2 } },
102102
output = { a = 1, b = 2 },
103103
},
104+
{
105+
input = { nil, { a = 1 }, { b = 2 } },
106+
output = { a = 1, b = 2 },
107+
},
108+
{
109+
input = { { a = 1 }, { b = 2 }, nil },
110+
output = { a = 1, b = 2 },
111+
},
112+
{
113+
input = { { a = 1 }, nil, { b = 2 } },
114+
output = { a = 1, b = 2 },
115+
},
116+
{
117+
input = { nil, { a = 1 }, nil, { b = 2 }, nil },
118+
output = { a = 1, b = 2 },
119+
},
104120
{
105121
input = { { a = 1 }, { a = 2 } },
106122
output = { a = 2 },
@@ -120,7 +136,11 @@ describe("util", function()
120136
}
121137

122138
for _, test in ipairs(tests) do
123-
assert.same(test.output, Util.merge(unpack(test.input)))
139+
local n = 0
140+
for i in pairs(test.input) do
141+
n = math.max(n, i)
142+
end
143+
assert.same(test.output, Util.merge(unpack(test.input, 1, n)))
124144
end
125145
end)
126146
end)

0 commit comments

Comments
 (0)