summaryrefslogtreecommitdiff
path: root/script-beta/core/diagnostics/unused-function.lua
blob: 52c7749a0cf5cefdfe034749a2b9eb50bf0e30ef (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
local files   = require 'files'
local guide   = require 'parser.guide'
local vm      = require 'vm'
local define  = require 'proto.define'
local lang    = require 'language'
local await   = require 'await'

return function (uri, callback)
    local ast = files.getAst(uri)
    if not ast then
        return
    end
    -- 只检查局部函数与全局函数
    guide.eachSourceType(ast.ast, 'function', function (source)
        local parent = source.parent
        if not parent then
            return
        end
        if  parent.type ~= 'local'
        and parent.type ~= 'setlocal'
        and parent.type ~= 'setglobal' then
            return
        end
        local hasSet
        local hasGet
        vm.eachRef(source, function (src)
            if vm.isSet(src) then
                hasSet = true
            else
                hasGet = true
            end
        end)
        if not hasGet and hasSet then
            callback {
                start   = source.start,
                finish  = source.finish,
                tags    = { define.DiagnosticTag.Unnecessary },
                message = lang.script.DIAG_UNUSED_FUNCTION,
            }
        end
    end)
end