summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/undefined-field.lua
blob: ea15dcdaac19792bc0c14adf0e402bb9a437df91 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local files   = require 'files'
local vm      = require 'vm'
local lang    = require 'language'
local guide   = require 'parser.guide'
local await   = require 'await'

local skipCheckClass = {
    ['unknown']       = true,
    ['any']           = true,
    ['table']         = true,
    ['nil']           = true,
    ['number']        = true,
    ['integer']       = true,
    ['boolean']       = true,
    ['function']      = true,
    ['userdata']      = true,
    ['lightuserdata'] = true,
}

---@async
return function (uri, callback)
    local ast = files.getState(uri)
    if not ast then
        return
    end

    local cache = {}

    ---@async
    local function checkUndefinedField(src)
        local id = noder.getID(src)
        if not id then
            return
        end
        if cache[id] then
            return
        end

        await.delay()

        if #vm.getDefs(src) > 0 then
            cache[id] = true
            return
        end
        local node = src.node
        if node then
            local defs = vm.getDefs(node)
            local ok
            for _, def in ipairs(defs) do
                if  def.type == 'doc.class.name'
                and not skipCheckClass[def[1]] then
                    ok = true
                    break
                end
            end
            if not ok then
                cache[id] = true
                return
            end
        end
        local message = lang.script('DIAG_UNDEF_FIELD', guide.getKeyName(src))
        if     src.type == 'getfield' and src.field then
            callback {
                start   = src.field.start,
                finish  = src.field.finish,
                message = message,
            }
        elseif src.type == 'getmethod' and src.method then
            callback {
                start   = src.method.start,
                finish  = src.method.finish,
                message = message,
            }
        end
    end
    guide.eachSourceType(ast.ast, 'getfield',  checkUndefinedField)
    guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField)
    guide.eachSourceType(ast.ast, 'getindex',  checkUndefinedField)
end