summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/undefined-doc-name.lua
blob: d8a4363b2a438aae2f3aef97698d98dd99a2fea5 (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
local files   = require 'files'
local guide   = require 'core.guide'
local lang    = require 'language'
local define  = require 'proto.define'
local vm      = require 'vm'

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

    if not state.ast.docs then
        return
    end

    local classCache = {
        ['any'] = true,
        ['nil'] = true,
    }
    local function hasNameOfClassOrAlias(name)
        if classCache[name] ~= nil then
            return classCache[name]
        end
        local docs = vm.getDocTypes(name)
        for _, otherDoc in ipairs(docs) do
            if otherDoc.type == 'doc.class.name'
            or otherDoc.type == 'doc.alias.name' then
                classCache[name] = true
                return true
            end
        end
        classCache[name] = false
        return false
    end

    local function hasNameOfGeneric(name, source)
        if not source.typeGeneric then
            return false
        end
        if not source.typeGeneric[name] then
            return false
        end
        return true
    end

    guide.eachSource(state.ast.docs, function (source)
        if  source.type ~= 'doc.extends.name'
        and source.type ~= 'doc.type.name' then
            return
        end
        if source.parent.type == 'doc.class' then
            return
        end
        local name = source[1]
        if name == '...' then
            return
        end
        if hasNameOfClassOrAlias(name)
        or hasNameOfGeneric(name, source) then
            return
        end
        callback {
            start   = source.start,
            finish  = source.finish,
            message = lang.script('DIAG_UNDEFINED_DOC_NAME', name)
        }
    end)
end