summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/not-yieldable.lua
blob: ed8533809faae4d3b657908015080675a28703dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

---@async
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