summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorsumneko <sumneko@hotmail.com>2019-05-14 16:09:38 +0800
committersumneko <sumneko@hotmail.com>2019-05-14 16:09:38 +0800
commitb5a351cd77cb6f7ddde692557cf0c5049d5ffbac (patch)
treef633e19250fae4b7ccea8265cba029d435420eef /server
parentfe3930a9f692c07eede7de3cb341ea0749b2e2ba (diff)
downloadlua-language-server-b5a351cd77cb6f7ddde692557cf0c5049d5ffbac.zip
检查未使用的不定参数
Diffstat (limited to 'server')
-rw-r--r--server/locale/en-US/script.lni1
-rw-r--r--server/locale/zh-CN/script.lni1
-rw-r--r--server/src/constant/DiagnosticDefaultSeverity.lua1
-rw-r--r--server/src/core/diagnostics.lua22
-rw-r--r--server/src/vm/function.lua2
-rw-r--r--server/test/diagnostics/init.lua9
6 files changed, 34 insertions, 2 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni
index 1072a894..51f99eb6 100644
--- a/server/locale/en-US/script.lni
+++ b/server/locale/en-US/script.lni
@@ -5,6 +5,7 @@ DIAG_UNDEF_GLOBAL = 'Undefined global `{}`.'
DIAG_UNDEF_ENV_CHILD = 'Undefined variable `{}` (overloaded `_ENV` ).'
DIAG_UNDEF_FENV_CHILD = 'Undefined variable `{}` (inside module).'
DIAG_UNUSED_LABEL = 'Unused label `{}`.'
+DIAG_UNUSED_VARARG = 'Unused vararg.'
DIAG_REDEFINED_LOCAL = 'Redefined local `{}`.'
DIAG_DUPLICATE_INDEX = 'Duplicate index `{}`.'
DIAG_PREVIOUS_CALL = 'Parsed as function call for the previous line. It may be necessary to add a `;` before.'
diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni
index beabf898..aa2daf05 100644
--- a/server/locale/zh-CN/script.lni
+++ b/server/locale/zh-CN/script.lni
@@ -5,6 +5,7 @@ DIAG_UNDEF_GLOBAL = '未定义的全局变量 `{}`。'
DIAG_UNDEF_ENV_CHILD = '未定义的变量 `{}`(重载了 `_ENV` )。'
DIAG_UNDEF_FENV_CHILD = '未定义的变量 `{}`(处于模块中)。'
DIAG_UNUSED_LABEL = '未使用的标签 `{}`。'
+DIAG_UNUSED_VARARG = '未使用的不定参数。'
DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。'
DIAG_DUPLICATE_INDEX = '重复的索引 `{}`。'
DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。'
diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua
index bc685cf2..2088a922 100644
--- a/server/src/constant/DiagnosticDefaultSeverity.lua
+++ b/server/src/constant/DiagnosticDefaultSeverity.lua
@@ -2,6 +2,7 @@ return {
['unused-local'] = 'Hint',
['undefined-global'] = 'Warning',
['unused-label'] = 'Hint',
+ ['unused-vararg'] = 'Hint',
['trailing-space'] = 'Hint',
['redefined-local'] = 'Hint',
['newline-call'] = 'Information',
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index dcd1a10b..16132bee 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -92,6 +92,22 @@ function mt:searchUnusedLabel(callback)
end)
end
+function mt:searchUnusedVararg(callback)
+ self.vm:eachSource(function (source)
+ local value = source:bindFunction()
+ if not value then
+ return
+ end
+ local func = value:getFunction()
+ if not func then
+ return
+ end
+ if func._dotsSource and not func._dotsLoad then
+ callback(func._dotsSource.start, func._dotsSource.finish)
+ end
+ end)
+end
+
local function isContainPos(obj, start, finish)
if obj.start <= start and obj.finish >= finish then
return true
@@ -663,6 +679,12 @@ return function (vm, lines, uri)
message = lang.script('DIAG_UNUSED_LABEL', key)
}
end)
+ -- 未使用的不定参数
+ session:doDiagnostics(session.searchUnusedVararg, 'unused-vararg', function ()
+ return {
+ message = lang.script.DIAG_UNUSED_VARARG
+ }
+ end)
-- 只有空格与制表符的行,以及后置空格
session:doDiagnostics(session.searchSpaces, 'trailing-space', function (message)
return {
diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua
index 1ba49f9c..9b236c55 100644
--- a/server/src/vm/function.lua
+++ b/server/src/vm/function.lua
@@ -210,6 +210,7 @@ function mt:loadDots()
if not self._dots then
self._dots = createMulti()
end
+ self._dotsLoad = true
return self._dots
end
@@ -373,6 +374,7 @@ function mt:createArg(vm, arg)
self:addArg(arg[1], arg, value)
elseif arg.type == '...' then
self._dots = createMulti()
+ self._dotsSource = arg
end
end
diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua
index 2ef052bf..cb842f71 100644
--- a/server/test/diagnostics/init.lua
+++ b/server/test/diagnostics/init.lua
@@ -149,7 +149,6 @@ TEST [[
(''):sub(1, 2)
]]
-
TEST [=[
return [[
@@ -177,6 +176,12 @@ f()
]]
TEST [[
+local function f(<!...!>)
+end
+f()
+]]
+
+TEST [[
local function f(var)
print(var)
end
@@ -218,7 +223,7 @@ mt:f(1, 2, <!3!>, <!4!>)
TEST [[
local function f(a, b, ...)
- return a, b
+ return a, b, ...
end
f(1, 2, 3, 4)
]]