Skip to content

Commit 393f3e8

Browse files
committed
Extracted undefined global check
1 parent 62068c0 commit 393f3e8

File tree

4 files changed

+47
-46
lines changed

4 files changed

+47
-46
lines changed

script/core/code-action.lua

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local autoreq = require 'core.completion.auto-require'
88
local rpath = require 'workspace.require-path'
99
local furi = require 'file-uri'
1010
local undefined = require 'core.diagnostics.undefined-global'
11+
local vm = require 'vm'
1112

1213
---@param uri uri
1314
---@param row integer
@@ -695,14 +696,7 @@ local function checkMissingRequire(results, uri, start, finish)
695696
return
696697
end
697698

698-
local potentialGlobals = {}
699-
guide.eachSourceBetween(state.ast, start, finish, function (source)
700-
if source.type == 'getglobal' then
701-
potentialGlobals[#potentialGlobals+1] = { name = source[1], endpos = source.finish }
702-
end
703-
end)
704-
705-
local function addPotentialRequires(global, endpos)
699+
local function addRequires(global, endpos)
706700
autoreq.check(state, global, endpos, function(moduleFile, stemname, targetSource)
707701
local visiblePaths = rpath.getVisiblePath(uri, furi.decode(moduleFile))
708702
if not visiblePaths or #visiblePaths == 0 then return end
@@ -728,9 +722,8 @@ local function checkMissingRequire(results, uri, start, finish)
728722
end)
729723
end
730724

731-
undefined(uri, function(foundUndefined)
732-
if foundUndefined.start <= start and start <= foundUndefined.finish and foundUndefined.start <= finish and finish <= foundUndefined.finish then
733-
addPotentialRequires(foundUndefined.undefinedGlobal, foundUndefined.finish)
725+
guide.eachSourceBetween(state.ast, start, finish, function (source)if vm.isUndefinedGlobal(source) then
726+
addRequires(source[1], source.finish)
734727
end
735728
end)
736729
end
@@ -747,7 +740,7 @@ return function (uri, start, finish, diagnostics)
747740
checkSwapParams(results, uri, start, finish)
748741
--checkExtractAsFunction(results, uri, start, finish)
749742
checkJsonToLua(results, uri, start, finish)
750-
checkMissingRequire(results, uri, start, finish, diagnostics)
743+
checkMissingRequire(results, uri, start, finish)
751744

752745
return results
753746
end

script/core/diagnostics/undefined-global.lua

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,21 @@ return function (uri, callback)
2020
return
2121
end
2222

23-
local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))
24-
local rspecial = config.get(uri, 'Lua.runtime.special')
25-
local cache = {}
26-
2723
-- 遍历全局变量,检查所有没有 set 模式的全局变量
2824
guide.eachSourceType(state.ast, 'getglobal', function (src) ---@async
29-
local key = src[1]
30-
if not key then
31-
return
32-
end
33-
if dglobals[key] then
34-
return
35-
end
36-
if rspecial[key] then
37-
return
38-
end
39-
local node = src.node
40-
if node.tag ~= '_ENV' then
41-
return
42-
end
43-
if cache[key] == nil then
44-
await.delay()
45-
cache[key] = vm.hasGlobalSets(uri, 'variable', key)
46-
end
47-
if cache[key] then
48-
return
49-
end
50-
local message = lang.script('DIAG_UNDEF_GLOBAL', key)
51-
if requireLike[key:lower()] then
52-
message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key))
25+
if vm.isUndefinedGlobal(src) then
26+
local key = src[1]
27+
local message = lang.script('DIAG_UNDEF_GLOBAL', key)
28+
if requireLike[key:lower()] then
29+
message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key))
30+
end
31+
32+
callback {
33+
start = src.start,
34+
finish = src.finish,
35+
message = message,
36+
undefinedGlobal = src[1]
37+
}
5338
end
54-
callback {
55-
start = src.start,
56-
finish = src.finish,
57-
message = message,
58-
undefinedGlobal = src[1]
59-
}
6039
end)
6140
end

script/vm/global.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local util = require 'utility'
22
local scope = require 'workspace.scope'
33
local guide = require 'parser.guide'
4+
local config = require 'config'
45
---@class vm
56
local vm = require 'vm.vm'
67

@@ -518,6 +519,33 @@ function vm.hasGlobalSets(suri, cate, name)
518519
return true
519520
end
520521

522+
---@param src parser.object
523+
local function checkIsUndefinedGlobal(src)
524+
local key = src[1]
525+
526+
local uri = guide.getUri(src)
527+
local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))
528+
local rspecial = config.get(uri, 'Lua.runtime.special')
529+
530+
local node = src.node
531+
return src.type == 'getglobal' and key and not (
532+
dglobals[key] or
533+
rspecial[key] or
534+
node.tag ~= '_ENV' or
535+
vm.hasGlobalSets(uri, 'variable', key)
536+
)
537+
end
538+
539+
---@param src parser.object
540+
---@return boolean
541+
function vm.isUndefinedGlobal(src)
542+
local node = vm.compileNode(src)
543+
if node._undefined_global == nil then
544+
node._undefined_global = checkIsUndefinedGlobal(src)
545+
end
546+
return node._undefined_global
547+
end
548+
521549
---@param source parser.object
522550
function compileObject(source)
523551
if source._globalNode ~= nil then

script/vm/node.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ vm.nodeCache = setmetatable({}, util.MODE_K)
1515
---@field [integer] vm.node.object
1616
---@field [vm.node.object] true
1717
---@field fields? table<vm.node|string, vm.node>
18+
---@field _undefined_global boolean?
1819
local mt = {}
1920
mt.__index = mt
2021
mt.id = 0

0 commit comments

Comments
 (0)