diff options
-rw-r--r-- | .vscode/settings.json | 3 | ||||
-rw-r--r-- | server/locale/en-US/script.lni | 3 | ||||
-rw-r--r-- | server/locale/zh-CN/script.lni | 3 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 51 | ||||
-rw-r--r-- | server/src/vm/global.lua | 1 |
5 files changed, 44 insertions, 17 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json index db5132b3..84d7eb1b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "OUT", "IN", "log", - "ac" + "ac", + "_G" ] } diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index 65d5743e..b66e52c3 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -2,7 +2,8 @@ DIAG_LINE_ONLY_SPACE = 'Line with spaces only.' DIAG_LINE_POST_SPACE = 'Line with postspace.' DIAG_UNUSED_LOCAL = 'Unused local `{}`.' -DIAG_UNDEFINED_GLOBAL = 'Undefined global `{}`.' +DIAG_UNDEF_GLOBAL = 'Undefined global `{}`.' +DIAG_UNDEF_ENV_CHILD = 'Undefined variable `{}` (overloaded `_ENV` ).' DIAG_UNUSED_LABEL = 'Unused label `{}`.' DIAG_REDEFINED_LOCAL = 'Redefined local `{}`.' DIAG_PREVIOUS_CALL = 'Parsed as function call for the previous line. It may be necessary to add a `;` before.' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index 0d9dc6cf..d3a18b36 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -2,7 +2,8 @@ DIAG_LINE_ONLY_SPACE = '只有空格的空行。' DIAG_LINE_POST_SPACE = '后置空格。' DIAG_UNUSED_LOCAL = '未使用的局部变量 `{}`。' -DIAG_UNDEFINED_GLOBAL = '未定义的全局变量 `{}`。' +DIAG_UNDEF_GLOBAL = '未定义的全局变量 `{}`。' +DIAG_UNDEF_ENV_CHILD = '未定义的变量 `{}`(重载了 `_ENV` )。' DIAG_UNUSED_LABEL = '未使用的标签 `{}`。' DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。' DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。' diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index d541e8de..72bcdbe4 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -60,21 +60,11 @@ function mt:searchUndefinedGlobal(callback) if name == '' then return end - local value = source:bindValue() - if not value then + local parent = source:get 'parent' + if not parent then return end - if not value:isGlobal() then - -- 上文重载了 _ENV 的情况 - local ok = value:eachInfo(function (info) - if info.type == 'set' then - return true - end - end) - if ok then - return - end - callback(source.start, source.finish, name) + if not parent:get 'ENV' then return end if definedGlobal[name] then @@ -296,6 +286,32 @@ function mt:doDiagnostics(func, code, callback) end end +function mt:searchUndefinedEnvChild(callback) + self.vm:eachSource(function (source) + if not source:get 'global' then + return + end + local name = source:getName() + if name == '' then + return + end + local parent = source:get 'parent' + if parent:get 'ENV' then + return + end + local ok = parent:eachInfo(function (info) + if info.type == 'set child' and info[1] == name then + return true + end + end) + if ok then + return + end + callback(source.start, source.finish, name) + return + end) +end + return function (vm, lines, uri) local session = setmetatable({ vm = vm, @@ -315,7 +331,7 @@ return function (vm, lines, uri) session:doDiagnostics(session.searchUndefinedGlobal, 'undefined-global', function (key) return { level = DiagnosticSeverity.Warning, - message = lang.script('DIAG_UNDEFINED_GLOBAL', key), + message = lang.script('DIAG_UNDEF_GLOBAL', key), } end) -- 未使用的Label @@ -368,5 +384,12 @@ return function (vm, lines, uri) message = lang.script.DIAG_LOWERCASE_GLOBAL, } end) + -- 未定义的变量(重载了 `_ENV`) + session:doDiagnostics(session.searchUndefinedEnvChild, 'undefined-env-child', function (key) + return { + level = DiagnosticSeverity.Information, + message = lang.script('DIAG_UNDEF_ENV_CHILD', key), + } + end) return session.datas end diff --git a/server/src/vm/global.lua b/server/src/vm/global.lua index 54680fd3..af30ffdd 100644 --- a/server/src/vm/global.lua +++ b/server/src/vm/global.lua @@ -13,6 +13,7 @@ return function (lsp) global = t._G global:markGlobal() + global:set('ENV', true) for k, v in pairs(t) do global:setChild(k, v, sourceMgr.dummy()) end |