Skip to content
17 changes: 17 additions & 0 deletions doc/en-us/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Array<string>
* ``"err-nonstandard-symbol"``
* ``"err-then-as-do"``
* ``"exp-in-action"``
* ``"global-element"``
* ``"global-in-nil-env"``
* ``"index-in-func-name"``
* ``"invisible"``
Expand Down Expand Up @@ -468,6 +469,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "None",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -587,6 +592,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "Fallback",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -775,6 +784,10 @@ object<string, string>
*/
"empty-block": "Opened",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "None",
/*
Enable cannot use global variables ( `_ENV` is set to `nil`) diagnostics.
*/
"global-in-nil-env": "Any",
Expand Down Expand Up @@ -1023,6 +1036,10 @@ object<string, string>
*/
"empty-block": "Hint",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "Warning",
/*
Enable cannot use global variables ( `_ENV` is set to `nil`) diagnostics.
*/
"global-in-nil-env": "Warning",
Expand Down
17 changes: 17 additions & 0 deletions doc/pt-br/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Array<string>
* ``"err-nonstandard-symbol"``
* ``"err-then-as-do"``
* ``"exp-in-action"``
* ``"global-element"``
* ``"global-in-nil-env"``
* ``"index-in-func-name"``
* ``"invisible"``
Expand Down Expand Up @@ -468,6 +469,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "None",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -587,6 +592,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "Fallback",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -775,6 +784,10 @@ object<string, string>
*/
"empty-block": "Opened",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "None",
/*
不能使用全局变量( `_ENV` 被设置为了 `nil`)
*/
"global-in-nil-env": "Any",
Expand Down Expand Up @@ -1023,6 +1036,10 @@ object<string, string>
*/
"empty-block": "Hint",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "Warning",
/*
不能使用全局变量( `_ENV` 被设置为了 `nil`)
*/
"global-in-nil-env": "Warning",
Expand Down
17 changes: 17 additions & 0 deletions doc/zh-cn/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Array<string>
* ``"err-nonstandard-symbol"``
* ``"err-then-as-do"``
* ``"exp-in-action"``
* ``"global-element"``
* ``"global-in-nil-env"``
* ``"index-in-func-name"``
* ``"invisible"``
Expand Down Expand Up @@ -468,6 +469,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "None",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -587,6 +592,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "Fallback",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -775,6 +784,10 @@ object<string, string>
*/
"empty-block": "Opened",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "None",
/*
不能使用全局变量( `_ENV` 被设置为了 `nil`)
*/
"global-in-nil-env": "Any",
Expand Down Expand Up @@ -1022,6 +1035,10 @@ object<string, string>
*/
"empty-block": "Hint",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "Warning",
/*
不能使用全局变量( `_ENV` 被设置为了 `nil`)
*/
"global-in-nil-env": "Warning",
Expand Down
17 changes: 17 additions & 0 deletions doc/zh-tw/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Array<string>
* ``"err-nonstandard-symbol"``
* ``"err-then-as-do"``
* ``"exp-in-action"``
* ``"global-element"``
* ``"global-in-nil-env"``
* ``"index-in-func-name"``
* ``"invisible"``
Expand Down Expand Up @@ -468,6 +469,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "None",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -587,6 +592,10 @@ object<string, string>
*/
"strict": "Fallback",
/*
* global-element
*/
"strict-convention": "Fallback",
/*
* no-unknown
*/
"strong": "Fallback",
Expand Down Expand Up @@ -775,6 +784,10 @@ object<string, string>
*/
"empty-block": "Opened",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "None",
/*
不能使用全域變數( `_ENV` 被設定為 `nil`)
*/
"global-in-nil-env": "Any",
Expand Down Expand Up @@ -1022,6 +1035,10 @@ object<string, string>
*/
"empty-block": "Hint",
/*
Enable diagnostics to warn about global elements.
*/
"global-element": "Warning",
/*
不能使用全域變數( `_ENV` 被設定為 `nil`)
*/
"global-in-nil-env": "Warning",
Expand Down
2 changes: 2 additions & 0 deletions locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ DIAG_INVISIBLE_PROTECTED =
'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.'
DIAG_INVISIBLE_PACKAGE =
'Field `{field}` can only be accessed in same file `{uri}`.'
DIAG_GLOBAL_ELEMENT =
'Element is global.'

MWS_NOT_SUPPORT =
'{} does not support multi workspace for now, I may need to restart to support the new workspace ...'
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 @@ -415,6 +415,8 @@ config.diagnostics['unknown-operator'] =
'Enable diagnostics for unknown operators.'
config.diagnostics['unreachable-code'] =
'Enable diagnostics for unreachable code.'
config.diagnostics['global-element'] =
'Enable diagnostics to warn about global elements.'
config.typeFormat.config =
'Configures the formatting behavior while typing Lua code.'
config.typeFormat.config.auto_complete_end =
Expand Down
2 changes: 2 additions & 0 deletions locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ DIAG_INVISIBLE_PROTECTED = -- TODO: need translate!
'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.'
DIAG_INVISIBLE_PACKAGE = -- TODO: need translate!
'Field `{field}` can only be accessed in same file `{uri}`.'
DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
'Element is global.'

MWS_NOT_SUPPORT =
'{} não é suportado múltiplos espaços de trabalho por enquanto, posso precisar reiniciar para estabelecer um novo espaço de trabalho ...'
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 @@ -415,6 +415,8 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate!
'Enable diagnostics for unknown operators.'
config.diagnostics['unreachable-code'] = -- TODO: need translate!
'Enable diagnostics for unreachable code.'
config.diagnostics['global-element'] = -- TODO: need translate!
'Enable diagnostics to warn about global elements.'
config.typeFormat.config = -- TODO: need translate!
'Configures the formatting behavior while typing Lua code.'
config.typeFormat.config.auto_complete_end = -- TODO: need translate!
Expand Down
2 changes: 2 additions & 0 deletions locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ DIAG_INVISIBLE_PROTECTED =
'字段 `{field}` 受到保护,只能在 `{class}` 类极其子类中才能访问。'
DIAG_INVISIBLE_PACKAGE =
'字段 `{field}` 只能在相同的文件 `{uri}` 中才能访问。'
DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
'Element is global.'

MWS_NOT_SUPPORT =
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'
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 @@ -414,6 +414,8 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate!
'Enable diagnostics for unknown operators.'
config.diagnostics['unreachable-code'] = -- TODO: need translate!
'Enable diagnostics for unreachable code.'
config.diagnostics['global-element'] = -- TODO: need translate!
'Enable diagnostics to warn about global elements.'
config.typeFormat.config = -- TODO: need translate!
'Configures the formatting behavior while typing Lua code.'
config.typeFormat.config.auto_complete_end = -- TODO: need translate!
Expand Down
2 changes: 2 additions & 0 deletions locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ DIAG_INVISIBLE_PROTECTED = -- TODO: need translate!
'Field `{field}` is protected, it can only be accessed in class `{class}` and its subclasses.'
DIAG_INVISIBLE_PACKAGE = -- TODO: need translate!
'Field `{field}` can only be accessed in same file `{uri}`.'
DIAG_GLOBAL_ELEMENT = -- TODO: need translate!
'Element is global.'

MWS_NOT_SUPPORT =
'{} 目前還不支援多工作目錄,我可能需要重新啟動才能支援新的工作目錄...'
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 @@ -414,6 +414,8 @@ config.diagnostics['unknown-operator'] = -- TODO: need translate!
'Enable diagnostics for unknown operators.'
config.diagnostics['unreachable-code'] = -- TODO: need translate!
'Enable diagnostics for unreachable code.'
config.diagnostics['global-element'] = -- TODO: need translate!
'Enable diagnostics to warn about global elements.'
config.typeFormat.config = -- TODO: need translate!
'Configures the formatting behavior while typing Lua code.'
config.typeFormat.config.auto_complete_end = -- TODO: need translate!
Expand Down
57 changes: 57 additions & 0 deletions script/core/diagnostics/global-element.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local files = require 'files'
local guide = require 'parser.guide'
local lang = require 'language'
local config = require 'config'
local vm = require 'vm'
local util = require 'utility'

local function isDocClass(source)
if not source.bindDocs then
return false
end
for _, doc in ipairs(source.bindDocs) do
if doc.type == 'doc.class' 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)
local ast = files.getState(uri)
if not ast then
return
end

local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))

guide.eachSourceType(ast.ast, 'setglobal', function (source)
local name = guide.getKeyName(source)
if not name or definedGlobal[name] then
return
end
-- If the assignment is marked as doc.class, then it is considered allowed
if isDocClass(source) then
return
end
if definedGlobal[name] == nil then
definedGlobal[name] = false
local global = vm.getGlobal('variable', name)
if global then
for _, set in ipairs(global:getSets(uri)) do
if vm.isMetaFile(guide.getUri(set)) then
definedGlobal[name] = true
return
end
end
end
end
callback {
start = source.start,
finish = source.finish,
message = lang.script.DIAG_GLOBAL_ELEMENT,
}
end)
end
8 changes: 8 additions & 0 deletions script/proto/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ m.register {
status = 'Any',
}

m.register {
'global-element',
} {
group = 'strict-conventions',
severity = 'Warning',
status = 'None'
}

m.register {
'duplicate-index',
} {
Expand Down
Loading