summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/global-in-nil-env.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-07-11 20:32:03 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-07-11 20:32:03 +0800
commit3820962ca2d23d525bc8417c8088e7eeb667cba1 (patch)
tree1f2fc76ce1344ee9e48935f94fe5b44182325453 /script/core/diagnostics/global-in-nil-env.lua
parent6d040fd8da9f77c92a6359657c2d845c3b62735d (diff)
downloadlua-language-server-3820962ca2d23d525bc8417c8088e7eeb667cba1.zip
resolve #1209
treat `_ENV = {}` as `local _ENV = {}` `local _ENV = nil` will disable all globals `local _ENV = {}` will enable all globals `local _ENV = {} ---@type mathlib` will open globals in mathlib
Diffstat (limited to 'script/core/diagnostics/global-in-nil-env.lua')
-rw-r--r--script/core/diagnostics/global-in-nil-env.lua68
1 files changed, 19 insertions, 49 deletions
diff --git a/script/core/diagnostics/global-in-nil-env.lua b/script/core/diagnostics/global-in-nil-env.lua
index 334fd81a..e154080c 100644
--- a/script/core/diagnostics/global-in-nil-env.lua
+++ b/script/core/diagnostics/global-in-nil-env.lua
@@ -2,65 +2,35 @@ local files = require 'files'
local guide = require 'parser.guide'
local lang = require 'language'
--- TODO: 检查路径是否可达
-local function mayRun(path)
- return true
-end
-
return function (uri, callback)
- local ast = files.getState(uri)
- if not ast then
- return
- end
- local root = guide.getRoot(ast.ast)
- local env = guide.getENV(root)
-
- local nilDefs = {}
- if not env or 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
+ local state = files.getState(uri)
+ if not state 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
- ok = nilDef
- break
- end
- end
- if ok then
- callback {
- start = source.start,
- finish = source.finish,
- uri = uri,
- message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
- related = {
- {
- start = ok.start,
- finish = ok.finish,
- uri = uri,
- }
+ return
+ end
+
+ if not node.value or node.value.type == 'nil' then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ uri = uri,
+ message = lang.script.DIAG_GLOBAL_IN_NIL_ENV,
+ related = {
+ {
+ start = node.start,
+ finish = node.finish,
+ uri = uri,
}
}
- end
+ }
end
end
- guide.eachSourceType(ast.ast, 'getglobal', check)
- guide.eachSourceType(ast.ast, 'setglobal', check)
+ guide.eachSourceType(state.ast, 'getglobal', check)
+ guide.eachSourceType(state.ast, 'setglobal', check)
end