fix(lua): give nicer error message when trying to index vim.NIL

The added test shows the context, one expected a JSON field
to be a Object but it was a null value

pros: shows `vim.NIL` instead of `a userdata`
cons: the context `field 'foo'` is lost. I think this is generated
with internal magic which is hard to replicate.

(cherry picked from commit 71b28da0f4)
This commit is contained in:
bfredl
2026-06-29 14:16:38 +02:00
committed by github-actions[bot]
parent 6da50f145a
commit a4985ac902
2 changed files with 16 additions and 0 deletions

View File

@@ -178,6 +178,13 @@ describe('vim.json.decode()', function()
pcall_err(exec_lua, [[return vim.json.decode('{"a":1/*x*/0}', { skip_comments = true })]])
)
end)
it('gives nice error message when attempting to index into null value', function()
exec_lua [[ parsed = vim.json.decode('{"foo": null}') ]]
eq('attempt to index vim.NIL', pcall_err(exec_lua, [[ return parsed.foo.sub_field ]]))
eq('attempt to index vim.NIL', pcall_err(exec_lua, [[ parsed.foo.new_field = 3 ]]))
end)
end)
describe('vim.json.encode()', function()