summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/duplicate-set-field.lua
blob: a4b205dda75197060df2767ad8ecb2b7466af3c2 (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
local files    = require 'files'
local lang     = require 'language'
local define   = require 'proto.define'
local guide    = require 'parser.guide'
local vm       = require 'vm'
local await    = require 'await'

local sourceTypes = {
    'setfield',
    'setmethod',
    'setindex',
}

---@param source parser.object
---@return parser.object?
local function getTopFunctionOfIf(source)
    while true do
        if source.type == 'ifblock'
        or source.type == 'elseifblock'
        or source.type == 'elseblock'
        or source.type == 'function'
        or source.type == 'main' then
            return source
        end
        source = source.parent
    end
    return nil
end

---@async
return function (uri, callback)
    local state = files.getState(uri)
    if not state then
        return
    end

    if vm.isMetaFile(uri) then
        return
    end

    ---@async
    guide.eachSourceTypes(state.ast, sourceTypes, function (src)
        await.delay()
        local name = guide.getKeyName(src)
        if not name then
            return
        end
        local value = vm.getObjectValue(src)
        if not value or value.type ~= 'function' then
            return
        end
        local myTopBlock = getTopFunctionOfIf(src)
        local defs = vm.getDefs(src)
        for _, def in ipairs(defs) do
            if def == src then
                goto CONTINUE
            end
            if  def.type ~= 'setfield'
            and def.type ~= 'setmethod'
            and def.type ~= 'setindex' then
                goto CONTINUE
            end
            local defTopBlock = getTopFunctionOfIf(def)
            if uri == guide.getUri(def) and myTopBlock ~= defTopBlock then
                goto CONTINUE
            end
            local defValue = vm.getObjectValue(def)
            if not defValue or defValue.type ~= 'function' then
                goto CONTINUE
            end
            callback {
                start   = src.start,
                finish  = src.finish,
                related = {{
                    start  = def.start,
                    finish = def.finish,
                    uri    = guide.getUri(def),
                }},
                message = lang.script('DIAG_DUPLICATE_SET_FIELD', name),
            }
            ::CONTINUE::
        end
    end)
end