From 7a3b9a09737473efd80ba0539374e4c177bb27e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 2 Nov 2021 21:24:46 +0800 Subject: not-yieldable --- script/core/diagnostics/not-yieldable.lua | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 script/core/diagnostics/not-yieldable.lua (limited to 'script/core/diagnostics/not-yieldable.lua') diff --git a/script/core/diagnostics/not-yieldable.lua b/script/core/diagnostics/not-yieldable.lua new file mode 100644 index 00000000..2f80e4d0 --- /dev/null +++ b/script/core/diagnostics/not-yieldable.lua @@ -0,0 +1,54 @@ +local files = require 'files' +local await = require 'await' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +local function isYieldAble(defs, i) + local hasFuncDef + for _, def in ipairs(defs) do + if def.type == 'function' then + hasFuncDef = true + local arg = def.args and def.args[i] + if arg and vm.isAsync(arg, true) then + return true + end + end + if def.type == 'doc.type.function' then + hasFuncDef = true + local arg = def.args and def.args[i] + if arg and vm.isAsync(arg.extends, true) then + return true + end + end + end + return not hasFuncDef +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'call', function (source) ---@async + if not source.args then + return + end + await.delay() + local defs = vm.getDefs(source.node) + if #defs == 0 then + return + end + for i, arg in ipairs(source.args) do + if vm.isAsync(arg, true) + and not isYieldAble(defs, i) then + callback { + start = source.node.start, + finish = source.node.finish, + message = lang.script('DIAG_NOT_YIELDABLE', i), + } + end + end + end) +end -- cgit v1.2.3