summaryrefslogtreecommitdiff
path: root/server/src/matcher/diagnostics.lua
blob: ffdab80e8588468531b7c0cbfc3bb1ab3d95a68e (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
--[[
data = {
    start = 1,
    finish = 1,
    level = 'Error' or 'Warning' or 'Information' or 'Hint',
    code = '',
    message = '',
}
]]--

local function searchUnusedLocals(results, callback)
    for _, var in ipairs(results.vars) do
        if var.type ~= 'local' then
            goto NEXT_VAR
        end
        if var.key == 'self'
        or var.key == '_'
        or var.key == '_ENV'
        then
            goto NEXT_VAR
        end
        for _, info in ipairs(var) do
            if info.type == 'get' then
                goto NEXT_VAR
            end
        end
        callback(var.source.start, var.source.finish, var.key)
        ::NEXT_VAR::
    end
end

return function (ast, results, lines)
    local datas = {}
    -- 搜索未使用的局部变量
    searchUnusedLocals(results, function (start, finish, code)
        datas[#datas+1] = {
            start   = start,
            finish  = finish,
            level   = 'Warning',
            code    = code,
            message = 'Unused local', -- LOCALE
        }
    end)
    return datas
end