summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/newfield-call.lua
blob: 2cbc13eefcb4d3bc9dee1e8132a3a689acb3b658 (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
local files = require 'files'
local guide = require 'parser.guide'
local lang  = require 'language'

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

    local lines = files.getLines(uri)
    local text  = files.getText(uri)

    guide.eachSourceType(ast.ast, 'table', function (source)
        for i = 1, #source do
            local field = source[i]
            if field.type == 'call' then
                local func = field.node
                local args = field.args
                if args then
                    local funcLine = guide.positionOf(lines, func.finish)
                    local argsLine = guide.positionOf(lines, args.start)
                    if argsLine > funcLine then
                        callback {
                            start   = field.start,
                            finish  = field.finish,
                            message = lang.script('DIAG_PREFIELD_CALL'
                                , text:sub(func.start, func.finish)
                                , text:sub(args.start, args.finish)
                            )
                        }
                    end
                end
            end
        end
    end)
end