summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/not-yieldable.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-11-02 21:24:46 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-11-02 21:24:46 +0800
commit7a3b9a09737473efd80ba0539374e4c177bb27e7 (patch)
treeaaa88295fc6eaf235664ce1e79ddd0deff34fea2 /script/core/diagnostics/not-yieldable.lua
parentfbb038b568fffe8ddbf6946cd0f7d49a624b496a (diff)
downloadlua-language-server-7a3b9a09737473efd80ba0539374e4c177bb27e7.zip
not-yieldable
Diffstat (limited to 'script/core/diagnostics/not-yieldable.lua')
-rw-r--r--script/core/diagnostics/not-yieldable.lua54
1 files changed, 54 insertions, 0 deletions
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