diff options
-rw-r--r-- | script/parser/guide.lua | 35 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 30 | ||||
-rw-r--r-- | test/definition/luadoc.lua | 56 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 16 |
4 files changed, 103 insertions, 34 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index cc21f5fa..ec78dbea 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -101,6 +101,7 @@ m.childMap = { ['doc.vararg'] = {'vararg', 'comment'}, ['doc.type.table'] = {'key', 'value', 'comment'}, ['doc.type.function'] = {'#args', '#returns', 'comment'}, + ['doc.type.typeliteral'] = {'node'}, ['doc.overload'] = {'overload', 'comment'}, ['doc.see'] = {'name', 'field'}, } @@ -1503,25 +1504,23 @@ function m.checkSameSimpleInSpecialBranch(status, obj, start, queue) end end -local function toValidGenericType(status, obj) - if obj.type ~= 'string' then - return obj - end - - if not status.interface.docType then - return obj - end - - local docs = status.interface.docType(obj[1]) - for i = 1, #docs do - local doc = docs[i] - if doc.type == 'doc.class.name' - or doc.type == 'doc.alias.name' then - return doc +local function appendValidGenericType(results, status, typeName, obj) + if typeName.parent.type == 'doc.type.typeliteral' then + if obj.type == 'string' and status.interface.docType then + local docs = status.interface.docType(obj[1]) + for i = 1, #docs do + local doc = docs[i] + if doc.type == 'doc.class.name' + or doc.type == 'doc.alias.name' then + results[#results+1] = doc + break + end + end end + else + -- 发现没有使用 `T`,则沿用既有逻辑直接返回实参 + results[#results+1] = obj end - - return obj end local function stepRefOfGeneric(status, typeUnit, args, mode) @@ -1548,7 +1547,7 @@ local function stepRefOfGeneric(status, typeUnit, args, mode) and source.parent.type == 'funcargs' then for index, arg in ipairs(source.parent) do if arg == source then - results[#results+1] = toValidGenericType(status, args[index]) + appendValidGenericType(results, status, typeName, args[index]) end end end diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index cd4e76a9..990b4606 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -56,6 +56,7 @@ Symbol <- ({} { / '...' / '+' / '#' + / '`' } {}) -> Symbol ]], { @@ -450,12 +451,41 @@ function parseType(parent) if not tp then break end + + -- 处理 `T` 的情况 + local typeLiteral = nil + if tp == 'symbol' and content == '`' then + nextToken() + if not checkToken('symbol', '`', 2) then + break + end + tp, content = peekToken() + if not tp then + break + end + -- TypeLiteral,指代类型的字面值。比如,对于类 Cat 来说,它的 TypeLiteral 是 "Cat" + typeLiteral = { + type = 'doc.type.typeliteral', + parent = result, + start = getStart(), + finish = nil, + node = nil, + } + end + if tp == 'name' then nextToken() local typeUnit = parseTypeUnit(result, content) if not typeUnit then break end + if typeLiteral then + nextToken() + typeLiteral.finish = getFinish() + typeLiteral.node = typeUnit + typeUnit.parent = typeLiteral + typeUnit = typeLiteral + end result.types[#result.types+1] = typeUnit if not result.start then result.start = typeUnit.start diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua index 4a23b2ac..1f3dae00 100644 --- a/test/definition/luadoc.lua +++ b/test/definition/luadoc.lua @@ -197,3 +197,59 @@ TEST [[ ---@type Cat local <?<!v!>?> ]] + +TEST [[ +---@class Foo +local Foo = {} +function Foo:<!bar1!>() end + +---@generic T +---@param arg1 T +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic(Foo) +print(v1.<?bar1?>) +]] + +TEST [[ +---@class Foo +local Foo = {} +function Foo:bar1() end + +---@generic T +---@param arg1 T +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic("Foo") +print(v1.<?bar1?>) +]] + +TEST [[ +---@class Foo +local Foo = {} +function Foo:bar1() end + +---@generic T +---@param arg1 `T` +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic(Foo) +print(v1.<?bar1?>) +]] + +TEST [[ +---@class Foo +local Foo = {} +function Foo:<!bar1!>() end + +---@generic T +---@param arg1 `T` +---@return T +function Generic(arg1) print(arg1) end + +local v1 = Generic("Foo") +print(v1.<?bar1?>) +]] diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index c593968e..4e485d2a 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -960,19 +960,3 @@ print(T1.f1) T2 = {} print(T2.<!f2!>) ]] - -TEST [[ ----@generic T ----@param arg1 T ----@return T -function Generic(arg1) return arg1 end - ----@class Foo ----@field bar1 integer -local Foo = {} - -local v1 = Generic("Foo") -print(v1.bar1) -local v2 = Generic(Foo) -print(v2.bar1) -]] |