diff options
-rw-r--r-- | doc/en-us/config.md | 16 | ||||
-rw-r--r-- | doc/pt-br/config.md | 16 | ||||
-rw-r--r-- | doc/zh-cn/config.md | 16 | ||||
-rw-r--r-- | doc/zh-tw/config.md | 16 | ||||
-rw-r--r-- | locale/en-us/setting.lua | 2 | ||||
-rw-r--r-- | locale/pt-br/setting.lua | 2 | ||||
-rw-r--r-- | locale/zh-cn/setting.lua | 2 | ||||
-rw-r--r-- | locale/zh-tw/setting.lua | 2 | ||||
-rw-r--r-- | script/config/template.lua | 1 | ||||
-rw-r--r-- | script/core/diagnostics/global-element.lua | 18 | ||||
-rw-r--r-- | script/core/diagnostics/lowercase-global.lua | 18 | ||||
-rw-r--r-- | script/vm/global.lua | 55 |
12 files changed, 156 insertions, 8 deletions
diff --git a/doc/en-us/config.md b/doc/en-us/config.md index f57d4d64..8dd8c059 100644 --- a/doc/en-us/config.md +++ b/doc/en-us/config.md @@ -406,6 +406,22 @@ Array<string> [] ``` +# diagnostics.globalsRegex + +Find defined global variables using regex. + +## type + +```ts +Array<string> +``` + +## default + +```jsonc +[] +``` + # diagnostics.groupFileStatus Modify the diagnostic needed file status in a group. diff --git a/doc/pt-br/config.md b/doc/pt-br/config.md index 7add2c98..7ee6c2ea 100644 --- a/doc/pt-br/config.md +++ b/doc/pt-br/config.md @@ -406,6 +406,22 @@ Array<string> [] ``` +# diagnostics.globalsRegex + +Find defined global variables using regex. + +## type + +```ts +Array<string> +``` + +## default + +```jsonc +[] +``` + # diagnostics.groupFileStatus Modify the diagnostic needed file status in a group. diff --git a/doc/zh-cn/config.md b/doc/zh-cn/config.md index ebb8325f..e7292b92 100644 --- a/doc/zh-cn/config.md +++ b/doc/zh-cn/config.md @@ -406,6 +406,22 @@ Array<string> [] ``` +# diagnostics.globalsRegex + +Find defined global variables using regex. + +## type + +```ts +Array<string> +``` + +## default + +```jsonc +[] +``` + # diagnostics.groupFileStatus 批量修改一个组中的文件状态。 diff --git a/doc/zh-tw/config.md b/doc/zh-tw/config.md index 8b01d78c..5e491a51 100644 --- a/doc/zh-tw/config.md +++ b/doc/zh-tw/config.md @@ -406,6 +406,22 @@ Array<string> [] ``` +# diagnostics.globalsRegex + +Find defined global variables using regex. + +## type + +```ts +Array<string> +``` + +## default + +```jsonc +[] +``` + # diagnostics.groupFileStatus 批量修改一個組中的檔案狀態。 diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua index 9ef46b86..2352a9e4 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.globalsRegex = +"Find defined global variables using regex." config.diagnostics.severity = [[ Modify the diagnostic severity. diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua index 6ececcd3..a5bc8e06 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.globalsRegex = -- TODO: need translate! +"Find defined global variables using regex." 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 78e7fb68..7aba522e 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.globalsRegex = -- TODO: need translate! +"Find defined global variables using regex." config.diagnostics.severity = [[ 修改诊断等级。 diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua index 2b43e954..aeabb63e 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.globalsRegex = -- TODO: need translate! +"Find defined global variables using regex." config.diagnostics.severity = [[ 修改診斷等級。 diff --git a/script/config/template.lua b/script/config/template.lua index 49907419..e74a9f9c 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -242,6 +242,7 @@ local template = { >> util.deepCopy(define.BuiltIn), ['Lua.diagnostics.enable'] = Type.Boolean >> true, ['Lua.diagnostics.globals'] = Type.Array(Type.String), + ['Lua.diagnostics.globalsRegex'] = Type.Array(Type.String), ['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/global-element.lua b/script/core/diagnostics/global-element.lua index e9dd46ce..a30ebbc6 100644 --- a/script/core/diagnostics/global-element.lua +++ b/script/core/diagnostics/global-element.lua @@ -17,6 +17,20 @@ local function isDocClass(source) return false end +local function isGlobalRegex(name, definedGlobalRegex) + if not definedGlobalRegex then + return false + end + + for _, pattern in ipairs(definedGlobalRegex) do + if name:match(pattern) then + return true + end + end + + return false +end + -- If global elements are discouraged by coding convention, this diagnostic helps with reminding about that -- Exceptions may be added to Lua.diagnostics.globals return function (uri, callback) @@ -26,6 +40,7 @@ return function (uri, callback) end local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) + local definedGlobalRegex = config.get(uri, 'Lua.diagnostics.globalsRegex') guide.eachSourceType(ast.ast, 'setglobal', function (source) local name = guide.getKeyName(source) @@ -36,6 +51,9 @@ return function (uri, callback) if isDocClass(source) then return end + if isGlobalRegex(name, definedGlobalRegex) then + return + end if definedGlobal[name] == nil then definedGlobal[name] = false local global = vm.getGlobal('variable', name) diff --git a/script/core/diagnostics/lowercase-global.lua b/script/core/diagnostics/lowercase-global.lua index 68bec234..c7e9294d 100644 --- a/script/core/diagnostics/lowercase-global.lua +++ b/script/core/diagnostics/lowercase-global.lua @@ -17,6 +17,20 @@ local function isDocClass(source) return false end +local function isGlobalRegex(name, definedGlobalRegex) + if not definedGlobalRegex then + return false + end + + for _, pattern in ipairs(definedGlobalRegex) do + if name:match(pattern) then + return true + end + end + + return false +end + -- 不允许定义首字母小写的全局变量(很可能是拼错或者漏删) return function (uri, callback) local ast = files.getState(uri) @@ -25,6 +39,7 @@ return function (uri, callback) end local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) + local definedGlobalRegex = config.get(uri, 'Lua.diagnostics.globalsRegex') guide.eachSourceType(ast.ast, 'setglobal', function (source) local name = guide.getKeyName(source) @@ -42,6 +57,9 @@ return function (uri, callback) if isDocClass(source) then return end + if isGlobalRegex(name, definedGlobalRegex) then + return + end if definedGlobal[name] == nil then definedGlobal[name] = false local global = vm.getGlobal('variable', name) diff --git a/script/vm/global.lua b/script/vm/global.lua index e830f6d8..aa987cf4 100644 --- a/script/vm/global.lua +++ b/script/vm/global.lua @@ -539,21 +539,60 @@ function vm.hasGlobalSets(suri, cate, name) return true end +---@param uri uri +---@param key string +---@return boolean +local function checkIsGlobalRegex(uri, key) + local dglobalsregex = config.get(uri, 'Lua.diagnostics.globalsRegex') + if not dglobalsregex then + return false + end + + for _, pattern in ipairs(dglobalsregex) do + if key:match(pattern) then + return true + end + end + + return false +end + ---@param src parser.object local function checkIsUndefinedGlobal(src) + if src.type ~= 'getglobal' then + return false + end + local key = src[1] + if not key then + return false + end + + local node = src.node + if node.tag ~= '_ENV' then + return false + end local uri = guide.getUri(src) - local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') + if rspecial[key] then + return false + end - local node = src.node - return src.type == 'getglobal' and key and not ( - dglobals[key] or - rspecial[key] or - node.tag ~= '_ENV' or - vm.hasGlobalSets(uri, 'variable', key) - ) + if vm.hasGlobalSets(uri, 'variable', key) then + return false + end + + local dglobals = config.get(uri, 'Lua.diagnostics.globals') + if util.arrayHas(dglobals, key) then + return false + end + + if checkIsGlobalRegex(uri, key) then + return false + end + + return true end ---@param src parser.object |