mirror of
https://github.com/neovim/neovim.git
synced 2025-12-16 03:15:39 +00:00
fix(lua): vim.tbl_get({}, nil, 1) should return nil #32218
Problem:
`vim.tbl_get(tbl, nil, 1)` returns `tbl` itself. In this case, `keys` is not
empty, but `ipairs` skips the iteration:
local keys = { nil, 1 }
assert(#keys == 2)
for i, k in ipairs(keys) do
assert(false, 'unreachable')
end
Solution:
Use `select("#", ...)` and `select(i, ...)` to ensure consistency for count and
iteration.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user