diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-05 19:31:38 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-05 19:31:38 +0800 |
commit | c844e46ae97f28ec928db6b701fdf5f2eea2664d (patch) | |
tree | 938d77a0aadb6a3975d146659302137f58d9babc /server-beta | |
parent | 5d4d2ccd06d56bb92219baf3825c5786efdf906c (diff) | |
download | lua-language-server-c844e46ae97f28ec928db6b701fdf5f2eea2664d.zip |
更新诊断实现
Diffstat (limited to 'server-beta')
-rw-r--r-- | server-beta/src/core/diagnostics/global-in-nil-env.lua | 23 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/newline-call.lua | 39 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/redundant-parameter.lua | 81 | ||||
-rw-r--r-- | server-beta/src/parser/ast.lua | 2 | ||||
-rw-r--r-- | server-beta/test/diagnostics/init.lua | 47 |
5 files changed, 182 insertions, 10 deletions
diff --git a/server-beta/src/core/diagnostics/global-in-nil-env.lua b/server-beta/src/core/diagnostics/global-in-nil-env.lua index a19a341f..72375ea5 100644 --- a/server-beta/src/core/diagnostics/global-in-nil-env.lua +++ b/server-beta/src/core/diagnostics/global-in-nil-env.lua @@ -40,14 +40,25 @@ return function (uri, callback) local mode, pathA = guide.getPath(nilDef, source) if mode == 'before' and mayRun(pathA) then - callback { - start = source.start, - finish = source.finish, - uri = uri, - message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, - } + ok = nilDef + break end end + if ok then + callback { + start = source.start, + finish = source.finish, + uri = uri, + message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, + relative = { + { + start = ok.start, + finish = ok.finish, + uri = uri, + } + } + } + end end end diff --git a/server-beta/src/core/diagnostics/newline-call.lua b/server-beta/src/core/diagnostics/newline-call.lua index b3d19c21..52b84197 100644 --- a/server-beta/src/core/diagnostics/newline-call.lua +++ b/server-beta/src/core/diagnostics/newline-call.lua @@ -1,3 +1,38 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + local lines = files.getLines(uri) + + guide.eachSourceType(ast.ast, 'call', function (source) + local node = source.node + local args = source.args + if not args then + return + end + + -- 必须有其他人在继续使用当前对象 + if not source.next then + return + end + + local nodeRow = guide.positionOf(lines, node.start) + local argRow = guide.positionOf(lines, args.start) + if nodeRow == argRow then + return + end + + if #args == 1 then + callback { + start = args.start, + finish = args.finish, + message = lang.script.DIAG_PREVIOUS_CALL, + } + end + end) end diff --git a/server-beta/src/core/diagnostics/redundant-parameter.lua b/server-beta/src/core/diagnostics/redundant-parameter.lua index b3d19c21..c087197c 100644 --- a/server-beta/src/core/diagnostics/redundant-parameter.lua +++ b/server-beta/src/core/diagnostics/redundant-parameter.lua @@ -1,3 +1,80 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local searcher = require 'searcher' +local lang = require 'language' +local define = require 'proto.define' + +local function packCallArgs(source) + if not source.args then + return nil + end + local result = {} + if source.node and source.node.type == 'getmethod' then + result[#result+1] = source.node.node + end + for _, arg in ipairs(source.args) do + result[#result+1] = arg + end + return result +end + +local function packFuncArgs(source) + if not source.args then + return nil + end + local result = {} + if source.parent and source.parent.type == 'setmethod' then + result[#result+1] = source.parent.node + end + for _, arg in ipairs(source.args) do + result[#result+1] = arg + end + return result +end + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + guide.eachSourceType(ast.ast, 'call', function (source) + local callArgs = packCallArgs(source) + if not callArgs then + return + end + + local func = source.node + local funcArgs + searcher.eachRef(func, function (info) + if info.mode == 'value' then + local src = info.source + if src.type == 'function' then + local args = packFuncArgs(src) + if args and (not funcArgs or #funcArgs < #args) then + funcArgs = args + end + end + end + end) + + if not funcArgs then + return + end + + local lastArg = funcArgs[#funcArgs] + if lastArg and lastArg.type == '...' then + return + end + + for i = #funcArgs + 1, #callArgs do + local arg = callArgs[i] + callback { + start = arg.start, + finish = arg.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_OVER_MAX_ARGS', #funcArgs, #callArgs) + } + end + end) end diff --git a/server-beta/src/parser/ast.lua b/server-beta/src/parser/ast.lua index 39e7d871..04148da0 100644 --- a/server-beta/src/parser/ast.lua +++ b/server-beta/src/parser/ast.lua @@ -607,6 +607,7 @@ local Defs = { local current = units[i] current.node = last current.start = last.start + last.next = current last = units[i] end return last @@ -1212,6 +1213,7 @@ local Defs = { local call = createCall(exp, func.finish + 1, exp.finish) call.node = func call.start = func.start + func.next = call values = { call } keys.range = call.finish end diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua index 680d6bf6..2970938d 100644 --- a/server-beta/test/diagnostics/init.lua +++ b/server-beta/test/diagnostics/init.lua @@ -210,6 +210,44 @@ x(1, 2, <!3!>) ]] TEST [[ +local function x(a, b, ...) + return a, b, ... +end +x(1, 2, 3, 4, 5) +]] + +TEST [[ +local m = {} +function m:x(a, b) + return a, b +end +m:x(1, 2, <!3!>) +]] + +TEST [[ +local m = {} +function m:x(a, b) + return a, b +end +m.x(1, 2, 3, <!4!>) +]] + +TEST [[ +local m = {} +function m.x(a, b) + return a, b +end +m:x(1, <!2!>, <!3!>, <!4!>) +]] + +TEST [[ +local m = {} +function m.x() +end +m:x() +]] + +TEST [[ InstanceName = 1 Instance = _G[InstanceName] ]] @@ -224,18 +262,27 @@ return [[ ]] ]=] +config.config.diagnostics.disable['unused-function'] = true TEST [[ local mt, x function mt:m() function x:m() end end +return mt, x ]] TEST [[ local mt = {} function mt:f() end +return mt +]] + +TEST [[ +local <!mt!> = {} +function mt:f() +end ]] TEST [[ |