From 09f8b6f4c4862aa164b0810720b523efc63bac91 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Sun, 18 May 2025 20:01:26 -0400 Subject: [PATCH 1/4] feat(missing-fields): ignore methods when determining missing fields --- script/core/diagnostics/missing-fields.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/script/core/diagnostics/missing-fields.lua b/script/core/diagnostics/missing-fields.lua index ed9bf1a6a..9695cb490 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,7 +70,8 @@ return function (uri, callback) for _, field in ipairs(fields) do if not field.optional - and not vm.compileNode(field):isNullable() then + and not vm.compileNode(field):isNullable() + and field.type == "doc.field" then local key = vm.getKeyName(field) if not key then local fieldnode = vm.compileNode(field.field)[1] From c73148b30c000c151f7c4e938db3178aca258e84 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Sun, 18 May 2025 20:05:06 -0400 Subject: [PATCH 2/4] test(missing-fields): add tests for missing fields but not methods --- test/diagnostics/missing-fields.lua | 119 ++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 14 deletions(-) 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 From 625cc6498d19bbd27a6edc9820a18f1452c0f1b6 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Sun, 18 May 2025 20:10:36 -0400 Subject: [PATCH 3/4] doc: update changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) 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` From 789605b69770eb6463820d4322ed97c1565936b3 Mon Sep 17 00:00:00 2001 From: Christopher Wood Date: Sun, 18 May 2025 22:41:13 -0400 Subject: [PATCH 4/4] perf(missing-fields): switch condition order --- script/core/diagnostics/missing-fields.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/core/diagnostics/missing-fields.lua b/script/core/diagnostics/missing-fields.lua index 9695cb490..07037ba8a 100644 --- a/script/core/diagnostics/missing-fields.lua +++ b/script/core/diagnostics/missing-fields.lua @@ -70,8 +70,8 @@ return function (uri, callback) for _, field in ipairs(fields) do if not field.optional - and not vm.compileNode(field):isNullable() - and field.type == "doc.field" then + and field.type == "doc.field" + and not vm.compileNode(field):isNullable() then local key = vm.getKeyName(field) if not key then local fieldnode = vm.compileNode(field.field)[1]