summaryrefslogtreecommitdiff
path: root/script/core/diagnostics
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-22 01:41:16 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-22 01:41:16 +0800
commit7b3e7f4b0a62b7c9c3b895e89b4fe79bb8f350ba (patch)
treeee441dc6545127e650c6fcf435c96d33079ac177 /script/core/diagnostics
parentd1e977ca8a6d72673649282bcd1973b1d279a8a1 (diff)
downloadlua-language-server-7b3e7f4b0a62b7c9c3b895e89b4fe79bb8f350ba.zip
fix
Diffstat (limited to 'script/core/diagnostics')
-rw-r--r--script/core/diagnostics/missing-parameter.lua122
-rw-r--r--script/core/diagnostics/redundant-parameter.lua63
2 files changed, 18 insertions, 167 deletions
diff --git a/script/core/diagnostics/missing-parameter.lua b/script/core/diagnostics/missing-parameter.lua
index 9844046f..b6067175 100644
--- a/script/core/diagnostics/missing-parameter.lua
+++ b/script/core/diagnostics/missing-parameter.lua
@@ -3,116 +3,6 @@ local guide = require 'parser.guide'
local vm = require 'vm'
local lang = require 'language'
----@param source parser.object
----@return integer
-local function countReturnsOfFunction(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
-
----@param source parser.object
----@return integer
-local function countReturnsOfDocFunction(source)
- return #source.returns
-end
-
-local function countMaxReturns(source)
- local hasFounded
- local n = 0
- for _, def in ipairs(vm.getDefs(source)) do
- if def.type == 'function' then
- hasFounded = true
- local rets = countReturnsOfFunction(def)
- if rets > n then
- n = rets
- end
- elseif def.type == 'doc.type.function' then
- hasFounded = true
- local rets = countReturnsOfDocFunction(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.node) - 1
- end
- result = result + #source.args
- return result
-end
-
----@return integer
-local function countFuncArgs(source)
- if not source.args or #source.args == 0 then
- return 0
- end
- local count = 0
- for i = #source.args, 1, -1 do
- local arg = source.args[i]
- if arg.type ~= '...'
- and not (arg.name and arg.name[1] =='...')
- and not vm.compileNode(arg):isNullable() then
- return i
- end
- end
- return count
-end
-
-local function getFuncArgs(func)
- local funcArgs
- local defs = vm.getDefs(func)
- for _, def in ipairs(defs) do
- if def.type == 'function'
- or def.type == 'doc.type.function' then
- local args = countFuncArgs(def)
- if not funcArgs or args < funcArgs then
- funcArgs = args
- end
- end
- end
- return funcArgs
-end
-
return function (uri, callback)
local state = files.getState(uri)
if not state then
@@ -120,19 +10,15 @@ return function (uri, callback)
end
guide.eachSourceType(state.ast, 'call', function (source)
- local callArgs = countCallArgs(source)
+ local _, callArgs = vm.countList(source.args)
- local func = source.node
- local funcArgs = getFuncArgs(func)
+ local funcNode = vm.compileNode(source.node)
+ local funcArgs = vm.countParamsOfNode(funcNode)
- if not funcArgs then
+ if callArgs >= funcArgs then
return
end
- local delta = callArgs - funcArgs
- if delta >= 0 then
- return
- end
callback {
start = source.start,
finish = source.finish,
diff --git a/script/core/diagnostics/redundant-parameter.lua b/script/core/diagnostics/redundant-parameter.lua
index 41781df8..2b7f1230 100644
--- a/script/core/diagnostics/redundant-parameter.lua
+++ b/script/core/diagnostics/redundant-parameter.lua
@@ -3,43 +3,6 @@ local guide = require 'parser.guide'
local vm = require 'vm'
local lang = require 'language'
-local function countCallArgs(source)
- local result = 0
- if not source.args then
- return 0
- end
- result = result + #source.args
- return result
-end
-
-local function countFuncArgs(source)
- if not source.args or #source.args == 0 then
- return 0
- end
- local lastArg = source.args[#source.args]
- if lastArg.type == '...'
- or (lastArg.name and lastArg.name[1] == '...') then
- return math.maxinteger
- else
- return #source.args
- end
-end
-
-local function getFuncArgs(func)
- local funcArgs
- local defs = vm.getDefs(func)
- for _, def in ipairs(defs) do
- if def.type == 'function'
- or def.type == 'doc.type.function' then
- local args = countFuncArgs(def)
- if not funcArgs or args > funcArgs then
- funcArgs = args
- end
- end
- end
- return funcArgs
-end
-
return function (uri, callback)
local state = files.getState(uri)
if not state then
@@ -47,28 +10,30 @@ return function (uri, callback)
end
guide.eachSourceType(state.ast, 'call', function (source)
- local callArgs = countCallArgs(source)
+ local callArgs = vm.countList(source.args)
if callArgs == 0 then
return
end
- local func = source.node
- local funcArgs = getFuncArgs(func)
-
- if not funcArgs then
- return
- end
+ local funcNode = vm.compileNode(source.node)
+ local _, funcArgs = vm.countParamsOfNode(funcNode)
- local delta = callArgs - funcArgs
- if delta <= 0 then
+ if callArgs <= funcArgs then
return
end
if callArgs == 1 and source.node.type == 'getmethod' then
return
end
- for i = #source.args - delta + 1, #source.args do
- local arg = source.args[i]
- if arg then
+ if funcArgs + 1 > #source.args then
+ local lastArg = source.args[#source.args]
+ callback {
+ start = lastArg.start,
+ finish = lastArg.finish,
+ message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, callArgs)
+ }
+ else
+ for i = funcArgs + 1, #source.args do
+ local arg = source.args[i]
callback {
start = arg.start,
finish = arg.finish,