diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 13:59:19 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 13:59:19 +0800 |
commit | 91b0bd8f72687f94342f384a916638fc87f1fabb (patch) | |
tree | 0b726adf57c34c156c7df5c78c56a970bbe97f33 | |
parent | 743d1a3ea8aa964d8aabf13e597fef21bfb361ec (diff) | |
download | lua-language-server-91b0bd8f72687f94342f384a916638fc87f1fabb.zip |
未定义的全局变量(之后需要排除内置全局变量)
-rw-r--r-- | server/src/matcher/diagnostics.lua | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/server/src/matcher/diagnostics.lua b/server/src/matcher/diagnostics.lua index ffdab80e..94dbebc2 100644 --- a/server/src/matcher/diagnostics.lua +++ b/server/src/matcher/diagnostics.lua @@ -29,6 +29,30 @@ local function searchUnusedLocals(results, callback) end end +local function searchUndefinedGlobal(results, callback) + for _, var in ipairs(results.vars) do + if var.type ~= 'field' then + goto NEXT_VAR + end + if var.parent.key ~= '_ENV' + and var.parent.key ~= '_G' + then + goto NEXT_VAR + end + for _, info in ipairs(var) do + if info.type == 'set' then + goto NEXT_VAR + end + end + for _, info in ipairs(var) do + if info.type == 'get' then + callback(info.source.start, info.source.finish, var.key) + end + end + ::NEXT_VAR:: + end +end + return function (ast, results, lines) local datas = {} -- 搜索未使用的局部变量 @@ -41,5 +65,15 @@ return function (ast, results, lines) message = 'Unused local', -- LOCALE } end) + -- 搜索读取未定义全局变量 + searchUndefinedGlobal(results, function (start, finish, code) + datas[#datas+1] = { + start = start, + finish = finish, + level = 'Warning', + code = code, + message = 'Undefined global', -- LOCALE + } + end) return datas end |