Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to find globals using regex #2629

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions doc/en-us/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions doc/pt-br/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions doc/zh-cn/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ Array<string>
[]
```

# diagnostics.globalsRegex

Find defined global variables using regex.

## type

```ts
Array<string>
```

## default

```jsonc
[]
```

# diagnostics.groupFileStatus

批量修改一个组中的文件状态。
Expand Down
16 changes: 16 additions & 0 deletions doc/zh-tw/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,22 @@ Array<string>
[]
```

# diagnostics.globalsRegex

Find defined global variables using regex.

## type

```ts
Array<string>
```

## default

```jsonc
[]
```

# diagnostics.groupFileStatus

批量修改一個組中的檔案狀態。
Expand Down
2 changes: 2 additions & 0 deletions locale/en-us/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions locale/pt-br/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions locale/zh-cn/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
[[
修改诊断等级。
Expand Down
2 changes: 2 additions & 0 deletions locale/zh-tw/setting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
[[
修改診斷等級。
Expand Down
1 change: 1 addition & 0 deletions script/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 18 additions & 0 deletions script/core/diagnostics/global-element.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions script/core/diagnostics/lowercase-global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
55 changes: 47 additions & 8 deletions script/vm/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down