summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/constant/DiagnosticDefaultSeverity.lua1
-rw-r--r--server/src/core/diagnostics.lua37
2 files changed, 38 insertions, 0 deletions
diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua
index 3cd67528..cc26cab2 100644
--- a/server/src/constant/DiagnosticDefaultSeverity.lua
+++ b/server/src/constant/DiagnosticDefaultSeverity.lua
@@ -2,6 +2,7 @@ return {
['unused-local'] = 'Hint',
['unused-function'] = 'Hint',
['undefined-global'] = 'Warning',
+ ['global-in-nil-env'] = 'Warning',
['unused-label'] = 'Hint',
['unused-vararg'] = 'Hint',
['trailing-space'] = 'Hint',
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index 509fe48c..0e0d85ba 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -553,6 +553,29 @@ function mt:searchUndefinedEnvChild(callback)
end)
end
+function mt:searchGlobalInNilEnv(callback)
+ self.vm:eachSource(function (source)
+ if not source:get 'global' then
+ return
+ end
+ local name = source:getName()
+ if name == '' then
+ return
+ end
+ local parentSource = source:get 'parent' :getSource()
+ if parentSource.type == 'nil' then
+ callback(source.start, source.finish, {
+ {
+ start = parentSource.start,
+ finish = parentSource.finish,
+ uri = self.uri,
+ }
+ })
+ end
+ return
+ end)
+end
+
function mt:checkEmmyClass(source, callback)
local class = source:get 'emmy.class'
if not class then
@@ -919,6 +942,20 @@ return function (vm, lines, uri)
}
end
end)
+ -- 全局变量不可用(置空了 `_ENV`)
+ session:doDiagnostics(session.searchGlobalInNilEnv, 'global-in-nil-env', function (related)
+ if vm.envType == '_ENV' then
+ return {
+ message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
+ related = related,
+ }
+ else
+ return {
+ message = lang.script.DIAG_GLOBAL_IN_NIL_FENV,
+ related = related,
+ }
+ end
+ end)
-- 构建表时重复定义field
session:doDiagnostics(session.searchDuplicateIndex, 'duplicate-index', function (key, related, type)
if type == 'unused' then