summaryrefslogtreecommitdiff
path: root/server-beta/src/core/diagnostics/global-in-nil-env.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server-beta/src/core/diagnostics/global-in-nil-env.lua')
-rw-r--r--server-beta/src/core/diagnostics/global-in-nil-env.lua57
1 files changed, 55 insertions, 2 deletions
diff --git a/server-beta/src/core/diagnostics/global-in-nil-env.lua b/server-beta/src/core/diagnostics/global-in-nil-env.lua
index b3d19c21..a19a341f 100644
--- a/server-beta/src/core/diagnostics/global-in-nil-env.lua
+++ b/server-beta/src/core/diagnostics/global-in-nil-env.lua
@@ -1,3 +1,56 @@
-return function ()
-
+local files = require 'files'
+local guide = require 'parser.guide'
+local searcher = require 'searcher'
+local lang = require 'language'
+
+-- TODO: 检查路径是否可达
+local function mayRun(path)
+ return true
+end
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+ local root = guide.getRoot(ast.ast)
+ local env = guide.getENV(root)
+
+ local nilDefs = {}
+ if not env.ref then
+ return
+ end
+ for _, ref in ipairs(env.ref) do
+ if ref.type == 'setlocal' then
+ if ref.value and ref.value.type == 'nil' then
+ nilDefs[#nilDefs+1] = ref
+ end
+ end
+ end
+
+ if #nilDefs == 0 then
+ return
+ end
+
+ local function check(source)
+ local node = source.node
+ if node.tag == '_ENV' then
+ local ok
+ for _, nilDef in ipairs(nilDefs) do
+ local mode, pathA = guide.getPath(nilDef, source)
+ if mode == 'before'
+ and mayRun(pathA) then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ uri = uri,
+ message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
+ }
+ end
+ end
+ end
+ end
+
+ guide.eachSourceType(ast.ast, 'getglobal', check)
+ guide.eachSourceType(ast.ast, 'setglobal', check)
end