diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/locale/en-US/script.lni | 2 | ||||
-rw-r--r-- | server/locale/zh-CN/script.lni | 2 | ||||
-rw-r--r-- | server/src/constant/DiagnosticDefaultSeverity.lua | 1 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 37 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 25 |
5 files changed, 66 insertions, 1 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index e7f67a09..9c108203 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -4,6 +4,8 @@ DIAG_UNUSED_LOCAL = 'Unused local `{}`.' DIAG_UNDEF_GLOBAL = 'Undefined global `{}`.' DIAG_UNDEF_ENV_CHILD = 'Undefined variable `{}` (overloaded `_ENV` ).' DIAG_UNDEF_FENV_CHILD = 'Undefined variable `{}` (inside module).' +DIAG_GLOBAL_IN_NIL_ENV = 'Invalid global (`_ENV` is `nil`).' +DIAG_GLOBAL_IN_NIL_FENV = 'Invalid global (module environment is `nil`).' DIAG_UNUSED_LABEL = 'Unused label `{}`.' DIAG_UNUSED_FUNCTION = 'Unused functions.' DIAG_UNUSED_VARARG = 'Unused vararg.' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index 51fc6826..b441db5a 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -4,6 +4,8 @@ DIAG_UNUSED_LOCAL = '未使用的局部变量 `{}`。' DIAG_UNDEF_GLOBAL = '未定义的全局变量 `{}`。' DIAG_UNDEF_ENV_CHILD = '未定义的变量 `{}`(重载了 `_ENV` )。' DIAG_UNDEF_FENV_CHILD = '未定义的变量 `{}`(处于模块中)。' +DIAG_GLOBAL_IN_NIL_ENV = '不能使用全局变量(`_ENV`被置为了`nil`)。' +DIAG_GLOBAL_IN_NIL_FENV = '不能使用全局变量(模块被置为了`nil`)。' DIAG_UNUSED_LABEL = '未使用的标签 `{}`。' DIAG_UNUSED_FUNCTION = '未使用的函数。' DIAG_UNUSED_VARARG = '未使用的不定参数。' diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua index 3cd67528..cc26cab2 100644 --- a/server/src/constant/DiagnosticDefaultSeverity.lua +++ b/server/src/constant/DiagnosticDefaultSeverity.lua @@ -2,6 +2,7 @@ return { ['unused-local'] = 'Hint', ['unused-function'] = 'Hint', ['undefined-global'] = 'Warning', + ['global-in-nil-env'] = 'Warning', ['unused-label'] = 'Hint', ['unused-vararg'] = 'Hint', ['trailing-space'] = 'Hint', diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 509fe48c..0e0d85ba 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -553,6 +553,29 @@ function mt:searchUndefinedEnvChild(callback) end) end +function mt:searchGlobalInNilEnv(callback) + self.vm:eachSource(function (source) + if not source:get 'global' then + return + end + local name = source:getName() + if name == '' then + return + end + local parentSource = source:get 'parent' :getSource() + if parentSource.type == 'nil' then + callback(source.start, source.finish, { + { + start = parentSource.start, + finish = parentSource.finish, + uri = self.uri, + } + }) + end + return + end) +end + function mt:checkEmmyClass(source, callback) local class = source:get 'emmy.class' if not class then @@ -919,6 +942,20 @@ return function (vm, lines, uri) } end end) + -- 全局变量不可用(置空了 `_ENV`) + session:doDiagnostics(session.searchGlobalInNilEnv, 'global-in-nil-env', function (related) + if vm.envType == '_ENV' then + return { + message = lang.script.DIAG_GLOBAL_IN_NIL_ENV, + related = related, + } + else + return { + message = lang.script.DIAG_GLOBAL_IN_NIL_FENV, + related = related, + } + end + end) -- 构建表时重复定义field session:doDiagnostics(session.searchDuplicateIndex, 'duplicate-index', function (key, related, type) if type == 'unused' then diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index f0f7096f..555bc0a8 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -1,7 +1,8 @@ -local core = require 'core' +local core = require 'core' local buildVM = require 'vm' local parser = require 'parser' local service = require 'service' +local config = require 'config' rawset(_G, 'TEST', true) @@ -151,6 +152,28 @@ print(1) _ENV = nil ]] +config.config.diagnostics.disable['undefined-env-child'] = true +TEST [[ +_ENV = nil +<!GLOBAL!> = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = nil +local _ = <!GLOBAL!> --> local _ = _ENV.GLOBAL +]] + +TEST [[ +_ENV = {} +GLOBAL = 1 --> _ENV.GLOBAL = 1 +]] + +TEST [[ +_ENV = {} +local _ = GLOBAL --> local _ = _ENV.GLOBAL +]] + +config.config.diagnostics.disable['undefined-env-child'] = nil TEST [[ print() <!('string')!>:sub(1, 1) |