summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/unbalanced-assignments.lua
blob: 35aebb453af647a72d7fc7e6d2c51dbd43fe8cd9 (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
local files  = require 'files'
local define = require 'proto.define'
local lang   = require 'language'
local guide  = require 'parser.guide'

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

    local last

    local function checkSet(source)
        if source.value then
            last = source
        else
            if not last then
                return
            end
            if  last.start       <= source.start
            and last.value.start >= source.finish then
                callback {
                    start   = source.start,
                    finish  = source.finish,
                    message = lang.script('DIAG_UNBALANCED_ASSIGNMENTS')
                }
            else
                last = nil
            end
        end
    end

    guide.eachSource(ast.ast, function (source)
        if source.type == 'local'
        or source.type == 'setlocal'
        or source.type == 'setglobal'
        or source.type == 'setfield'
        or source.type == 'setindex' then
            checkSet(source)
        end
    end)
end