From a927549a7a32d173eb686022f3cf1bfad9815f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 4 Jan 2021 20:30:17 +0800 Subject: `unused-function` checks recursive --- script/core/diagnostics/unused-function.lua | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'script') diff --git a/script/core/diagnostics/unused-function.lua b/script/core/diagnostics/unused-function.lua index e6ae9386..41c239f9 100644 --- a/script/core/diagnostics/unused-function.lua +++ b/script/core/diagnostics/unused-function.lua @@ -23,25 +23,33 @@ return function (uri, callback) if not ast then return end - -- 只检查局部函数 - guide.eachSourceType(ast.ast, 'function', function (source) + + local cache = {} + local function checkFunction(source) + if cache[source] ~= nil then + return cache[source] + end + cache[source] = false local parent = source.parent if not parent then - return + return false end if parent.type ~= 'local' and parent.type ~= 'setlocal' then - return + return false end if isToBeClosed(parent) then - return + return false end local hasGet local refs = vm.getRefs(source) for _, src in ipairs(refs) do if vm.isGet(src) then - hasGet = true - break + local func = guide.getParentFunction(src) + if not checkFunction(func) then + hasGet = true + break + end end end if not hasGet then @@ -60,6 +68,14 @@ return function (uri, callback) message = lang.script.DIAG_UNUSED_FUNCTION, } end + cache[source] = true + return true end + return false + end + + -- 只检查局部函数 + guide.eachSourceType(ast.ast, 'function', function (source) + checkFunction(source) end) end -- cgit v1.2.3