Skip to content

Commit

Permalink
Merge pull request #2505 from lizho/master
Browse files Browse the repository at this point in the history
support tuple type
  • Loading branch information
sumneko authored Feb 19, 2024
2 parents 7848d03 + 7503881 commit 8fc5a88
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
74 changes: 74 additions & 0 deletions script/parser/luadoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,78 @@ local function parseTable(parent)
return typeUnit
end

local function parseTuple(parent)
if not checkToken('symbol', '[', 1) then
return nil
end
nextToken()
local typeUnit = {
type = 'doc.type.table',
start = getStart(),
parent = parent,
fields = {},
isTuple = true,
}

local index = 1
while true do
if checkToken('symbol', ']', 1) then
nextToken()
break
end
local field = {
type = 'doc.type.field',
parent = typeUnit,
}

do
local needCloseParen
if checkToken('symbol', '(', 1) then
nextToken()
needCloseParen = true
end
field.name = {
type = 'doc.type',
start = getFinish(),
firstFinish = getFinish(),
finish = getFinish(),
parent = field,
}
field.name.types = {
[1] = {
type = 'doc.type.integer',
start = getFinish(),
finish = getFinish(),
parent = field.name,
[1] = index,
}
}
index = index + 1
field.extends = parseType(field)
if not field.extends then
break
end
field.optional = field.extends.optional
field.start = field.extends.start
field.finish = field.extends.finish
if needCloseParen then
nextSymbolOrError ')'
end
end

typeUnit.fields[#typeUnit.fields+1] = field
if checkToken('symbol', ',', 1)
or checkToken('symbol', ';', 1) then
nextToken()
else
nextSymbolOrError(']')
break
end
end
typeUnit.finish = getFinish()
return typeUnit
end

local function parseSigns(parent)
if not checkToken('symbol', '<', 1) then
return nil
Expand Down Expand Up @@ -682,6 +754,7 @@ end
function parseTypeUnit(parent)
local result = parseFunction(parent)
or parseTable(parent)
or parseTuple(parent)
or parseString(parent)
or parseCode(parent)
or parseInteger(parent)
Expand Down Expand Up @@ -864,6 +937,7 @@ local docSwitch = util.switch()
while true do
local extend = parseName('doc.extends.name', result)
or parseTable(result)
or parseTuple(result)
if not extend then
pushWarning {
type = 'LUADOC_MISS_CLASS_EXTENDS_NAME',
Expand Down
20 changes: 11 additions & 9 deletions script/vm/infer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,24 @@ local viewNodeSwitch;viewNodeSwitch = util.switch()
end
infer._hasClass = true
local buf = {}
buf[#buf+1] = '{ '
buf[#buf+1] = source.isTuple and '[' or '{ '
for i, field in ipairs(source.fields) do
if i > 1 then
buf[#buf+1] = ', '
end
local key = field.name
if key.type == 'doc.type' then
buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
elseif type(key[1]) == 'string' then
buf[#buf+1] = key[1] .. ': '
else
buf[#buf+1] = ('[%q]: '):format(key[1])
if not source.isTuple then
local key = field.name
if key.type == 'doc.type' then
buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
elseif type(key[1]) == 'string' then
buf[#buf+1] = key[1] .. ': '
else
buf[#buf+1] = ('[%q]: '):format(key[1])
end
end
buf[#buf+1] = vm.getInfer(field.extends):view(uri)
end
buf[#buf+1] = ' }'
buf[#buf+1] = source.isTuple and ']' or ' }'
return table.concat(buf)
end)
: case 'doc.type.string'
Expand Down
7 changes: 7 additions & 0 deletions test/diagnostics/assign-type-mismatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ local t = {}
t['x'] = nil
]]

TEST [[
---@type [boolean]
local t = { <![1]!> = nil }
t = nil
]]

TEST [[
local t = { true }
Expand Down
11 changes: 11 additions & 0 deletions test/hover/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,17 @@ local <?x?>
local x: table<ClassA, ClassB>
]]

TEST [[
---@type [ClassA, ClassB]
local <?x?>
]]
[[
local x: [ClassA, ClassB] {
[1]: ClassA,
[2]: ClassB,
}
]]

--TEST [[
-----@class ClassA
-----@class ClassB
Expand Down

0 comments on commit 8fc5a88

Please sign in to comment.