diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-05-23 21:24:29 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-05-23 21:24:29 +0800 |
commit | f0f80fdf1b5c83467687541ffda36cabb8f9bd62 (patch) | |
tree | 155b907025c269dd7cf2a0eb7588de9aa8f47163 | |
parent | c1e9e46dea95140fd14fd12f99fc098c59ba9fcd (diff) | |
download | lua-language-server-f0f80fdf1b5c83467687541ffda36cabb8f9bd62.zip |
fix #1131
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/core/diagnostics/missing-parameter.lua | 58 | ||||
-rw-r--r-- | test/diagnostics/common.lua | 26 |
3 files changed, 85 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index 097d692b..54b0a129 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 3.2.4 * `FIX` hover: can not union `table` with other basic types * `FIX` [#1125](https://github.com/sumneko/lua-language-server/issues/1125) +* `FIX` [#1131](https://github.com/sumneko/lua-language-server/issues/1131) ## 3.2.3 `2022-5-16` diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua index 698680ca..f69afa79 100644 --- a/script/core/diagnostics/missing-parameter.lua +++ b/script/core/diagnostics/missing-parameter.lua @@ -3,11 +3,69 @@ local guide = require 'parser.guide' local vm = require 'vm' local lang = require 'language' +---@param source parser.object +---@return integer +local function countReturns(source) + local n = 0 + + local docs = source.bindDocs + if docs then + for _, doc in ipairs(docs) do + if doc.type == 'doc.return' then + for _, rtn in ipairs(doc.returns) do + if rtn.returnIndex and rtn.returnIndex > n then + n = rtn.returnIndex + end + end + end + end + end + + local returns = source.returns + if returns then + for _, rtn in ipairs(returns) do + if #rtn > n then + n = #rtn + end + end + end + + return n +end + +local function countMaxReturns(source) + local hasFounded + local n = 0 + for _, def in ipairs(vm.getDefs(source)) do + if def.type == 'doc.type.function' + or def.type == 'function' then + hasFounded = true + local rets = countReturns(def) + if rets > n then + n = rets + end + end + end + + if hasFounded then + return n + else + return math.huge + end +end + local function countCallArgs(source) local result = 0 if not source.args then return 0 end + local lastArg = source.args[#source.args] + if lastArg.type == 'varargs' then + return math.huge + end + if lastArg.type == 'call' then + result = result + countMaxReturns(lastArg) - 1 + end result = result + #source.args return result end diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index 56d1cbcc..eddf9c26 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -283,6 +283,32 @@ x(1, 2) ]] TEST [[ +---@diagnostic disable: unused-local + +---@param a integer +---@param b integer +local function f(a, b) +end + +f(...) +]] + +TEST [[ +---@diagnostic disable: unused-local + +---@param a integer +---@param b integer +local function f(a, b) +end + +local function return2Numbers() + return 1, 2 +end + +f(return2Numbers()) +]] + +TEST [[ ---@param a integer ---@param b? integer local function x(a, b) |