From 6376ac1a2d98280304221243b00a1bc8cac74dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 4 Nov 2019 19:56:00 +0800 Subject: =?UTF-8?q?=E8=AF=8A=E6=96=AD=E6=9C=AA=E5=AE=9A=E4=B9=89=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/diagnostics/undefined-global.lua | 41 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'server-beta/src/core') diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua index db7af133..888064dc 100644 --- a/server-beta/src/core/diagnostics/undefined-global.lua +++ b/server-beta/src/core/diagnostics/undefined-global.lua @@ -3,6 +3,8 @@ local guide = require 'parser.guide' local searcher = require 'searcher' local define = require 'proto.define' local lang = require 'language' +local library = require 'library' +local config = require 'config' return function (uri, callback) local ast = files.getAst(uri) @@ -10,26 +12,53 @@ return function (uri, callback) return end + -- 先遍历一次所有该文件中的全局变量 + -- 如果变量有 set 行为,则做标记 + -- 然后再遍历一次,对所有的行为打上标记 local hasSet = {} searcher.eachGlobal(ast.ast, function (info) - local key = info.key - if hasSet[key] ~= nil then + if hasSet[info.source] ~= nil then return end - hasSet[key] = false + local mark = false searcher.eachRef(info.source, function (info) if info.mode == 'set' then - hasSet[key] = true + mark = true end end) + searcher.eachRef(info.source, function (info) + hasSet[info.source] = mark + end) end) + -- 然后再遍历一次,检查所有标记为假的全局变量 searcher.eachGlobal(ast.ast, function (info) local source = info.source - if info.mode == 'get' and not hasSet[info.key] then + local key = info.key + local skey = key:match '^s|(.+)$' + if not skey then + return + end + if library.global[skey] then + return + end + if config.config.diagnostics.globals[skey] then + return + end + if info.mode == 'get' and not hasSet[source] then + local message + local otherVersion = library.other[key] + local customVersion = library.custom[key] + if otherVersion then + message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_VERSION', table.concat(otherVersion, '/'), config.config.runtime.version)) + elseif customVersion then + message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_CUSTOM', table.concat(customVersion, '/'))) + else + message = lang.script('DIAG_UNDEF_GLOBAL', info.key) + end callback { start = source.start, finish = source.finish, - message = lang.script('DIAG_UNDEF_GLOBAL', info.key), + message = message, } end end) -- cgit v1.2.3