Skip to content

Commit c084104

Browse files
committed
also detects missing index fields
1 parent 7582afd commit c084104

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

script/core/diagnostics/missing-fields.lua

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ return function (uri, callback)
1515
guide.eachSourceType(state.ast, 'table', function (src)
1616
await.delay()
1717

18+
vm.removeNode(src) -- the node is not updated correctly, reason still unknown
1819
local defs = vm.getDefs(src)
1920
local sortedDefs = {}
2021
for _, def in ipairs(defs) do
@@ -47,7 +48,7 @@ return function (uri, callback)
4748

4849
local myKeys = {}
4950
for _, field in ipairs(src) do
50-
local key = vm.getKeyName(field)
51+
local key = vm.getKeyName(field) or field.tindex
5152
if key then
5253
myKeys[key] = true
5354
end
@@ -57,8 +58,20 @@ return function (uri, callback)
5758
if not field.optional
5859
and not vm.compileNode(field):isNullable() then
5960
local key = vm.getKeyName(field)
61+
if not key then
62+
local fieldnode = vm.compileNode(field.field)[1]
63+
if fieldnode and fieldnode.type == 'doc.type.integer' then
64+
---@cast fieldnode parser.object
65+
key = vm.getKeyName(fieldnode)
66+
end
67+
end
68+
6069
if key and not myKeys[key] then
61-
missedKeys[#missedKeys+1] = ('`%s`'):format(key)
70+
if type(key) == "number" then
71+
missedKeys[#missedKeys+1] = ('`[%s]`'):format(key)
72+
else
73+
missedKeys[#missedKeys+1] = ('`%s`'):format(key)
74+
end
6275
end
6376
end
6477
end

test/diagnostics/missing-fields.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,22 @@ local x = <!{
334334
bb = 2,
335335
bd = 4,
336336
}!>
337+
]]
338+
339+
TEST[[
340+
---@class A
341+
---@field [1] string
342+
---@field x number
343+
344+
---@type A
345+
local t = {x = 1, ""}
346+
]]
347+
348+
TEST[[
349+
---@class A
350+
---@field [1] string
351+
---@field x number
352+
353+
---@type A
354+
local t = <!{x = 1}!>
337355
]]

0 commit comments

Comments
 (0)