summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server-beta/src/core/diagnostics/undefined-global.lua37
-rw-r--r--server-beta/src/core/diagnostics/unused-function.lua29
-rw-r--r--server-beta/src/core/diagnostics/unused-local.lua2
-rw-r--r--server-beta/src/parser/guide.lua17
4 files changed, 73 insertions, 12 deletions
diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua
index b3d19c21..db7af133 100644
--- a/server-beta/src/core/diagnostics/undefined-global.lua
+++ b/server-beta/src/core/diagnostics/undefined-global.lua
@@ -1,3 +1,36 @@
-return function ()
-
+local files = require 'files'
+local guide = require 'parser.guide'
+local searcher = require 'searcher'
+local define = require 'proto.define'
+local lang = require 'language'
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+
+ local hasSet = {}
+ searcher.eachGlobal(ast.ast, function (info)
+ local key = info.key
+ if hasSet[key] ~= nil then
+ return
+ end
+ hasSet[key] = false
+ searcher.eachRef(info.source, function (info)
+ if info.mode == 'set' then
+ hasSet[key] = true
+ end
+ end)
+ end)
+ searcher.eachGlobal(ast.ast, function (info)
+ local source = info.source
+ if info.mode == 'get' and not hasSet[info.key] then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ message = lang.script('DIAG_UNDEF_GLOBAL', info.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 b3d19c21..80123948 100644
--- a/server-beta/src/core/diagnostics/unused-function.lua
+++ b/server-beta/src/core/diagnostics/unused-function.lua
@@ -1,3 +1,28 @@
-return function ()
-
+local files = require 'files'
+local guide = require 'parser.guide'
+local searcher = require 'searcher'
+local define = require 'proto.define'
+local lang = require 'language'
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+ guide.eachSourceType(ast.ast, 'function', function (source)
+ local hasGet
+ searcher.eachRef(source, function (info)
+ if info.mode == 'get' then
+ hasGet = true
+ end
+ end)
+ if not hasGet then
+ callback {
+ start = source.start,
+ finish = source.finish,
+ tags = { define.DiagnosticTag.Unnecessary },
+ message = lang.script.DIAG_UNUSED_FUNCTION,
+ }
+ end
+ end)
end
diff --git a/server-beta/src/core/diagnostics/unused-local.lua b/server-beta/src/core/diagnostics/unused-local.lua
index 9b97ca88..d110a714 100644
--- a/server-beta/src/core/diagnostics/unused-local.lua
+++ b/server-beta/src/core/diagnostics/unused-local.lua
@@ -8,7 +8,7 @@ local function hasGet(loc)
return false
end
for _, ref in ipairs(loc.ref) do
- if ref.type == 'get' then
+ if ref.mode == 'get' then
return true
end
end
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua
index 773fb6d5..d8dd873e 100644
--- a/server-beta/src/parser/guide.lua
+++ b/server-beta/src/parser/guide.lua
@@ -309,16 +309,19 @@ end
--- 遍历所有指定类型的source
function m.eachSourceType(ast, type, callback)
- local cache = ast.typeCache
+ if not ast.typeCache then
+ ast.typeCache = {}
+ end
+ local cache = ast.typeCache[type]
if not cache then
cache = {}
- ast.typeCache = cache
- m.eachSource(ast, function (source)
- if source.type == type then
- cache[#cache+1] = source
- end
- end)
+ ast.typeCache[type] = cache
end
+ m.eachSource(ast, function (source)
+ if source.type == type then
+ cache[#cache+1] = source
+ end
+ end)
for i = 1, #cache do
callback(cache[i])
end