diff options
-rw-r--r-- | package.json | 11 | ||||
-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/constant/DiagnosticDefaultSeverity.lua | 1 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 30 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 28 |
6 files changed, 72 insertions, 0 deletions
diff --git a/package.json b/package.json index e150393a..fcaa3a85 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,17 @@ "scope": "resource", "type": "string" }, + "newfield-call": { + "default": "Warning", + "enum": [ + "Error", + "Warning", + "Information", + "Hint" + ], + "scope": "resource", + "type": "string" + }, "newline-call": { "default": "Information", "enum": [ diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index 9c108203..176d1846 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -13,6 +13,7 @@ DIAG_REDEFINED_LOCAL = 'Redefined local `{}`.' DIAG_DUPLICATE_INDEX = 'Duplicate index `{}`.' DIAG_DUPLICATE_METHOD = 'Duplicate method `{}`.' DIAG_PREVIOUS_CALL = 'Parsed as function call for the previous line. It may be necessary to add a `;` before.' +DIAG_PREFIELD_CALL = 'Parsed as `{} {}`. It may be necessary to add a `,` or `;` in the middle.' 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 `{}` first. You may need to add brackets.' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index b441db5a..98cc01c8 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -13,6 +13,7 @@ DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。' DIAG_DUPLICATE_INDEX = '重复的索引 `{}`。' DIAG_DUPLICATE_METHOD = '重复的方法 `{}`。' DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。' +DIAG_PREFIELD_CALL = '解析为了 `{} {}`。你可能需要在中间加一个`,`或`;`。' DIAG_OVER_MAX_ARGS = '函数只接收 {:d} 个参数,但你传了 {:d} 个。' DIAG_OVER_MAX_VALUES = '只有 {} 个变量,但你设置了 {} 个值。' DIAG_AMBIGUITY_1 = '会优先运算 `{}`,你可能需要加个括号。' diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua index cc26cab2..b7f07f63 100644 --- a/server/src/constant/DiagnosticDefaultSeverity.lua +++ b/server/src/constant/DiagnosticDefaultSeverity.lua @@ -8,6 +8,7 @@ return { ['trailing-space'] = 'Hint', ['redefined-local'] = 'Hint', ['newline-call'] = 'Information', + ['newfield-call'] = 'Warning', ['redundant-parameter'] = 'Hint', ['ambiguity-1'] = 'Warning', ['lowercase-global'] = 'Information', diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 8584897e..c99f6e87 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -251,6 +251,30 @@ function mt:searchNewLineCall(callback) end) end +function mt:searchNewFieldCall(callback) + local lines = self.lines + self.vm:eachSource(function (source) + if source.type ~= 'table' then + return + end + for i = 1, #source do + local field = source[i] + if field.type == 'simple' then + local callSource = field[#field] + local funcSource = field[#field-1] + local callLine = lines:rowcol(callSource.start) + local funcLine = lines:rowcol(funcSource.finish) + if callLine > funcLine then + callback(funcSource.start, callSource.finish + , lines.buf:sub(funcSource.start, funcSource.finish) + , lines.buf:sub(callSource.start, callSource.finish) + ) + end + end + end + end) +end + function mt:searchRedundantParameters(callback) self.vm:eachSource(function (source) local args = source:bindCall() @@ -911,6 +935,12 @@ return function (vm, lines, uri) message = lang.script.DIAG_PREVIOUS_CALL, } end) + -- 以字符串开始的field(可能被误解析为了上一行的call) + session:doDiagnostics(session.searchNewFieldCall, 'newfield-call', function (func, call) + return { + message = lang.script('DIAG_PREFIELD_CALL', func, call), + } + end) -- 调用函数时的参数数量是否超过函数的接收数量 session:doDiagnostics(session.searchRedundantParameters, 'redundant-parameter', function (max, passed) return { diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index 555bc0a8..c854e5a2 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -185,6 +185,34 @@ print() ]] TEST [[ +return { + print + <!'string'!> +} +]] + +TEST [[ +return { + print + <!{ + x = 1, + }!> +} +]] + +TEST [[ +print() +'string' +]] + +TEST [[ +print +{ + x = 1, +} +]] + +TEST [[ local function x(a, b) return a, b end |