aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/lua/vim/shared.lua10
-rw-r--r--test/functional/lua/vim_spec.lua1
2 files changed, 6 insertions, 5 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index f19533f474..04a40830f7 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -526,15 +526,15 @@ end
---@param ... any Optional keys (0 or more, variadic) via which to index the table
---@return any # Nested value indexed by key (if it exists), else nil
function vim.tbl_get(o, ...)
- local keys = { ... }
- if #keys == 0 then
+ local nargs = select('#', ...)
+ if nargs == 0 then
return nil
end
- for i, k in ipairs(keys) do
- o = o[k] --- @type any
+ for i = 1, nargs do
+ o = o[select(i, ...)] --- @type any
if o == nil then
return nil
- elseif type(o) ~= 'table' and next(keys, i) then
+ elseif type(o) ~= 'table' and i ~= nargs then
return nil
end
end
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 55e5158596..8e22644339 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -970,6 +970,7 @@ describe('lua stdlib', function()
)
eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')"))
eq(NIL, exec_lua('return vim.tbl_get({})'))
+ eq(NIL, exec_lua("return vim.tbl_get({}, nil, 'key')"))
eq(1, exec_lua("return select('#', vim.tbl_get({}))"))
eq(1, exec_lua("return select('#', vim.tbl_get({ nested = {} }, 'nested', 'missing_key'))"))
end)