summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-08-27 14:06:19 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-08-27 14:06:19 +0800
commita90fa730acdbd823e556dcbc66ab8a360650c290 (patch)
treed2a833f6fd46bebbef204f2d88f7dff40a900f7b /server
parent1fd5d5954f1c95b97dd9c5ab1c4dabb714bd5003 (diff)
downloadlua-language-server-a90fa730acdbd823e556dcbc66ab8a360650c290.zip
诊断未使用的局部函数
Diffstat (limited to 'server')
-rw-r--r--server/src/constant/DiagnosticDefaultSeverity.lua1
-rw-r--r--server/src/core/diagnostics.lua56
-rw-r--r--server/test/diagnostics/init.lua52
3 files changed, 90 insertions, 19 deletions
diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua
index 33df37d8..3cd67528 100644
--- a/server/src/constant/DiagnosticDefaultSeverity.lua
+++ b/server/src/constant/DiagnosticDefaultSeverity.lua
@@ -1,5 +1,6 @@
return {
['unused-local'] = 'Hint',
+ ['unused-function'] = 'Hint',
['undefined-global'] = 'Warning',
['unused-label'] = 'Hint',
['unused-vararg'] = 'Hint',
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index 26327ea2..509fe48c 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -9,6 +9,13 @@ local DiagnosticTag = require 'constant.DiagnosticTag'
local mt = {}
mt.__index = mt
+local function isContainPos(obj, start, finish)
+ if obj.start <= start and obj.finish >= finish then
+ return true
+ end
+ return false
+end
+
function mt:searchUnusedLocals(callback)
self.vm:eachSource(function (source)
local loc = source:bindLocal()
@@ -39,6 +46,41 @@ function mt:searchUnusedLocals(callback)
end)
end
+function mt:searchUnusedFunctions(callback)
+ self.vm:eachSource(function (source)
+ local loc = source:bindLocal()
+ if not loc then
+ return
+ end
+ if loc:get 'emmy arg' then
+ return
+ end
+ if source:action() ~= 'local' then
+ return
+ end
+ if loc:get 'hide' then
+ return
+ end
+ local used = loc:eachInfo(function (info)
+ if info.type == 'get' then
+ return true
+ end
+ end)
+ if used then
+ return
+ end
+ loc:eachInfo(function (info, src)
+ if info.type == 'set' or info.type == 'local' then
+ local v = src:bindValue()
+ local func = v and v:getFunction()
+ if func then
+ callback(func:getSource().start, func:getSource().finish)
+ end
+ end
+ end)
+ end)
+end
+
function mt:searchUndefinedGlobal(callback)
local definedGlobal = {}
for name in pairs(config.config.diagnostics.globals) do
@@ -112,13 +154,6 @@ function mt:searchUnusedVararg(callback)
end)
end
-local function isContainPos(obj, start, finish)
- if obj.start <= start and obj.finish >= finish then
- return true
- end
- return false
-end
-
local function isInString(vm, start, finish)
return vm:eachSource(function (source)
if source.type == 'string' and isContainPos(source, start, finish) then
@@ -798,6 +833,13 @@ return function (vm, lines, uri)
tags = {DiagnosticTag.Unnecessary},
}
end)
+ -- 未使用的函数
+ session:doDiagnostics(session.searchUnusedFunctions, 'unused-function', function ()
+ return {
+ message = lang.script.DIAG_UNUSED_FUNCTION,
+ tags = {DiagnosticTag.Unnecessary},
+ }
+ end)
-- 读取未定义全局变量
session:doDiagnostics(session.searchUndefinedGlobal, 'undefined-global', function (key)
local message = lang.script('DIAG_UNDEF_GLOBAL', key)
diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua
index 981133f5..70a6c586 100644
--- a/server/test/diagnostics/init.lua
+++ b/server/test/diagnostics/init.lua
@@ -5,18 +5,26 @@ local service = require 'service'
rawset(_G, 'TEST', true)
-local function catch_target(script)
+local function catch_target(script, ...)
local list = {}
- local cur = 1
- local cut = 0
- while true do
- local start, finish = script:find('<!.-!>', cur)
- if not start then
- break
+ local function catch(buf)
+ local cur = 1
+ local cut = 0
+ while true do
+ local start, finish = buf:find('<!.-!>', cur)
+ if not start then
+ break
+ end
+ list[#list+1] = { start - cut, finish - 4 - cut }
+ cur = finish + 1
+ cut = cut + 4
+ end
+ end
+ catch(script)
+ if ... then
+ for _, buf in ipairs {...} do
+ catch(buf)
end
- list[#list+1] = { start - cut, finish - 4 - cut }
- cur = finish + 1
- cut = cut + 4
end
local new_script = script:gsub('<!(.-)!>', '%1')
return new_script, list
@@ -38,8 +46,8 @@ local function founded(targets, results)
return true
end
-function TEST(script)
- local new_script, target = catch_target(script)
+function TEST(script, ...)
+ local new_script, target = catch_target(script, ...)
local lsp = service()
local ast = parser:ast(new_script, 'lua', 'Lua 5.3')
assert(ast)
@@ -65,6 +73,26 @@ TEST [[
local <!x!>
]]
+TEST([[
+<!local function x()
+end!>
+]],
+[[
+local function <!x!>()
+end
+]]
+)
+
+TEST [[
+local <!x!> = <!function () end!>
+]]
+
+TEST [[
+local <!x!>
+x = <!function () end!>
+]]
+
+
TEST [[
print(<!x!>)
print(<!log!>)