Skip to content

Commit 8fc5a88

Browse files
authored
Merge pull request #2505 from lizho/master
support tuple type
2 parents 7848d03 + 7503881 commit 8fc5a88

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

script/parser/luadoc.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,78 @@ 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+
if not field.extends then
421+
break
422+
end
423+
field.optional = field.extends.optional
424+
field.start = field.extends.start
425+
field.finish = field.extends.finish
426+
if needCloseParen then
427+
nextSymbolOrError ')'
428+
end
429+
end
430+
431+
typeUnit.fields[#typeUnit.fields+1] = field
432+
if checkToken('symbol', ',', 1)
433+
or checkToken('symbol', ';', 1) then
434+
nextToken()
435+
else
436+
nextSymbolOrError(']')
437+
break
438+
end
439+
end
440+
typeUnit.finish = getFinish()
441+
return typeUnit
442+
end
443+
372444
local function parseSigns(parent)
373445
if not checkToken('symbol', '<', 1) then
374446
return nil
@@ -682,6 +754,7 @@ end
682754
function parseTypeUnit(parent)
683755
local result = parseFunction(parent)
684756
or parseTable(parent)
757+
or parseTuple(parent)
685758
or parseString(parent)
686759
or parseCode(parent)
687760
or parseInteger(parent)
@@ -864,6 +937,7 @@ local docSwitch = util.switch()
864937
while true do
865938
local extend = parseName('doc.extends.name', result)
866939
or parseTable(result)
940+
or parseTuple(result)
867941
if not extend then
868942
pushWarning {
869943
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)