summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server-beta/src/core/diagnostics/undefined-global.lua12
-rw-r--r--server-beta/src/core/diagnostics/unused-function.lua8
-rw-r--r--server-beta/src/searcher/eachRef.lua15
-rw-r--r--server-beta/test/diagnostics/init.lua7
4 files changed, 38 insertions, 4 deletions
diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua
index 6c9abaca..ec796086 100644
--- a/server-beta/src/core/diagnostics/undefined-global.lua
+++ b/server-beta/src/core/diagnostics/undefined-global.lua
@@ -3,6 +3,7 @@ local searcher = require 'searcher'
local lang = require 'language'
local library = require 'library'
local config = require 'config'
+local guide = require 'parser.guide'
return function (uri, callback)
local ast = files.getAst(uri)
@@ -60,4 +61,15 @@ return function (uri, callback)
}
end
end)
+ -- 再遍历一次 getglobal ,找出 _ENV 被重载的情况
+ guide.eachSourceType(ast.ast, 'getglobal', function (source)
+ if hasSet[source] == nil then
+ local key = source[1]
+ callback {
+ start = source.start,
+ finish = source.finish,
+ message = lang.script('DIAG_UNDEF_ENV_CHILD', key),
+ }
+ end
+ end)
end
diff --git a/server-beta/src/core/diagnostics/unused-function.lua b/server-beta/src/core/diagnostics/unused-function.lua
index 80123948..0886660d 100644
--- a/server-beta/src/core/diagnostics/unused-function.lua
+++ b/server-beta/src/core/diagnostics/unused-function.lua
@@ -11,12 +11,16 @@ return function (uri, callback)
end
guide.eachSourceType(ast.ast, 'function', function (source)
local hasGet
+ local hasSet
searcher.eachRef(source, function (info)
- if info.mode == 'get' then
+ if info.mode == 'get' then
hasGet = true
+ elseif info.mode == 'set'
+ or info.mode == 'declare' then
+ hasSet = true
end
end)
- if not hasGet then
+ if not hasGet and hasSet then
callback {
start = source.start,
finish = source.finish,
diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua
index 649206e6..8f6b5c2f 100644
--- a/server-beta/src/searcher/eachRef.lua
+++ b/server-beta/src/searcher/eachRef.lua
@@ -78,7 +78,17 @@ local function ofValue(value, callback)
source = value,
mode = 'value',
}
- searcher.eachRef(value, callback)
+ local parent = value.parent
+ if parent.type == 'local'
+ or parent.type == 'setglobal'
+ or parent.type == 'setlocal'
+ or parent.type == 'setfield'
+ or parent.type == 'setmethod'
+ or parent.type == 'setindex' then
+ if parent.value == value then
+ searcher.eachRef(parent, callback)
+ end
+ end
end
local function ofSelf(loc, callback)
@@ -325,7 +335,8 @@ local function eachRef(source, callback)
ofGoTo(source, callback)
elseif stype == 'label' then
ofLabel(source, callback)
- elseif stype == 'table' then
+ elseif stype == 'table'
+ or stype == 'function' then
ofValue(source, callback)
elseif stype == 'main' then
ofMain(source, callback)
diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua
index 0c2b077f..524e9626 100644
--- a/server-beta/test/diagnostics/init.lua
+++ b/server-beta/test/diagnostics/init.lua
@@ -69,6 +69,12 @@ TEST [[
local <!x!>
]]
+TEST [[
+local function x()
+end
+x()
+]]
+
TEST([[
<!local function x()
end!>
@@ -145,6 +151,7 @@ local _ENV
TEST [[
local x
return x, function (<!x!>)
+ return x
end
]]