diff options
-rw-r--r-- | changelog.md | 7 | ||||
-rw-r--r-- | script/vm/compiler.lua | 17 | ||||
-rw-r--r-- | test/type_inference/init.lua | 5 |
3 files changed, 21 insertions, 8 deletions
diff --git a/changelog.md b/changelog.md index b248b6f4..f8c80f5c 100644 --- a/changelog.md +++ b/changelog.md @@ -114,6 +114,12 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. } ``` * `CHG` [#1533] supports completion with table field of function +* `CHG` [#1457] infer parameter type by function type + ```lua + ---@type fun(x: number) + local function f(x) --> `x` is inferred as `number` + end + ``` * `FIX` [#1479] * `FIX` [#1480] * `FIX` [#1567] @@ -132,6 +138,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. [#1202]: https://github.com/sumneko/lua-language-server/issues/1202 [#1332]: https://github.com/sumneko/lua-language-server/issues/1332 [#1344]: https://github.com/sumneko/lua-language-server/issues/1344 +[#1457]: https://github.com/sumneko/lua-language-server/issues/1457 [#1458]: https://github.com/sumneko/lua-language-server/issues/1458 [#1479]: https://github.com/sumneko/lua-language-server/issues/1479 [#1480]: https://github.com/sumneko/lua-language-server/issues/1480 diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 826c3742..9e8acb0a 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1111,6 +1111,8 @@ local compilerSwitch = util.switch() : call(function (source) vm.setNode(source, source) + local parent = source.parent + if source.bindDocs then for _, doc in ipairs(source.bindDocs) do if doc.type == 'doc.overload' then @@ -1120,16 +1122,16 @@ local compilerSwitch = util.switch() end -- table.sort(string[], function (<?x?>) end) - if source.parent.type == 'callargs' then - local call = source.parent.parent + if parent.type == 'callargs' then + local call = parent.parent vm.compileCallArg(source, call) end -- function f() return function (<?x?>) end end - if source.parent.type == 'return' then - for i, ret in ipairs(source.parent) do + if parent.type == 'return' then + for i, ret in ipairs(parent) do if ret == source then - local func = guide.getParentFunction(source.parent) + local func = guide.getParentFunction(parent) if func then local returnObj = vm.getReturnOfFunction(func, i) if returnObj then @@ -1142,9 +1144,8 @@ local compilerSwitch = util.switch() end -- { f = function (<?x?>) end } - if source.parent.type == 'tablefield' - or source.parent.type == 'tableindex' then - vm.setNode(source, vm.compileNode(source.parent)) + if guide.isSet(parent) then + vm.setNode(source, vm.compileNode(parent)) end end) : case 'paren' diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 9af0d28c..36bbaa83 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -3931,3 +3931,8 @@ local t = { [5] = function(<?x?>) end, } ]] + +TEST 'number' [[ +---@type fun(x: number) +local function f(<?x?>) end +]] |