summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/undefined-field.lua
blob: f73bfab8df09d27e59c45f5203211bde3e4c2567 (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
80
81
82
83
84
85
86
87
88
89
90
91
local files   = require 'files'
local vm      = require 'vm'
local lang    = require 'language'
local config  = require 'config'
local guide   = require 'parser.guide'
local define  = require 'proto.define'

return function (uri, callback)
    local ast = files.getAst(uri)
    if not ast then
        return
    end

    local function getAllDocClassFromInfer(src)
        local infers = vm.getInfers(src, 0)

        if not infers then
            return nil
        end

        local allDocClass = {}
        for i = 1, #infers do
            local infer = infers[i]
            if infer.type ~= '_G' then
                local inferSource = infer.source
                if inferSource.type == 'doc.class' then
                    allDocClass[#allDocClass+1] = inferSource
                elseif inferSource.type == 'doc.class.name' then
                    allDocClass[#allDocClass+1] = inferSource.parent
                end
            end
        end

        return allDocClass
    end

    ---@param allDocClass table int
    local function getAllFieldsFromAllDocClass(allDocClass)
        local fields = {}
        local empty = true
        for _, docClass in ipairs(allDocClass) do
            local refs = vm.getFields(docClass)

            for _, ref in ipairs(refs) do
                if ref.type == 'getfield' or ref.type == 'getmethod' then
                    goto CONTINUE
                end
                local name = vm.getKeyName(ref)
                if not name or vm.getKeyType(ref) ~= 'string' then
                    goto CONTINUE
                end
                fields[name] = true
                empty = false
                ::CONTINUE::
            end
        end
       
        if empty then
            return nil
        else
            return fields
        end
    end

    local function checkUndefinedField(src)
        local fieldName = guide.getKeyName(src)

        local allDocClass = getAllDocClassFromInfer(src.node)
        if (not allDocClass) or (#allDocClass == 0) then
            return
        end

        local fields = getAllFieldsFromAllDocClass(allDocClass)

        -- 没找到任何 field,跳过检查
        if not fields then
            return
        end

        if not fields[fieldName] then
            local message = lang.script('DIAG_UNDEF_FIELD', fieldName)
            callback {
                start   = src.start,
                finish  = src.finish,
                message = message,
            }
        end
    end
    guide.eachSourceType(ast.ast, 'getfield', checkUndefinedField);
    guide.eachSourceType(ast.ast, 'getmethod', checkUndefinedField);
end