diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-05 21:06:01 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-05 21:06:01 +0800 |
commit | 511be388f88af275d2c1ac6bef0c8964a376b427 (patch) | |
tree | edb9b5237aaf835833e7e162dab9551d1b48e23c /server-beta/src | |
parent | 12eecd0df7d2c577d56b5e81e393963a614dc8e7 (diff) | |
download | lua-language-server-511be388f88af275d2c1ac6bef0c8964a376b427.zip |
更新诊断实现
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/diagnostics/duplicate-index.lua | 63 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/empty-block.lua | 50 | ||||
-rw-r--r-- | server-beta/src/core/diagnostics/lowercase-global.lua | 47 |
3 files changed, 154 insertions, 6 deletions
diff --git a/server-beta/src/core/diagnostics/duplicate-index.lua b/server-beta/src/core/diagnostics/duplicate-index.lua index b3d19c21..8ebe0dee 100644 --- a/server-beta/src/core/diagnostics/duplicate-index.lua +++ b/server-beta/src/core/diagnostics/duplicate-index.lua @@ -1,3 +1,62 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local define = require 'proto.define' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + local mark = {} + guide.eachSourceType(ast.ast, 'table', function (source) + for _, obj in ipairs(source) do + if obj.type == 'tablefield' + or obj.type == 'tableindex' then + local name = guide.getKeyName(obj) + if name then + if not mark[name] then + mark[name] = {} + end + mark[name][#mark[name]+1] = obj.field or obj.index + end + end + end + end) + + for name, defs in pairs(mark) do + local sname = name:match '^.|(.+)$' + if #defs > 1 and sname then + local related = {} + for i = 1, #defs do + local def = defs[i] + related[i] = { + start = def.start, + finish = def.finish, + uri = uri, + } + end + for i = 1, #defs - 1 do + local def = defs[i] + callback { + start = def.start, + finish = def.finish, + related = related, + message = lang.script('DIAG_DUPLICATE_INDEX', sname), + level = define.DiagnosticSeverity.Hint, + tags = { define.DiagnosticTag.Unnecessary }, + } + end + for i = #defs, #defs do + local def = defs[i] + callback { + start = def.start, + finish = def.finish, + related = related, + message = lang.script('DIAG_DUPLICATE_INDEX', sname), + } + end + end + end end diff --git a/server-beta/src/core/diagnostics/empty-block.lua b/server-beta/src/core/diagnostics/empty-block.lua index b3d19c21..2024f4e3 100644 --- a/server-beta/src/core/diagnostics/empty-block.lua +++ b/server-beta/src/core/diagnostics/empty-block.lua @@ -1,3 +1,49 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local define = require 'proto.define' + +-- 检查空代码块 +-- 但是排除忙等待(repeat/while) +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + guide.eachSourceType(ast.ast, 'if', function (source) + for _, block in ipairs(source) do + if #block > 0 then + return + end + end + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script.DIAG_EMPTY_BLOCK, + } + end) + guide.eachSourceType(ast.ast, 'loop', function (source) + if #source > 0 then + return + end + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script.DIAG_EMPTY_BLOCK, + } + end) + guide.eachSourceType(ast.ast, 'in', function (source) + if #source > 0 then + return + end + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script.DIAG_EMPTY_BLOCK, + } + end) end diff --git a/server-beta/src/core/diagnostics/lowercase-global.lua b/server-beta/src/core/diagnostics/lowercase-global.lua index b3d19c21..87a12190 100644 --- a/server-beta/src/core/diagnostics/lowercase-global.lua +++ b/server-beta/src/core/diagnostics/lowercase-global.lua @@ -1,3 +1,46 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local lang = require 'language' +local config = require 'config' +local library = require 'library' + +-- 不允许定义首字母小写的全局变量(很可能是拼错或者漏删) +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + local definedGlobal = {} + for name in pairs(config.config.diagnostics.globals) do + definedGlobal[name] = true + end + for name in pairs(library.global) do + definedGlobal[name] = true + end + + guide.eachSourceType(ast.ast, 'setglobal', function (source) + local name = guide.getKeyName(source) + if not name then + return + end + local sname = name:match '^s|(.+)$' + if not sname then + return + end + if definedGlobal[sname] then + return + end + local first = sname:match '%w' + if not first then + return + end + if first:match '%l' then + callback { + start = source.start, + finish = source.finish, + message = lang.script.DIAG_LOWERCASE_GLOBAL, + } + end + end) end |