summaryrefslogtreecommitdiff
path: root/server/src/core/diagnostics.lua
blob: 7bf4367536d53b13319de85d9065e42fc0d5e272 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
local lang = require 'language'
local config = require 'config'
local library = require 'core.library'
local buildGlobal = require 'vm.global'

local DiagnosticSeverity = {
    Error       = 1,
    Warning     = 2,
    Information = 3,
    Hint        = 4,
}

local mt = {}
mt.__index = mt

function mt:searchUnusedLocals(callback)
    self.vm:eachSource(function (source)
        local loc = source:bindLocal()
        if not loc then
            return
        end
        local name = loc:getName()
        if name == '_' or name == '_ENV' or name == '' then
            return
        end
        if source:action() ~= 'local' then
            return
        end
        if loc:get 'hide' then
            return
        end
        local used = loc:eachInfo(function (info)
            if info.type == 'get' then
                return true
            end
        end)
        if not used then
            callback(source.start, source.finish, name)
        end
    end)
end

function mt:searchUndefinedGlobal(callback)
    local definedGlobal = {}
    for name in pairs(config.config.diagnostics.globals) do
        definedGlobal[name] = true
    end
    local envValue = buildGlobal(self.vm.lsp)
    envValue:eachInfo(function (info)
        if info.type == 'set child' then
            local name = info[1]
            definedGlobal[name] = true
        end
    end)
    self.vm:eachSource(function (source)
        if not source:get 'global' then
            return
        end
        local name = source:getName()
        if name == '' then
            return
        end
        local parent = source:get 'parent'
        if not parent then
            return
        end
        if not parent:get 'ENV' and not source:get 'in index' then
            return
        end
        if definedGlobal[name] then
            return
        end
        if type(name) ~= 'string' then
            return
        end
        callback(source.start, source.finish, name)
    end)
end

function mt:searchUnusedLabel(callback)
    self.vm:eachSource(function (source)
        local label = source:bindLabel()
        if not label then
            return
        end
        if source:action() ~= 'set' then
            return
        end
        local used = label:eachInfo(function (info)
            if info.type == 'get' then
                return true
            end
        end)
        if not used then
            callback(source.start, source.finish, label:getName())
        end
    end)
end

local function isContainPos(obj, start, finish)
    if obj.start <= start and obj.finish >= finish then
        return true
    end
    return false
end

local function isInString(vm, start, finish)
    return vm:eachSource(function (source)
        if source.type == 'string' and isContainPos(source, start, finish) then
            return true
        end
    end)
end

function mt:searchSpaces(callback)
    local vm = self.vm
    local lines = self.lines
    for i = 1, #lines do
        local line = lines:line(i)

        if line:find '^[ \t]+$' then
            local start, finish = lines:range(i)
            if isInString(vm, start, finish) then
                goto NEXT_LINE
            end
            callback(start, finish, lang.script.DIAG_LINE_ONLY_SPACE)
            goto NEXT_LINE
        end

        local pos = line:find '[ \t]+$'
        if pos then
            local start, finish = lines:range(i)
            start = start + pos - 1
            if isInString(vm, start, finish) then
                goto NEXT_LINE
            end
            callback(start, finish, lang.script.DIAG_LINE_POST_SPACE)
            goto NEXT_LINE
        end

        ::NEXT_LINE::
    end
end

function mt:searchRedefinition(callback)
    local used = {}
    local uri = self.uri
    self.vm:eachSource(function (source)
        local loc = source:bindLocal()
        if not loc then
            return
        end
        local shadow = loc:shadow()
        if not shadow then
            return
        end
        if used[shadow] then
            return
        end
        used[shadow] = true
        if loc:get 'hide' then
            return
        end
        local name = loc:getName()
        if name == '_' or name == '_ENV' or name == '' then
            return
        end
        local related = {}
        for i = 1, #shadow do
            related[i] = {
                start  = shadow[i]:getSource().start,
                finish = shadow[i]:getSource().finish,
                uri    = uri,
            }
        end
        for i = 2, #shadow do
            callback(shadow[i]:getSource().start, shadow[i]:getSource().finish, name, related)
        end
    end)
end

function mt:searchNewLineCall(callback)
    local lines = self.lines
    self.vm:eachSource(function (source)
        if source.type ~= 'simple' then
            return
        end
        for i = 1, #source - 1 do
            local callSource = source[i]
            local funcSource = source[i-1]
            if callSource.type ~= 'call' then
                goto CONTINUE
            end
            local callLine = lines:rowcol(callSource.start)
            local funcLine = lines:rowcol(funcSource.finish)
            if callLine > funcLine then
                callback(callSource.start, callSource.finish)
            end
            :: CONTINUE ::
        end
    end)
end

function mt:searchRedundantParameters(callback)
    self.vm:eachSource(function (source)
        local args = source:bindCall()
        if not args then
            return
        end

        local value = source:findCallFunction()
        if not value then
            return
        end

        local func = value:getFunction()
        -- 参数中有 ... ,不用再检查了
        if func:hasDots() then
            return
        end
        local max = #func.args
        local passed = #args
        for i = max + 1, passed do
            local extra = args[i]
            callback(extra.start, extra.finish, max, passed)
        end
    end)
end

function mt:searchAmbiguity1(callback)
    self.vm:eachSource(function (source)
        if source.op ~= 'or' then
            return
        end
        local exp = source[2]
        if exp.op ~= '+' and exp.op ~= '-' then
            return
        end
        if exp[1][1] == 0 and exp[2].type == 'number' then
            callback(source.start, source.finish, exp.op, exp[2][1])
        end
    end)
end

function mt:searchLowercaseGlobal(callback)
    local definedGlobal = {}
    for name in pairs(config.config.diagnostics.globals) do
        definedGlobal[name] = true
    end
    for name in pairs(library.global) do
        definedGlobal[name] = true
    end
    local uri = self.vm.uri
    local envValue = self.vm.env:getValue()
    envValue:eachInfo(function (info, src)
        if info.type == 'set child' and src.uri == uri then
            local name = info[1]
            if definedGlobal[name] then
                return
            end
            if type(name) ~= 'string' then
                return
            end
            local first = name:match '%w'
            if not first then
                return
            end
            if first:match '%l' then
                callback(src.start, src.finish)
            end
        end
    end)
end

function mt:doDiagnostics(func, code, callback)
    if config.config.diagnostics.disable[code] then
        return
    end
    func(self, function (start, finish, ...)
        local data = callback(...)
        data.code   = code
        data.start  = start
        data.finish = finish
        self.datas[#self.datas+1] = data
    end)
    if coroutine.isyieldable() then
        if self.vm:isRemoved() then
            coroutine.yield('stop')
        else
            coroutine.yield()
        end
    end
end

function mt:searchUndefinedEnvChild(callback)
    self.vm:eachSource(function (source)
        if not source:get 'global' then
            return
        end
        local name = source:getName()
        if name == '' then
            return
        end
        if source:get 'in index' then
            return
        end
        local parent = source:get 'parent'
        if parent:get 'ENV' then
            return
        end
        local value = source:bindValue()
        if not value then
            return
        end
        if value:getSource() == source then
            callback(source.start, source.finish, name)
        end
        return
    end)
end

return function (vm, lines, uri)
    local session = setmetatable({
        vm = vm,
        lines = lines,
        uri = uri,
        datas = {},
    }, mt)

    -- 未使用的局部变量
    session:doDiagnostics(session.searchUnusedLocals, 'unused-local', function (key)
        return {
            level   = DiagnosticSeverity.Hint,
            message = lang.script('DIAG_UNUSED_LOCAL', key),
        }
    end)
    -- 读取未定义全局变量
    session:doDiagnostics(session.searchUndefinedGlobal, 'undefined-global', function (key)
        local message = lang.script('DIAG_UNDEF_GLOBAL', key)
        local otherVersion = library.other[key]
        local customLib = library.custom[key]
        if otherVersion then
            message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_VERSION', table.concat(otherVersion, '/'), config.config.runtime.version))
        end
        if customLib then
            message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_CUSTOM', table.concat(customLib, '/')))
        end
        return {
            level   = DiagnosticSeverity.Warning,
            message = message,
        }
    end)
    -- 未使用的Label
    session:doDiagnostics(session.searchUnusedLabel, 'unused-label', function (key)
        return {
            level   =DiagnosticSeverity.Hint,
            message = lang.script('DIAG_UNUSED_LABEL', key)
        }
    end)
    -- 只有空格与制表符的行,以及后置空格
    session:doDiagnostics(session.searchSpaces, 'trailing-space', function (message)
        return {
            level   = DiagnosticSeverity.Hint,
            message = message,
        }
    end)
    -- 重定义局部变量
    session:doDiagnostics(session.searchRedefinition, 'redefined-local', function (key, related)
        return {
            level   = DiagnosticSeverity.Hint,
            message = lang.script('DIAG_REDEFINED_LOCAL', key),
            related = related,
        }
    end)
    -- 以括号开始的一行(可能被误解析为了上一行的call)
    session:doDiagnostics(session.searchNewLineCall, 'newline-call', function ()
        return {
            level   = DiagnosticSeverity.Information,
            message = lang.script.DIAG_PREVIOUS_CALL,
        }
    end)
    -- 调用函数时的参数数量是否超过函数的接收数量
    session:doDiagnostics(session.searchRedundantParameters, 'remainder-parameters', function (max, passed)
        return {
            level   = DiagnosticSeverity.Information,
            message = lang.script('DIAG_OVER_MAX_ARGS', max, passed),
        }
    end)
    -- x or 0 + 1
    session:doDiagnostics(session.searchAmbiguity1, 'ambiguity-1', function (op, num)
        return {
            level   = DiagnosticSeverity.Warning,
            message = lang.script('DIAG_AMBIGUITY_1', op, num),
        }
    end)
    -- 不允许定义首字母小写的全局变量(很可能是拼错或者漏删)
    session:doDiagnostics(session.searchLowercaseGlobal, 'lowercase-global', function ()
        return {
            level   = DiagnosticSeverity.Information,
            message = lang.script.DIAG_LOWERCASE_GLOBAL,
        }
    end)
    -- 未定义的变量(重载了 `_ENV`)
    session:doDiagnostics(session.searchUndefinedEnvChild, 'undefined-env-child', function (key)
        if vm.envType == '_ENV' then
            return {
                level   = DiagnosticSeverity.Information,
                message = lang.script('DIAG_UNDEF_ENV_CHILD', key),
            }
        else
            return {
                level   = DiagnosticSeverity.Information,
                message = lang.script('DIAG_UNDEF_FENV_CHILD', key),
            }
        end
    end)
    return session.datas
end