Skip to content

[feat] generics: capture types with backticks #3149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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` Allow capture object types with backticks in generics

## 3.14.0
`2025-4-7`
Expand Down
22 changes: 21 additions & 1 deletion script/vm/sign.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,29 @@ function mt:resolve(uri, args)
if object.literal then
-- 'number' -> `T`
for n in node:eachObject() do
local typeName = nil
local typeUri = nil
if n.type == 'string' then
typeName = n[1]
typeUri = guide.getUri(n)
elseif n.type == "global" and n.cate == "type" then
typeName = n:getName()
elseif (n.type == "function" or n.type == "doc.type.function")
and #n.returns > 0 then
---@cast n parser.object
local type = vm.declareGlobal('type', object.pattern and object.pattern:format(n[1]) or n[1], guide.getUri(n))
local fret = vm.getReturnOfFunction(n, 1)
if fret then
local compiled = vm.compileNode(fret)
local r1 = compiled and compiled[1]
if r1 and r1.cate == "type" then
typeName = r1:getName()
end
end
end
if typeName ~= nil then
---@cast n parser.object
local type = vm.declareGlobal('type',
object.pattern and object.pattern:format(typeName) or typeName, typeUri)
resolved[key] = vm.createNode(type, resolved[key])
end
end
Expand Down
56 changes: 55 additions & 1 deletion test/definition/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ print(v1.<?bar1?>)
TEST [[
---@class Foo
local Foo = {}
function Foo:bar1() end
function Foo:<!bar1!>() end

---@generic T
---@param arg1 `T`
Expand Down Expand Up @@ -402,6 +402,43 @@ local v1 = Generic("Foo")
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo.2
local nFoo2 = {}
function nFoo2:<!bar1!>() end

---@class Foo
local Foo = {}

---@generic T
---@param arg1 n.`T`.2
---@return T
function Generic(arg1) print(arg1) end

local v1 = Generic(Foo)
print(v1.<?bar1?>)
]]

TEST [[
---@class n.Foo.2
local nFoo2 = {}
function nFoo2:<!bar1!>() end

---@class Foo
local Foo = {}

---@return Foo
function returnsFoo() print("") end

---@generic T
---@param arg1 n.`T`.2
---@return T
function Generic(arg1) print(arg1) end

local v1 = Generic(returnsFoo())
print(v1.<?bar1?>)
]]

TEST [[
---@class n-Foo-2
local Foo = {}
Expand Down Expand Up @@ -430,6 +467,23 @@ local v1 = Generic({"Foo"})
print(v1.<?bar1?>)
]]

TEST [[
---@class n-Foo-2
local nFoo2 = {}
function nFoo2:<!bar1!>() end

---@class Foo
local Foo = {}

---@generic T
---@param arg1 n-`T`-2[]
---@return T
function Generic(arg1) print(arg1) end

local v1 = Generic({Foo})
print(v1.<?bar1?>)
]]

TEST [[
---@class A
local t
Expand Down
Loading