diff --git a/changelog.md b/changelog.md index d929b7919..9b9efc74a 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ * `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available * `FIX` support hex color codes with `#` in `textDocument/documentColor` +* `FIX` Prevent class methods from triggering missing-fields diagnostics ## 3.14.0 `2025-4-7` diff --git a/script/core/diagnostics/missing-fields.lua b/script/core/diagnostics/missing-fields.lua index ed9bf1a6a..07037ba8a 100644 --- a/script/core/diagnostics/missing-fields.lua +++ b/script/core/diagnostics/missing-fields.lua @@ -31,7 +31,7 @@ return function (uri, callback) local class = vm.getGlobal('type', className) ---@cast class -nil for _, set in ipairs(class:getSets(uri)) do - if set.type == 'doc.class' + if set.type == 'doc.class' and vm.docHasAttr(set, 'partial') then sortedDefs[className].isPartial = true @@ -70,6 +70,7 @@ return function (uri, callback) for _, field in ipairs(fields) do if not field.optional + and field.type == "doc.field" and not vm.compileNode(field):isNullable() then local key = vm.getKeyName(field) if not key then diff --git a/test/diagnostics/missing-fields.lua b/test/diagnostics/missing-fields.lua index 66abfe162..d0876ac74 100644 --- a/test/diagnostics/missing-fields.lua +++ b/test/diagnostics/missing-fields.lua @@ -51,6 +51,97 @@ local t = { } ]] +TEST [[ +---@diagnostic disable: unused-local +---@class A +---@field x number +---@field y? number +---@field z number +local A = {} + +function A:fun() +end + +---@type A +local t = { + x = 1, + y = 2, + z = 3, +} +]] + +TEST [[ +---@diagnostic disable: unused-local + +---@class Parent +---@field a number +local Parent = {} + +function Parent:fun2() +end + +---@class A : Parent +---@field x number +---@field y? number +---@field z number +local A = {} + +function A:fun() +end + +---@type A +local t = +]] + +TEST [[ +---@diagnostic disable: unused-local + +---@class Parent +---@field a number +local Parent = {} + +function Parent:fun2() +end + +---@class A : Parent +---@field x number +---@field y? number +---@field z number +local A = {} + +function A:fun() +end + +---@type A +local t = { + x = 1, + y = 2, + z = 3, + a = 1, +} +]] + +TEST [[ +---@diagnostic disable: unused-local +---@class A +---@field x number +---@field y? number +---@field z number +local A = {} + +function A:fun() +end + +---@type A +local t = +]] + TEST [[ ---@diagnostic disable: unused-local ---@class A @@ -336,7 +427,7 @@ local x = ]] -TEST[[ +TEST [[ ---@class A ---@field [1] string ---@field x number @@ -345,7 +436,7 @@ TEST[[ local t = {x = 1, ""} ]] -TEST[[ +TEST [[ ---@class A ---@field [1] string ---@field x number @@ -356,7 +447,7 @@ local t = -- Inheritance -TEST[[ +TEST [[ ---@class A ---@field x number @@ -366,7 +457,7 @@ TEST[[ local t = ]] -TEST[[ +TEST [[ ---@class A ---@field x number ---@field y number @@ -377,7 +468,7 @@ TEST[[ local t = ]] -TEST[[ +TEST [[ ---@class A ---@field x number @@ -390,7 +481,7 @@ local t = -- Inheritance + optional -TEST[[ +TEST [[ ---@class A ---@field x? number @@ -400,7 +491,7 @@ TEST[[ local t = {} ]] -TEST[[ +TEST [[ ---@class A ---@field x? number ---@field y number @@ -411,7 +502,7 @@ TEST[[ local t = {y = 1} ]] -TEST[[ +TEST [[ ---@class A ---@field x? number @@ -424,7 +515,7 @@ local t = {y = 1} -- Inheritance + function call -TEST[[ +TEST [[ ---@class A ---@field x number @@ -436,7 +527,7 @@ local function f(b) end f ]] -TEST[[ +TEST [[ ---@class A ---@field x number ---@field y number @@ -449,7 +540,7 @@ local function f(b) end f ]] -TEST[[ +TEST [[ ---@class A ---@field x number @@ -464,7 +555,7 @@ f -- partial class -TEST[[ +TEST [[ ---@class A ---@field x number @@ -474,7 +565,7 @@ TEST[[ local t = {} ]] -TEST[[ +TEST [[ ---@class A ---@field x number @@ -485,7 +576,7 @@ TEST[[ local t = ]] -TEST[[ +TEST [[ ---@class A ---@field x number