summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeOzay <colpaert.benoit@gmail.com>2024-07-24 18:01:44 +0200
committerNeOzay <colpaert.benoit@gmail.com>2024-07-24 18:01:44 +0200
commitc08410458924a9f3c604e662cfce80f0f86d5b78 (patch)
treeae6c48e1a0236036e914a6a28b0ad0d96621787b
parent7582afd72f6d111f81b9c48caf43bafa80a1887a (diff)
downloadlua-language-server-c08410458924a9f3c604e662cfce80f0f86d5b78.zip
also detects missing index fields
-rw-r--r--script/core/diagnostics/missing-fields.lua17
-rw-r--r--test/diagnostics/missing-fields.lua18
2 files changed, 33 insertions, 2 deletions
diff --git a/script/core/diagnostics/missing-fields.lua b/script/core/diagnostics/missing-fields.lua
index b901461a..18b561ee 100644
--- a/script/core/diagnostics/missing-fields.lua
+++ b/script/core/diagnostics/missing-fields.lua
@@ -15,6 +15,7 @@ return function (uri, callback)
guide.eachSourceType(state.ast, 'table', function (src)
await.delay()
+ vm.removeNode(src) -- the node is not updated correctly, reason still unknown
local defs = vm.getDefs(src)
local sortedDefs = {}
for _, def in ipairs(defs) do
@@ -47,7 +48,7 @@ return function (uri, callback)
local myKeys = {}
for _, field in ipairs(src) do
- local key = vm.getKeyName(field)
+ local key = vm.getKeyName(field) or field.tindex
if key then
myKeys[key] = true
end
@@ -57,8 +58,20 @@ return function (uri, callback)
if not field.optional
and not vm.compileNode(field):isNullable() then
local key = vm.getKeyName(field)
+ if not key then
+ local fieldnode = vm.compileNode(field.field)[1]
+ if fieldnode and fieldnode.type == 'doc.type.integer' then
+ ---@cast fieldnode parser.object
+ key = vm.getKeyName(fieldnode)
+ end
+ end
+
if key and not myKeys[key] then
- missedKeys[#missedKeys+1] = ('`%s`'):format(key)
+ if type(key) == "number" then
+ missedKeys[#missedKeys+1] = ('`[%s]`'):format(key)
+ else
+ missedKeys[#missedKeys+1] = ('`%s`'):format(key)
+ end
end
end
end
diff --git a/test/diagnostics/missing-fields.lua b/test/diagnostics/missing-fields.lua
index f1b1beae..8c1ffbbb 100644
--- a/test/diagnostics/missing-fields.lua
+++ b/test/diagnostics/missing-fields.lua
@@ -334,4 +334,22 @@ local x = <!{
bb = 2,
bd = 4,
}!>
+]]
+
+TEST[[
+---@class A
+---@field [1] string
+---@field x number
+
+---@type A
+local t = {x = 1, ""}
+]]
+
+TEST[[
+---@class A
+---@field [1] string
+---@field x number
+
+---@type A
+local t = <!{x = 1}!>
]] \ No newline at end of file