summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/not-yieldable.lua
blob: 055025d4636ae3023f2538c32f9553ec24ad14a6 (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 function isYieldAble(defs, i)
    local hasFuncDef
    for _, def in ipairs(defs) do
        if def.type == 'function' then
            local arg = def.args and def.args[i]
            if arg then
                hasFuncDef = true
                if vm.getInfer(arg):hasType(guide.getUri(def), 'any')
                or vm.isAsync(arg, true)
                or arg.type == '...' then
                    return true
                end
            end
        end
        if def.type == 'doc.type.function' then
            local arg = def.args and def.args[i]
            if arg then
                hasFuncDef = true
                if vm.getInfer(arg.extends):hasType(guide.getUri(def), '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