diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-27 19:07:46 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-27 19:07:46 +0800 |
commit | edc9797838506728ba65a50b94781ac99e3bdd86 (patch) | |
tree | 27aac9c1216cc2075621ad36da57183111cd6c22 /script/core | |
parent | cae348536cef3f2f5f96c5821c7e45539aeff1cf (diff) | |
download | lua-language-server-edc9797838506728ba65a50b94781ac99e3bdd86.zip |
diagnostic `missing-return-value`
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/diagnostics/missing-return-value.lua | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/script/core/diagnostics/missing-return-value.lua b/script/core/diagnostics/missing-return-value.lua new file mode 100644 index 00000000..491475c7 --- /dev/null +++ b/script/core/diagnostics/missing-return-value.lua @@ -0,0 +1,62 @@ +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 min = vm.countReturnsOfFunction(source) + if min == 0 then + return + end + local returns = source.returns + if not returns then + return + end + for _, ret in ipairs(returns) do + local rmin, rmax = vm.countList(ret) + if rmax < min then + if rmin == rmax then + callback { + start = ret.start, + finish = ret.start + #'return', + message = lang.script('DIAG_MISSING_RETURN_VALUE', { + min = min, + rmax = rmax, + }), + } + else + callback { + start = ret.start, + finish = ret.start + #'return', + message = lang.script('DIAG_MISSING_RETURN_VALUE', { + min = min, + rmin = rmin, + rmax = rmax, + }), + } + end + end + end + end) +end |