Merge pull request #21426 from williamboman/fix/tbl_get-nil-return-val

fix(lua): always return nil values in vim.tbl_get when no results
This commit is contained in:
Lewis Russell 2022-12-15 08:03:31 +00:00 committed by GitHub
commit 3091fa778a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 5 deletions

View File

@ -395,15 +395,14 @@ end
function vim.tbl_get(o, ...)
local keys = { ... }
if #keys == 0 then
return
return nil
end
for i, k in ipairs(keys) do
if type(o[k]) ~= 'table' and next(keys, i) then
return nil
end
o = o[k]
if o == nil then
return
return nil
elseif type(o) ~= 'table' and next(keys, i) then
return nil
end
end
return o

View File

@ -512,6 +512,8 @@ describe('lua stdlib', function()
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = function () end }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({})"))
eq(1, exec_lua("return select('#', vim.tbl_get({}))"))
eq(1, exec_lua("return select('#', vim.tbl_get({ nested = {} }, 'nested', 'missing_key'))"))
end)
it('vim.tbl_extend', function()