summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/redundant-parameter.lua
blob: 667f9c61f52797dd4ecff9b7c08544be5f282c1a (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
local files  = require 'files'
local guide  = require 'parser.guide'
local vm     = require 'vm'
local lang   = require 'language'
local await  = require 'await'

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

    ---@async
    guide.eachSourceType(state.ast, 'call', function (source)
        await.delay()
        local callArgs = vm.countList(source.args)
        if callArgs == 0 then
            return
        end

        local funcNode = vm.compileNode(source.node)
        local _, funcArgs = vm.countParamsOfNode(funcNode)

        if callArgs <= funcArgs then
            return
        end
        if callArgs == 1 and source.node.type == 'getmethod' then
            return
        end
        if funcArgs + 1 > #source.args then
            local lastArg = source.args[#source.args]
            if lastArg.type == 'call' and funcArgs > 0 then
                -- 如果函数接收至少一个参数,那么调用方最后一个参数是函数调用
                -- 导致的参数数量太多可以忽略。
                -- 如果函数不接收任何参数,那么任何参数都是错误的。
                return
            end
            callback {
                start   = lastArg.start,
                finish  = lastArg.finish,
                message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, callArgs)
            }
        else
            for i = funcArgs + 1, #source.args do
                local arg = source.args[i]
                callback {
                    start   = arg.start,
                    finish  = arg.finish,
                    message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, callArgs)
                }
            end
        end
    end)

    ---@async
    guide.eachSourceType(state.ast, 'function', function (source)
        await.delay()
        if not source.args then
            return
        end
        local _, funcArgs = vm.countParamsOfSource(source)
        local myArgs = #source.args
        for i = funcArgs + 1, myArgs do
            local arg = source.args[i]
            callback {
                start   = arg.start,
                finish  = arg.finish,
                message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, myArgs),
            }
        end
    end)
end