Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 19 additions & 5 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1353,10 +1353,16 @@ local function compileFunctionParam(func, source)
]]
local found = false
for n in funcNode:eachObject() do
if (n.type == 'doc.type.function' or n.type == 'function')
and n.args[aindex] and n.args[aindex] ~= source
-- Extract actual function type node
local funcType = n
if n.type == 'doc.field' and n.extends then
funcType = n.extends
end
Comment thread
ChouUn marked this conversation as resolved.
Outdated

if (funcType.type == 'doc.type.function' or funcType.type == 'function')
and funcType.args and funcType.args[aindex] and funcType.args[aindex] ~= source
then
Comment thread
ChouUn marked this conversation as resolved.
Outdated
local argNode = vm.compileNode(n.args[aindex])
local argNode = vm.compileNode(funcType.args[aindex])
for an in argNode:eachObject() do
if an.type ~= 'doc.generic.name' then
vm.setNode(source, an)
Expand Down Expand Up @@ -1460,8 +1466,16 @@ 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
local argNode = vm.compileNode(n.args[aindex])
-- Extract actual function type node
local funcType = n
if n.type == 'doc.field' and n.extends then
funcType = n.extends
end

if (funcType.type == 'function' or funcType.type == 'doc.type.function')
and funcType.args and funcType.args[aindex]
then
Comment thread
ChouUn marked this conversation as resolved.
Outdated
local argNode = vm.compileNode(funcType.args[aindex])
for an in argNode:eachObject() do
if an.type ~= 'doc.generic.name' then
vm.setNode(source, an)
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