summaryrefslogtreecommitdiff
path: root/server-beta/src/core/diagnostics/global-in-nil-env.lua
blob: 9a0d4f358e9ab9718ed7039785fb562299481315 (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
local files    = require 'files'
local guide    = require 'parser.guide'
local lang     = require 'language'

-- TODO: 检查路径是否可达
local function mayRun(path)
    return true
end

return function (uri, callback)
    local ast = files.getAst(uri)
    if not ast then
        return
    end
    local root = guide.getRoot(ast.ast)
    local env  = guide.getENV(root)

    local nilDefs = {}
    if not env.ref then
        return
    end
    for _, ref in ipairs(env.ref) do
        if ref.type == 'setlocal' then
            if ref.value and ref.value.type == 'nil' then
                nilDefs[#nilDefs+1] = ref
            end
        end
    end

    if #nilDefs == 0 then
        return
    end

    local function check(source)
        local node = source.node
        if node.tag == '_ENV' then
            local ok
            for _, nilDef in ipairs(nilDefs) do
                local mode, pathA = guide.getPath(nilDef, source)
                if  mode == 'before'
                and mayRun(pathA) then
                    ok = nilDef
                    break
                end
            end
            if ok then
                callback {
                    start   = source.start,
                    finish  = source.finish,
                    uri     = uri,
                    message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
                    related = {
                        {
                            start  = ok.start,
                            finish = ok.finish,
                            uri    = uri,
                        }
                    }
                }
            end
        end
    end

    guide.eachSourceType(ast.ast, 'getglobal', check)
    guide.eachSourceType(ast.ast, 'setglobal', check)
end