summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script-beta/core/diagnostics/unused-function.lua13
-rw-r--r--script-beta/parser/guide.lua39
-rw-r--r--script-beta/vm/getGlobals.lua10
-rw-r--r--script-beta/vm/getLibrary.lua16
-rw-r--r--script-beta/vm/getValue.lua5
-rw-r--r--script-beta/vm/vm.lua18
6 files changed, 72 insertions, 29 deletions
diff --git a/script-beta/core/diagnostics/unused-function.lua b/script-beta/core/diagnostics/unused-function.lua
index 52c7749a..f417fd92 100644
--- a/script-beta/core/diagnostics/unused-function.lua
+++ b/script-beta/core/diagnostics/unused-function.lua
@@ -21,16 +21,15 @@ return function (uri, callback)
and parent.type ~= 'setglobal' then
return
end
- local hasSet
local hasGet
- vm.eachRef(source, function (src)
- if vm.isSet(src) then
- hasSet = true
- else
+ local refs = guide.requestReference(source)
+ for _, src in ipairs(refs) do
+ if vm.isGet(src) then
hasGet = true
+ break
end
- end)
- if not hasGet and hasSet then
+ end
+ if not hasGet then
callback {
start = source.start,
finish = source.finish,
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index 033ffde1..8f168543 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -994,7 +994,7 @@ function m.searchFields(status, obj, key)
if not simple then
return nil
end
- simple[2] = key and ('s|' .. key) or '*'
+ simple[#simple+1] = key and ('s|' .. key) or '*'
m.searchSameFields(newStatus, simple, 'def')
local results = newStatus.results
m.cleanResults(results)
@@ -1242,7 +1242,7 @@ end
function m.searchSameFields(status, simple, mode)
local first = simple.first
- local fref = first.ref
+ local fref = first and first.ref
local queue = {}
if fref then
for i = 1, #fref do
@@ -1277,11 +1277,7 @@ function m.searchSameFields(status, simple, mode)
end
end
-function m.searchRefsAsFunctionReturn(status, obj)
- -- 只有 function 才搜索返回值引用
- if obj.type ~= 'function' then
- return
- end
+function m.searchRefsAsFunctionReturn(status, obj, mode)
status.results[#status.results+1] = obj
-- 搜索所在函数
local currentFunc = m.getParentFunction(obj)
@@ -1346,6 +1342,32 @@ function m.searchRefsAsFunctionReturn(status, obj)
end
end
+function m.searchRefsAsFunctionSet(status, obj, mode)
+ local parent = obj.parent
+ if not parent then
+ return
+ end
+ if parent.type == 'local'
+ or parent.type == 'setlocal'
+ or parent.type == 'setglobal'
+ or parent.type == 'setfield'
+ or parent.type == 'setmethod'
+ or parent.type == 'setindex'
+ or parent.type == 'tableindex'
+ or parent.type == 'tablefield' then
+ m.searchRefs(status, parent, mode)
+ end
+end
+
+function m.searchRefsAsFunction(status, obj, mode)
+ if obj.type ~= 'function' then
+ return
+ end
+ m.searchRefsAsFunctionSet(status, obj, mode)
+ -- 检查自己作为返回函数时的引用
+ m.searchRefsAsFunctionReturn(status, obj, mode)
+end
+
function m.cleanResults(results)
local mark = {}
for i = #results, 1, -1 do
@@ -1406,8 +1428,7 @@ function m.requestReference(obj)
-- 根据 field 搜索引用
m.searchRefs(status, obj, 'ref')
- -- 检查自己作为返回函数时的引用
- m.searchRefsAsFunctionReturn(status, obj)
+ m.searchRefsAsFunction(status, obj, 'ref')
return status.results
end
diff --git a/script-beta/vm/getGlobals.lua b/script-beta/vm/getGlobals.lua
index 2760fea4..0ff92e3a 100644
--- a/script-beta/vm/getGlobals.lua
+++ b/script-beta/vm/getGlobals.lua
@@ -7,12 +7,8 @@ local function getGlobals(root)
return nil
end
local cache = {}
- local mark = {}
- vm.eachField(env, function (src)
- if mark[src] then
- return
- end
- mark[src] = true
+ local fields = guide.requestFields(env)
+ for _, src in ipairs(fields) do
local name = vm.getKeyName(src)
if not name then
return
@@ -24,7 +20,7 @@ local function getGlobals(root)
end
cache[name][#cache[name]+1] = src
vm.cache.getGlobal[src] = name
- end)
+ end
return cache
end
diff --git a/script-beta/vm/getLibrary.lua b/script-beta/vm/getLibrary.lua
index 32438805..ac6402de 100644
--- a/script-beta/vm/getLibrary.lua
+++ b/script-beta/vm/getLibrary.lua
@@ -70,13 +70,21 @@ local function checkNode(source)
end
end
+local function checkRef(source)
+ local results = guide.requestReference(source)
+ for _, src in ipairs(results) do
+ local lib = checkStdLibrary(src) or checkNode(src)
+ if lib then
+ return lib
+ end
+ end
+ return nil
+end
+
local function getLibrary(source)
return checkNode(source)
or checkStdLibrary(source)
- or vm.eachRef(source, function (src)
- return checkStdLibrary(src)
- or checkNode(src)
- end, 100)
+ or checkRef(source)
end
function vm.getLibrary(source)
diff --git a/script-beta/vm/getValue.lua b/script-beta/vm/getValue.lua
index fc4ba64b..52c66064 100644
--- a/script-beta/vm/getValue.lua
+++ b/script-beta/vm/getValue.lua
@@ -485,12 +485,13 @@ local function inferByGetTable(results, source)
end
local function checkDef(results, source)
- vm.eachDef(source, function (src)
+ local defs = guide.requestDefinition(source)
+ for _, src in ipairs(defs) do
local tp = vm.getValue(src)
if tp then
merge(results, tp)
end
- end)
+ end
end
local function checkLibraryTypes(source)
diff --git a/script-beta/vm/vm.lua b/script-beta/vm/vm.lua
index f68ce6ca..556de3df 100644
--- a/script-beta/vm/vm.lua
+++ b/script-beta/vm/vm.lua
@@ -80,6 +80,24 @@ function m.isSet(src)
return false
end
+function m.isGet(src)
+ local tp = src.type
+ if tp == 'getglobal'
+ or tp == 'getlocal'
+ or tp == 'getfield'
+ or tp == 'getmethod'
+ or tp == 'getindex' then
+ return true
+ end
+ if tp == 'call' then
+ local special = m.getSpecial(src.node)
+ if special == 'rawget' then
+ return true
+ end
+ end
+ return false
+end
+
function m.getArgInfo(source)
local callargs = source.parent
if not callargs or callargs.type ~= 'callargs' then