diff options
author | sumneko <sumneko@hotmail.com> | 2019-04-15 18:47:56 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-04-15 18:47:56 +0800 |
commit | be7c40b012cfc269a25cc3d8ef0dd4eefd99b461 (patch) | |
tree | a84cac2f6ab768cfc9de39291f539443c1d38e38 | |
parent | c8e51355694ea523ebfbf34c576868c6c59c166c (diff) | |
download | lua-language-server-be7c40b012cfc269a25cc3d8ef0dd4eefd99b461.zip |
检查多余的赋值
-rw-r--r-- | server/locale/en-US/script.lni | 1 | ||||
-rw-r--r-- | server/locale/zh-CN/script.lni | 1 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 36 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 8 |
4 files changed, 45 insertions, 1 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index 5da02108..0fac11c8 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -9,6 +9,7 @@ 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.' DIAG_OVER_MAX_ARGS = 'The function takes only {:d} parameters, but you passed {:d}.' +DIAG_OVER_MAX_ARGS = 'Only has {} variables, but you set {} values.' DIAG_AMBIGUITY_1 = 'Compute `0 {op} {num}` first. You may need to add brackets.' DIAG_LOWERCASE_GLOBAL = 'Global variable in lowercase initial' DIAG_DIAGNOSTICS = 'Diagnostics' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index a0fe9701..a71590cc 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -9,6 +9,7 @@ DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。' DIAG_DUPLICATE_INDEX = '重复的索引 `{}`。' DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。' DIAG_OVER_MAX_ARGS = '函数只接收 {:d} 个参数,但你传了 {:d} 个。' +DIAG_OVER_MAX_VALUES = '只有 {} 个变量,但你设置了 {} 个值。' DIAG_AMBIGUITY_1 = '会优先运算 `0 {op} {num}`,你可能需要加个括号。' DIAG_LOWERCASE_GLOBAL = '首字母小写的全局变量' DIAG_DIAGNOSTICS = '诊断' diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 1fdcac37..73f8c8ce 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -347,6 +347,33 @@ function mt:searchEmptyBlock(callback) end) end +function mt:searchRedundantValue(callback) + self.vm:eachSource(function (source) + if source.type == 'set' or source.type == 'local' then + local args = source[1] + local values = source[2] + if not source[2] then + return + end + local argCount, valueCount + if args.type == 'list' then + argCount = #args + else + argCount = 1 + end + if values.type == 'list' then + valueCount = #values + else + valueCount = 1 + end + for i = argCount + 1, valueCount do + local value = values[i] + callback(value.start, value.finish, argCount, valueCount) + end + end + end) +end + function mt:doDiagnostics(func, code, callback) if config.config.diagnostics.disable[code] then return @@ -455,7 +482,7 @@ return function (vm, lines, uri) } end) -- 调用函数时的参数数量是否超过函数的接收数量 - session:doDiagnostics(session.searchRedundantParameters, 'remainder-parameters', function (max, passed) + session:doDiagnostics(session.searchRedundantParameters, 'redundant-parameter', function (max, passed) return { level = DiagnosticSeverity.Information, message = lang.script('DIAG_OVER_MAX_ARGS', max, passed), @@ -504,5 +531,12 @@ return function (vm, lines, uri) message = lang.script.DIAG_EMPTY_BLOCK, } end) + -- 多余的赋值 + session:doDiagnostics(session.searchRedundantValue, 'redundant-value', function (max, passed) + return { + level = DiagnosticSeverity.Information, + message = lang.script('DIAG_OVER_MAX_VALUES', max, passed), + } + end) return session.datas end diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index 365b2732..9fbe9624 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -304,3 +304,11 @@ TEST [[ <!for _ in pairs(_VERSION) do end!> ]] + +TEST [[ +local _ = 1, <!2!> +]] + +TEST [[ +_ = 1, <!2!> +]] |