diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-28 03:23:55 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-28 03:23:55 +0800 |
commit | abdeade219bde1714fd8f145652d42315e1e6d25 (patch) | |
tree | 018962a9f93b3b8159a3c146fd64dd33901380f6 /script | |
parent | ae024958da9a14ed837495f79972a85458ee3f0c (diff) | |
download | lua-language-server-abdeade219bde1714fd8f145652d42315e1e6d25.zip |
new diag ``redundant-return-value``
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/redundant-return-value.lua | 69 | ||||
-rw-r--r-- | script/files.lua | 1 | ||||
-rw-r--r-- | script/proto/diagnostic.lua | 1 | ||||
-rw-r--r-- | script/vm/runner.lua | 3 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 1 |
5 files changed, 73 insertions, 2 deletions
diff --git a/script/core/diagnostics/redundant-return-value.lua b/script/core/diagnostics/redundant-return-value.lua new file mode 100644 index 00000000..8f8422fe --- /dev/null +++ b/script/core/diagnostics/redundant-return-value.lua @@ -0,0 +1,69 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +local function hasDocReturn(func) + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + return true + end + end + return false +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'function', function (source) + if not hasDocReturn(source) then + return + end + local _, max = vm.countReturnsOfFunction(source) + local returns = source.returns + if not returns then + return + end + for _, ret in ipairs(returns) do + local rmin, rmax = vm.countList(ret) + if rmin > max then + for i = max + 1, #ret - 1 do + callback { + start = ret[i].start, + finish = ret[i].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = i, + }), + } + end + if #ret == rmax then + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = rmax, + }), + } + else + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE_RANGE', { + max = max, + rmin = #ret, + rmax = rmax, + }), + } + end + end + end + end) +end diff --git a/script/files.lua b/script/files.lua index 3bef6d33..d9148e27 100644 --- a/script/files.lua +++ b/script/files.lua @@ -457,7 +457,6 @@ function m.eachFile(suri) end --- Pairs dll files ----@return function function m.eachDll() local map = {} for uri, file in pairs(m.dllMap) do diff --git a/script/proto/diagnostic.lua b/script/proto/diagnostic.lua index 5972150c..e449d864 100644 --- a/script/proto/diagnostic.lua +++ b/script/proto/diagnostic.lua @@ -59,6 +59,7 @@ m.register { 'redundant-parameter', 'missing-parameter', 'missing-return-value', + 'redundant-return-value', } { group = 'unbalanced', severity = 'Warning', diff --git a/script/vm/runner.lua b/script/vm/runner.lua index 0dd33595..b8ac13fd 100644 --- a/script/vm/runner.lua +++ b/script/vm/runner.lua @@ -113,7 +113,8 @@ end ---@param action parser.object ---@param topNode vm.node ---@param outNode? vm.node ----@return vm.node +---@return vm.node topNode +---@return vm.node outNode function mt:_lookInto(action, topNode, outNode) if not action then return topNode, outNode diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 3aebc5e0..d24cbfe9 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -397,6 +397,7 @@ end ---@param uriOrPath uri|string ---@return string +---@return boolean suc function m.getRelativePath(uriOrPath) local path, uri if uriOrPath:sub(1, 5) == 'file:' then |