summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/not-yieldable.lua
blob: 612b5b9861f033eb2167c1c2fe77442cc1ad259f (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
56
57
58
59
60
61
62
63
local files = require 'files'
local await = require 'await'
local guide = require 'parser.guide'
local vm    = require 'vm'
local lang  = require 'language'
local infer = require 'core.infer'

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 then
                if infer.hasType(arg, 'any')
                or vm.isAsync(arg, true) then
                    return true
                end
            end
        end
        if def.type == 'doc.type.function' then
            hasFuncDef = true
            local arg = def.args and def.args[i]
            if arg then
                if infer.hasType(arg.extends, 'any')
                or vm.isAsync(arg.extends, true) then
                    return true
                end
            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 vm.isLinkedCall(source.node, i)
            and not isYieldAble(defs, i) then
                callback {
                    start   = arg.start,
                    finish  = arg.finish,
                    message = lang.script('DIAG_NOT_YIELDABLE', i),
                }
            end
        end
    end)
end