Skip to content

Commit b94bef0

Browse files
authored
Merge pull request #3368 from ChouUn/fix/field-override-type-inference
feat: support type inference for @field and @type function declarations in method overrides
1 parent 39bb895 commit b94bef0

4 files changed

Lines changed: 73 additions & 1 deletion

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `NEW` Support type inference for `@field` and `@type` function declarations in method overrides [#3367](https://github.com/LuaLS/lua-language-server/issues/3367)
56
* `FIX` Deduplicate documentation bindings for parameters
67
* `FIX` Correct `math.type` meta return annotation to use `nil` instead of the string literal `'nil'`
78
* `FIX` Fix initial `nameStyle.config` not getting loaded in the appropriate workspace.

script/vm/compiler.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,9 @@ local function compileFunctionParam(func, source)
14601460
end
14611461
vm.getClassFields(suri, extClass, key, function (field, _isMark)
14621462
for n in vm.compileNode(field):eachObject() do
1463-
if n.type == 'function' and n.args[aindex] then
1463+
if (n.type == 'function' or n.type == 'doc.type.function')
1464+
and n.args[aindex]
1465+
then
14641466
local argNode = vm.compileNode(n.args[aindex])
14651467
for an in argNode:eachObject() do
14661468
if an.type ~= 'doc.generic.name' then
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
-- Test @type field declaration with method override
3+
TEST 'Buff' [[
4+
---@class Buff
5+
local mt = {}
6+
---@type (fun(self: Buff, target: Buff): boolean)?
7+
mt.on_cover = nil
8+
9+
---@class Buff.CommandAura : Buff
10+
local tpl = {}
11+
function tpl:on_cover(<?target?>)
12+
return true
13+
end
14+
]]
15+
16+
-- Test @field declaration with method override
17+
TEST 'Animal' [[
18+
---@class Animal
19+
---@field can_eat (fun(self: Animal, other: Animal): boolean)?
20+
local base = {}
21+
22+
---@class Dog : Animal
23+
local dog = {}
24+
function dog:can_eat(<?other?>)
25+
return true
26+
end
27+
]]
28+
29+
-- Test optional method with @type
30+
TEST 'string' [[
31+
---@class Base
32+
local base = {}
33+
---@type (fun(self: Base, x: string): number)?
34+
base.callback = nil
35+
36+
---@class Child : Base
37+
local child = {}
38+
function child:callback(<?x?>)
39+
return 1
40+
end
41+
]]
42+
43+
-- Test non-optional @field
44+
TEST 'number' [[
45+
---@class Handler
46+
---@field process fun(self: Handler, value: number): string
47+
local handler = {}
48+
49+
---@class CustomHandler : Handler
50+
local custom = {}
51+
function custom:process(<?value?>)
52+
return tostring(value)
53+
end
54+
]]
55+
56+
-- Test multiple parameters with @type
57+
TEST 'string' [[
58+
---@class Processor
59+
local proc = {}
60+
---@type fun(self: Processor, a: number, b: string): boolean
61+
proc.handle = nil
62+
63+
---@class MyProcessor : Processor
64+
local my = {}
65+
function my:handle(a, <?b?>)
66+
return true
67+
end
68+
]]

test/type_inference/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ end
4646

4747
require 'type_inference.common'
4848
require 'type_inference.param_match'
49+
require 'type_inference.field_override'

0 commit comments

Comments
 (0)