Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `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
Expand Down
4 changes: 3 additions & 1 deletion script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions test/type_inference/field_override.lua
Original file line number Diff line number Diff line change
@@ -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(<?target?>)
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(<?other?>)
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(<?x?>)
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(<?value?>)
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, <?b?>)
return true
end
]]
1 change: 1 addition & 0 deletions test/type_inference/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ end

require 'type_inference.common'
require 'type_inference.param_match'
require 'type_inference.field_override'
Loading