summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/duplicate-set-field.lua
blob: 7b1718ec8ef716f7b3cd419efa47a31ab738b832 (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
92
93
94
local files    = require 'files'
local searcher = require 'core.searcher'
local lang     = require 'language'
local define   = require 'proto.define'
local guide    = require "parser.guide"

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

    guide.eachSourceType(ast.ast, 'local', function (source)
        if not source.ref then
            return
        end
        local sets = {}
        for _, ref in ipairs(source.ref) do
            if ref.type ~= 'getlocal' then
                goto CONTINUE
            end
            local nxt = ref.next
            if not nxt then
                goto CONTINUE
            end
            if nxt.type == 'setfield'
            or nxt.type == 'setmethod'
            or nxt.type == 'setindex' then
                local name = guide.getKeyName(nxt)
                if not name then
                    goto CONTINUE
                end
                local value = searcher.getObjectValue(nxt)
                if not value or value.type ~= 'function' then
                    goto CONTINUE
                end
                if not sets[name] then
                    sets[name] = {}
                end
                sets[name][#sets[name]+1] = nxt
            end
            ::CONTINUE::
        end
        for name, values in pairs(sets) do
            if #values <= 1 then
                goto CONTINUE_SETS
            end
            local blocks = {}
            for _, value in ipairs(values) do
                local block = searcher.getBlock(value)
                if not blocks[block] then
                    blocks[block] = {}
                end
                blocks[block][#blocks[block]+1] = value
            end
            for _, defs in pairs(blocks) do
                if #defs <= 1 then
                    goto CONTINUE_BLOCKS
                end
                local related = {}
                for i = 1, #defs do
                    local def = defs[i]
                    related[i] = {
                        start  = def.start,
                        finish = def.finish,
                        uri    = uri,
                    }
                end
                for i = 1, #defs - 1 do
                    local def = defs[i]
                    callback {
                        start   = def.start,
                        finish  = def.finish,
                        related = related,
                        message = lang.script('DIAG_DUPLICATE_SET_FIELD', name),
                        level   = define.DiagnosticSeverity.Hint,
                        tags    = { define.DiagnosticTag.Unnecessary },
                    }
                end
                for i = #defs, #defs do
                    local def = defs[i]
                    callback {
                        start   = def.start,
                        finish  = def.finish,
                        related = related,
                        message = lang.script('DIAG_DUPLICATE_SET_FIELD', name),
                    }
                end
                ::CONTINUE_BLOCKS::
            end
            ::CONTINUE_SETS::
        end
    end)
end