diff options
Diffstat (limited to 'server-beta/src/core')
5 files changed, 111 insertions, 11 deletions
diff --git a/server-beta/src/core/diagnostics/redefined-local.lua b/server-beta/src/core/diagnostics/redefined-local.lua index b3d19c21..a1b0e77a 100644 --- a/server-beta/src/core/diagnostics/redefined-local.lua +++ b/server-beta/src/core/diagnostics/redefined-local.lua @@ -1,3 +1,32 @@ -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 + guide.eachSourceType(ast.ast, 'local', function (source) + local name = source[1] + if name == '_' + or name == '_ENV' then + return + end + local exist = guide.getLocal(source, name, source.start) + if exist then + callback { + start = source.start, + finish = source.finish, + message = lang.script('DIAG_REDEFINED_LOCAL', name), + related = { + { + start = exist.start, + finish = exist.finish, + uri = uri, + } + }, + } + end + end) end diff --git a/server-beta/src/core/diagnostics/trailing-space.lua b/server-beta/src/core/diagnostics/trailing-space.lua index b3d19c21..ef7fbef1 100644 --- a/server-beta/src/core/diagnostics/trailing-space.lua +++ b/server-beta/src/core/diagnostics/trailing-space.lua @@ -1,3 +1,57 @@ -return function () - +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' + +local function isInString(ast, offset) + local result = false + guide.eachSourceType(ast, 'string', function (source) + if offset >= source.start and offset <= source.finish then + result = true + end + end) + return result +end + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + local lines = files.getLines(uri) + local text = files.getText(uri) + + for i = 1, #lines do + local start = lines[i].start + 1 + local finish = lines[i].finish - 1 + local line = text:sub(start, finish) + if line:find '^[ \t]+[\r\n]*$' then + local offset = guide.offsetOf(lines, i-1, start-1) + if isInString(ast.ast, offset) then + goto NEXT_LINE + end + callback { + start = start, + finish = finish, + message = lang.script.DIAG_LINE_ONLY_SPACE, + } + goto NEXT_LINE + end + + local pos = line:find '[ \t]+[\r\n]*$' + if pos then + start = start + pos - 1 + local offset = guide.offsetOf(lines, i-1, start-1) + if isInString(ast.ast, offset) then + goto NEXT_LINE + end + callback { + start = start, + finish = finish, + message = lang.script.DIAG_LINE_POST_SPACE, + } + goto NEXT_LINE + end + + ::NEXT_LINE:: + end end diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua index 888064dc..6c9abaca 100644 --- a/server-beta/src/core/diagnostics/undefined-global.lua +++ b/server-beta/src/core/diagnostics/undefined-global.lua @@ -1,7 +1,5 @@ local files = require 'files' -local guide = require 'parser.guide' local searcher = require 'searcher' -local define = require 'proto.define' local lang = require 'language' local library = require 'library' local config = require 'config' @@ -12,9 +10,9 @@ return function (uri, callback) return end - -- 先遍历一次所有该文件中的全局变量 + -- 先遍历一次该文件中的全局变量 -- 如果变量有 set 行为,则做标记 - -- 然后再遍历一次,对所有的行为打上标记 + -- 然后再遍历一次,对所有的行为打上相同标记 local hasSet = {} searcher.eachGlobal(ast.ast, function (info) if hasSet[info.source] ~= nil then diff --git a/server-beta/src/core/diagnostics/unused-label.lua b/server-beta/src/core/diagnostics/unused-label.lua index b3d19c21..e6d998ba 100644 --- a/server-beta/src/core/diagnostics/unused-label.lua +++ b/server-beta/src/core/diagnostics/unused-label.lua @@ -1,3 +1,22 @@ -return function () - +local files = require 'files' +local guide = require 'parser.guide' +local define = require 'proto.define' +local lang = require 'language' + +return function (uri, callback) + local ast = files.getAst(uri) + if not ast then + return + end + + guide.eachSourceType(ast.ast, 'label', function (source) + if not source.ref then + callback { + start = source.start, + finish = source.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_UNUSED_LABEL', source[1]), + } + end + end) end diff --git a/server-beta/src/core/diagnostics/unused-local.lua b/server-beta/src/core/diagnostics/unused-local.lua index d110a714..e6052e39 100644 --- a/server-beta/src/core/diagnostics/unused-local.lua +++ b/server-beta/src/core/diagnostics/unused-local.lua @@ -8,7 +8,7 @@ local function hasGet(loc) return false end for _, ref in ipairs(loc.ref) do - if ref.mode == 'get' then + if ref.type == 'getlocal' then return true end end |