diff options
Diffstat (limited to 'server-beta')
-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 | ||||
-rw-r--r-- | server-beta/test/diagnostics/init.lua | 2 |
4 files changed, 155 insertions, 7 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 diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua index 1227c469..d8724375 100644 --- a/server-beta/test/diagnostics/init.lua +++ b/server-beta/test/diagnostics/init.lua @@ -397,7 +397,7 @@ X = table[<!x!>] TEST [[ return { - <!x = 1!>, + <!x!> = 1, y = 2, <!x!> = 3, } |