Skip to content

Commit 7f95e6f

Browse files
authored
support tuple type
1 parent 9185dfa commit 7f95e6f

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

script/parser/luadoc.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,75 @@ local function parseTable(parent)
369369
return typeUnit
370370
end
371371

372+
local function parseTuple(parent)
373+
if not checkToken('symbol', '[', 1) then
374+
return nil
375+
end
376+
nextToken()
377+
local typeUnit = {
378+
type = 'doc.type.table',
379+
start = getStart(),
380+
parent = parent,
381+
fields = {},
382+
isTuple = true,
383+
}
384+
385+
local index = 1
386+
while true do
387+
if checkToken('symbol', ']', 1) then
388+
nextToken()
389+
break
390+
end
391+
local field = {
392+
type = 'doc.type.field',
393+
parent = typeUnit,
394+
}
395+
396+
do
397+
local needCloseParen
398+
if checkToken('symbol', '(', 1) then
399+
nextToken()
400+
needCloseParen = true
401+
end
402+
field.name = {
403+
type = 'doc.type',
404+
start = getFinish(),
405+
firstFinish = getFinish(),
406+
finish = getFinish(),
407+
parent = field,
408+
}
409+
field.name.types = {
410+
[1] = {
411+
type = 'doc.type.integer',
412+
start = getFinish(),
413+
finish = getFinish(),
414+
parent = field.name,
415+
[1] = index,
416+
}
417+
}
418+
index = index + 1
419+
field.extends = parseType(field)
420+
field.optional = field.extends.optional
421+
field.start = field.extends.start
422+
field.finish = field.extends.finish
423+
if needCloseParen then
424+
nextSymbolOrError ')'
425+
end
426+
end
427+
428+
typeUnit.fields[#typeUnit.fields+1] = field
429+
if checkToken('symbol', ',', 1)
430+
or checkToken('symbol', ';', 1) then
431+
nextToken()
432+
else
433+
nextSymbolOrError(']')
434+
break
435+
end
436+
end
437+
typeUnit.finish = getFinish()
438+
return typeUnit
439+
end
440+
372441
local function parseSigns(parent)
373442
if not checkToken('symbol', '<', 1) then
374443
return nil
@@ -682,6 +751,7 @@ end
682751
function parseTypeUnit(parent)
683752
local result = parseFunction(parent)
684753
or parseTable(parent)
754+
or parseTuple(parent)
685755
or parseString(parent)
686756
or parseCode(parent)
687757
or parseInteger(parent)
@@ -864,6 +934,7 @@ local docSwitch = util.switch()
864934
while true do
865935
local extend = parseName('doc.extends.name', result)
866936
or parseTable(result)
937+
or parseTuple(result)
867938
if not extend then
868939
pushWarning {
869940
type = 'LUADOC_MISS_CLASS_EXTENDS_NAME',

script/vm/infer.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,24 @@ local viewNodeSwitch;viewNodeSwitch = util.switch()
157157
end
158158
infer._hasClass = true
159159
local buf = {}
160-
buf[#buf+1] = '{ '
160+
buf[#buf+1] = source.isTuple and '[' or '{ '
161161
for i, field in ipairs(source.fields) do
162162
if i > 1 then
163163
buf[#buf+1] = ', '
164164
end
165-
local key = field.name
166-
if key.type == 'doc.type' then
167-
buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
168-
elseif type(key[1]) == 'string' then
169-
buf[#buf+1] = key[1] .. ': '
170-
else
171-
buf[#buf+1] = ('[%q]: '):format(key[1])
165+
if not source.isTuple then
166+
local key = field.name
167+
if key.type == 'doc.type' then
168+
buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
169+
elseif type(key[1]) == 'string' then
170+
buf[#buf+1] = key[1] .. ': '
171+
else
172+
buf[#buf+1] = ('[%q]: '):format(key[1])
173+
end
172174
end
173175
buf[#buf+1] = vm.getInfer(field.extends):view(uri)
174176
end
175-
buf[#buf+1] = ' }'
177+
buf[#buf+1] = source.isTuple and ']' or ' }'
176178
return table.concat(buf)
177179
end)
178180
: case 'doc.type.string'

test/diagnostics/assign-type-mismatch.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ local t = {}
9898
t['x'] = nil
9999
]]
100100

101+
TEST [[
102+
---@type [boolean]
103+
local t = { <![1]!> = nil }
104+
105+
t = nil
106+
]]
107+
101108
TEST [[
102109
local t = { true }
103110

test/hover/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,17 @@ local <?x?>
12281228
local x: table<ClassA, ClassB>
12291229
]]
12301230

1231+
TEST [[
1232+
---@type [ClassA, ClassB]
1233+
local <?x?>
1234+
]]
1235+
[[
1236+
local x: [ClassA, ClassB] {
1237+
[1]: ClassA,
1238+
[2]: ClassB,
1239+
}
1240+
]]
1241+
12311242
--TEST [[
12321243
-----@class ClassA
12331244
-----@class ClassB

0 commit comments

Comments
 (0)