Skip to content

Commit 119b8ef

Browse files
committed
generics: capture types with backticks
1 parent 485835e commit 119b8ef

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

script/vm/sign.lua

+21-1
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,29 @@ function mt:resolve(uri, args)
5151
if object.literal then
5252
-- 'number' -> `T`
5353
for n in node:eachObject() do
54+
local typeName = nil
55+
local typeUri = nil
5456
if n.type == 'string' then
57+
typeName = n[1]
58+
typeUri = guide.getUri(n)
59+
elseif n.type == "global" and n.cate == "type" then
60+
typeName = n:getName()
61+
elseif (n.type == "function" or n.type == "doc.type.function")
62+
and #n.returns > 0 then
5563
---@cast n parser.object
56-
local type = vm.declareGlobal('type', object.pattern and object.pattern:format(n[1]) or n[1], guide.getUri(n))
64+
local fret = vm.getReturnOfFunction(n, 1)
65+
if fret then
66+
local compiled = vm.compileNode(fret)
67+
local r1 = compiled and compiled[1]
68+
if r1 and r1.cate == "type" then
69+
typeName = r1:getName()
70+
end
71+
end
72+
end
73+
if typeName ~= nil then
74+
---@cast n parser.object
75+
local type = vm.declareGlobal('type',
76+
object.pattern and object.pattern:format(typeName) or typeName, typeUri)
5777
resolved[key] = vm.createNode(type, resolved[key])
5878
end
5979
end

test/definition/luadoc.lua

+55-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ print(v1.<?bar1?>)
293293
TEST [[
294294
---@class Foo
295295
local Foo = {}
296-
function Foo:bar1() end
296+
function Foo:<!bar1!>() end
297297
298298
---@generic T
299299
---@param arg1 `T`
@@ -402,6 +402,43 @@ local v1 = Generic("Foo")
402402
print(v1.<?bar1?>)
403403
]]
404404

405+
TEST [[
406+
---@class n.Foo.2
407+
local nFoo2 = {}
408+
function nFoo2:<!bar1!>() end
409+
410+
---@class Foo
411+
local Foo = {}
412+
413+
---@generic T
414+
---@param arg1 n.`T`.2
415+
---@return T
416+
function Generic(arg1) print(arg1) end
417+
418+
local v1 = Generic(Foo)
419+
print(v1.<?bar1?>)
420+
]]
421+
422+
TEST [[
423+
---@class n.Foo.2
424+
local nFoo2 = {}
425+
function nFoo2:<!bar1!>() end
426+
427+
---@class Foo
428+
local Foo = {}
429+
430+
---@return Foo
431+
function returnsFoo() print("") end
432+
433+
---@generic T
434+
---@param arg1 n.`T`.2
435+
---@return T
436+
function Generic(arg1) print(arg1) end
437+
438+
local v1 = Generic(returnsFoo())
439+
print(v1.<?bar1?>)
440+
]]
441+
405442
TEST [[
406443
---@class n-Foo-2
407444
local Foo = {}
@@ -430,6 +467,23 @@ local v1 = Generic({"Foo"})
430467
print(v1.<?bar1?>)
431468
]]
432469

470+
TEST [[
471+
---@class n-Foo-2
472+
local nFoo2 = {}
473+
function nFoo2:<!bar1!>() end
474+
475+
---@class Foo
476+
local Foo = {}
477+
478+
---@generic T
479+
---@param arg1 n-`T`-2[]
480+
---@return T
481+
function Generic(arg1) print(arg1) end
482+
483+
local v1 = Generic({Foo})
484+
print(v1.<?bar1?>)
485+
]]
486+
433487
TEST [[
434488
---@class A
435489
local t

0 commit comments

Comments
 (0)