summaryrefslogtreecommitdiff
path: root/script/core
diff options
context:
space:
mode:
authorSewbacca <sebastian.kalus@kolabnow.com>2023-07-17 14:34:49 +0200
committerSewbacca <sebastian.kalus@kolabnow.com>2023-07-17 14:34:49 +0200
commit393f3e844d5164116de8bd739fc06d76bd03439f (patch)
tree2e1b514bdc38885623781089fba3ef327c76f39a /script/core
parent62068c0709d2fd131607940852b0d938b43794b4 (diff)
downloadlua-language-server-393f3e844d5164116de8bd739fc06d76bd03439f.zip
Extracted undefined global check
Diffstat (limited to 'script/core')
-rw-r--r--script/core/code-action.lua17
-rw-r--r--script/core/diagnostics/undefined-global.lua47
2 files changed, 18 insertions, 46 deletions
diff --git a/script/core/code-action.lua b/script/core/code-action.lua
index b37b478e..7f59f493 100644
--- a/script/core/code-action.lua
+++ b/script/core/code-action.lua
@@ -8,6 +8,7 @@ local autoreq = require 'core.completion.auto-require'
local rpath = require 'workspace.require-path'
local furi = require 'file-uri'
local undefined = require 'core.diagnostics.undefined-global'
+local vm = require 'vm'
---@param uri uri
---@param row integer
@@ -695,14 +696,7 @@ local function checkMissingRequire(results, uri, start, finish)
return
end
- local potentialGlobals = {}
- guide.eachSourceBetween(state.ast, start, finish, function (source)
- if source.type == 'getglobal' then
- potentialGlobals[#potentialGlobals+1] = { name = source[1], endpos = source.finish }
- end
- end)
-
- local function addPotentialRequires(global, endpos)
+ local function addRequires(global, endpos)
autoreq.check(state, global, endpos, function(moduleFile, stemname, targetSource)
local visiblePaths = rpath.getVisiblePath(uri, furi.decode(moduleFile))
if not visiblePaths or #visiblePaths == 0 then return end
@@ -728,9 +722,8 @@ local function checkMissingRequire(results, uri, start, finish)
end)
end
- undefined(uri, function(foundUndefined)
- if foundUndefined.start <= start and start <= foundUndefined.finish and foundUndefined.start <= finish and finish <= foundUndefined.finish then
- addPotentialRequires(foundUndefined.undefinedGlobal, foundUndefined.finish)
+ guide.eachSourceBetween(state.ast, start, finish, function (source)if vm.isUndefinedGlobal(source) then
+ addRequires(source[1], source.finish)
end
end)
end
@@ -747,7 +740,7 @@ return function (uri, start, finish, diagnostics)
checkSwapParams(results, uri, start, finish)
--checkExtractAsFunction(results, uri, start, finish)
checkJsonToLua(results, uri, start, finish)
- checkMissingRequire(results, uri, start, finish, diagnostics)
+ checkMissingRequire(results, uri, start, finish)
return results
end
diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua
index 87501277..d9d94959 100644
--- a/script/core/diagnostics/undefined-global.lua
+++ b/script/core/diagnostics/undefined-global.lua
@@ -20,42 +20,21 @@ return function (uri, callback)
return
end
- local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))
- local rspecial = config.get(uri, 'Lua.runtime.special')
- local cache = {}
-
-- 遍历全局变量,检查所有没有 set 模式的全局变量
guide.eachSourceType(state.ast, 'getglobal', function (src) ---@async
- local key = src[1]
- if not key then
- return
- end
- if dglobals[key] then
- return
- end
- if rspecial[key] then
- return
- end
- local node = src.node
- if node.tag ~= '_ENV' then
- return
- end
- if cache[key] == nil then
- await.delay()
- cache[key] = vm.hasGlobalSets(uri, 'variable', key)
- end
- if cache[key] then
- return
- end
- local message = lang.script('DIAG_UNDEF_GLOBAL', key)
- if requireLike[key:lower()] then
- message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key))
+ if vm.isUndefinedGlobal(src) then
+ local key = src[1]
+ local message = lang.script('DIAG_UNDEF_GLOBAL', key)
+ if requireLike[key:lower()] then
+ message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key))
+ end
+
+ callback {
+ start = src.start,
+ finish = src.finish,
+ message = message,
+ undefinedGlobal = src[1]
+ }
end
- callback {
- start = src.start,
- finish = src.finish,
- message = message,
- undefinedGlobal = src[1]
- }
end)
end