diff --git a/changelog.md b/changelog.md index 3b605d2cf..5dc7bfd6a 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `NEW` Support type inference for `@field` and `@type` function declarations in method overrides [#3367](https://github.com/LuaLS/lua-language-server/issues/3367) * `CHG` Modified the `ResolveRequire` function to pass the source URI as a third argument. ## 3.17.1 diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 5267a037b..af9f81f2e 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1460,7 +1460,9 @@ local function compileFunctionParam(func, source) end vm.getClassFields(suri, extClass, key, function (field, _isMark) for n in vm.compileNode(field):eachObject() do - if n.type == 'function' and n.args[aindex] then + if (n.type == 'function' or n.type == 'doc.type.function') + and n.args[aindex] + then local argNode = vm.compileNode(n.args[aindex]) for an in argNode:eachObject() do if an.type ~= 'doc.generic.name' then diff --git a/test/type_inference/field_override.lua b/test/type_inference/field_override.lua new file mode 100644 index 000000000..71e5b1866 --- /dev/null +++ b/test/type_inference/field_override.lua @@ -0,0 +1,68 @@ + +-- Test @type field declaration with method override +TEST 'Buff' [[ +---@class Buff +local mt = {} +---@type (fun(self: Buff, target: Buff): boolean)? +mt.on_cover = nil + +---@class Buff.CommandAura : Buff +local tpl = {} +function tpl:on_cover() + return true +end +]] + +-- Test @field declaration with method override +TEST 'Animal' [[ +---@class Animal +---@field can_eat (fun(self: Animal, other: Animal): boolean)? +local base = {} + +---@class Dog : Animal +local dog = {} +function dog:can_eat() + return true +end +]] + +-- Test optional method with @type +TEST 'string' [[ +---@class Base +local base = {} +---@type (fun(self: Base, x: string): number)? +base.callback = nil + +---@class Child : Base +local child = {} +function child:callback() + return 1 +end +]] + +-- Test non-optional @field +TEST 'number' [[ +---@class Handler +---@field process fun(self: Handler, value: number): string +local handler = {} + +---@class CustomHandler : Handler +local custom = {} +function custom:process() + return tostring(value) +end +]] + +-- Test multiple parameters with @type +TEST 'string' [[ +---@class Processor +local proc = {} +---@type fun(self: Processor, a: number, b: string): boolean +proc.handle = nil + +---@class MyProcessor : Processor +local my = {} +function my:handle(a, ) + return true +end +]] diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 4eaad03d2..b45a1a4dd 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -46,3 +46,4 @@ end require 'type_inference.common' require 'type_inference.param_match' +require 'type_inference.field_override'