diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua index 18840e94fa956eefc0e6c8c7a654442bd5b33dfc..6fd7abdc539b7d35bbfee02232bed603a6c9a110 100644 --- a/locale/en-us/setting.lua +++ b/locale/en-us/setting.lua @@ -48,6 +48,8 @@ config.diagnostics.disable = "Disabled diagnostic (Use code in hover brackets)." config.diagnostics.globals = "Defined global variables." +config.diagnostics.ignoredConstantPrefixes = +"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables." config.diagnostics.severity = [[ Modify the diagnostic severity. diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua index 75904872573e7cd44c396ccc2b40cc6749e656a2..d67464b37565b6454afb89470688cc5bdec42c30 100644 --- a/locale/pt-br/setting.lua +++ b/locale/pt-br/setting.lua @@ -48,6 +48,8 @@ config.diagnostics.disable = -- TODO: need translate! "Disabled diagnostic (Use code in hover brackets)." config.diagnostics.globals = -- TODO: need translate! "Defined global variables." +config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate! +"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables." config.diagnostics.severity = -- TODO: need translate! [[ Modify the diagnostic severity. diff --git a/locale/zh-cn/setting.lua b/locale/zh-cn/setting.lua index d488ac3f16c210d7a55951888e477c3f6c732b90..89de87661f9c50ee073eb3cbd6e1406746adabaf 100644 --- a/locale/zh-cn/setting.lua +++ b/locale/zh-cn/setting.lua @@ -48,6 +48,8 @@ config.diagnostics.disable = "禁用的诊断(使用浮框括号内的代码)。" config.diagnostics.globals = "已定义的全局变量。" +config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate! +"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables." config.diagnostics.severity = [[ 修改诊断等级。 diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua index 8c74668da5e3e7d161b01190dc3400aed6412d62..f964afa627aa0d834a1ea21b4feff35cdb41d19c 100644 --- a/locale/zh-tw/setting.lua +++ b/locale/zh-tw/setting.lua @@ -48,6 +48,8 @@ config.diagnostics.disable = "停用的診斷(使用浮框括號內的程式碼)。" config.diagnostics.globals = "已定義的全域變數。" +config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate! +"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables." config.diagnostics.severity = [[ 修改診斷等級。 diff --git a/script/config/template.lua b/script/config/template.lua index 3d2a8d35d2400203399883db6876d52862081700..059dcab9614faa0f26ea6209b4c98b71dd7500fa 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -242,6 +242,15 @@ local template = { >> util.deepCopy(define.BuiltIn), ['Lua.diagnostics.enable'] = Type.Boolean >> true, ['Lua.diagnostics.globals'] = Type.Array(Type.String), + ['Lua.diagnostics.ignoredConstantPrefixes'] = Type.Array(Type.String) >> { + 'MT_', + 'S_', + 'sfx_', + 'SKINCOLOR_', + 'SPR_', + 'SPR2_', + 'TOL_', + }, ['Lua.diagnostics.disable'] = Type.Array(Type.String << util.getTableKeys(diag.getDiagAndErrNameMap(), true)), ['Lua.diagnostics.severity'] = Type.Hash( Type.String << util.getTableKeys(define.DiagnosticDefaultNeededFileStatus, true), diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index 179c92043a0bcf96d35fa594dc7f4d994d44a814..4fb8117badfe781d88ba90cec6c3499b18203771 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -13,6 +13,15 @@ local requireLike = { ['load'] = true, } +local function isNameIgnored(name, ignoredPrefixes) + for i = 1, #ignoredPrefixes do + if name:find(ignoredPrefixes[i], nil, false) == 1 then + return true + end + end + return false +end + ---@async return function (uri, callback) local state = files.getState(uri) @@ -22,6 +31,7 @@ return function (uri, callback) local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') + local ignoredPrefixes = config.get(uri, 'Lua.diagnostics.ignoredConstantPrefixes') local cache = {} -- 遍历全局变量,检查所有没有 set 模式的全局变量 @@ -36,6 +46,9 @@ return function (uri, callback) if rspecial[key] then return end + if isNameIgnored(key, ignoredPrefixes) then + return + end local node = src.node if node.tag ~= '_ENV' then return