Skip to content

Commit 8850130

Browse files
committed
type check: supports array part in literal table
resolve #1374
1 parent fe5eb50 commit 8850130

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
141141

142142
x = y --> Warning: Cannot assign `<string, number>` to `<string, string>`
143143
```
144+
* `CHG` [#1374] type check: supports array part in literal table
145+
```lua
146+
---@type boolean[]
147+
local t = { 1, 2, 3 } --> Warning: Cannot assign `integer` to `boolean`
148+
```
144149
* `FIX` [#1479]
145150
* `FIX` [#1480]
146151
* `FIX` [#1567]
@@ -159,6 +164,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
159164
[#1202]: https://github.com/sumneko/lua-language-server/issues/1202
160165
[#1332]: https://github.com/sumneko/lua-language-server/issues/1332
161166
[#1344]: https://github.com/sumneko/lua-language-server/issues/1344
167+
[#1374]: https://github.com/sumneko/lua-language-server/issues/1374
162168
[#1434]: https://github.com/sumneko/lua-language-server/issues/1434
163169
[#1457]: https://github.com/sumneko/lua-language-server/issues/1457
164170
[#1458]: https://github.com/sumneko/lua-language-server/issues/1458

script/core/diagnostics/assign-type-mismatch.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ local checkTypes = {
1212
'setindex',
1313
'setmethod',
1414
'tablefield',
15-
'tableindex'
15+
'tableindex',
16+
'tableexp',
1617
}
1718

1819
---@param source parser.object

script/vm/compiler.lua

+12-8
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ local compilerSwitch = util.switch()
13431343
if not hasMarkDoc then
13441344
vm.compileByParentNode(source.node, guide.getKeyName(source), false, function (src)
13451345
if src.type == 'doc.field'
1346-
or src.type == 'doc.type.field' then
1346+
or src.type == 'doc.type.field'
1347+
or src.type == 'doc.type.name' then
13471348
hasMarkDoc = true
13481349
vm.setNode(source, vm.compileNode(src))
13491350
end
@@ -1362,15 +1363,18 @@ local compilerSwitch = util.switch()
13621363
end)
13631364
: case 'tableexp'
13641365
: call(function (source)
1365-
if (source.parent.type == 'table') then
1366-
local node = vm.compileNode(source.parent)
1367-
for n in node:eachObject() do
1368-
if n.type == 'doc.type.array' then
1369-
vm.setNode(source, vm.compileNode(n.node))
1370-
end
1366+
local hasMarkDoc
1367+
vm.compileByParentNode(source.parent, source.tindex, false, function (src)
1368+
if src.type == 'doc.field'
1369+
or src.type == 'doc.type.field'
1370+
or src.type == 'doc.type.name' then
1371+
hasMarkDoc = true
1372+
vm.setNode(source, vm.compileNode(src))
13711373
end
1374+
end)
1375+
if not hasMarkDoc then
1376+
vm.setNode(source, vm.compileNode(source.value))
13721377
end
1373-
vm.setNode(source, vm.compileNode(source.value))
13741378
end)
13751379
: case 'function.return'
13761380
---@param source parser.object

test/diagnostics/type-check.lua

+14
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,20 @@ local function bar(opts)
942942
end
943943
]]
944944

945+
TEST [[
946+
---@type {[1]: string, [10]: number, xx: boolean}
947+
local t = {
948+
<!true!>,
949+
<![10]!> = 's',
950+
<!xx!> = 1,
951+
}
952+
]]
953+
954+
TEST [[
955+
---@type boolean[]
956+
local t = { <!1!>, <!2!>, <!3!> }
957+
]]
958+
945959
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
946960
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
947961
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)