summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/diagnostics/undefined-field.lua20
-rw-r--r--test/diagnostics/init.lua3
2 files changed, 23 insertions, 0 deletions
diff --git a/script/core/diagnostics/undefined-field.lua b/script/core/diagnostics/undefined-field.lua
index e5e55f14..d4a0fe78 100644
--- a/script/core/diagnostics/undefined-field.lua
+++ b/script/core/diagnostics/undefined-field.lua
@@ -11,11 +11,25 @@ return function (uri, callback)
return
end
+ local function is_G(src)
+ if not src then
+ return false
+ end
+ while src.node ~= nil and src.type == 'getfield' and src.field[1] == '_G' do
+ src = src.node
+ end
+ return src.type == 'getglobal' and src.special == '_G'
+ end
+
local function checkUndefinedField(src)
local fieldName = guide.getKeyName(src)
+ if is_G(src)then
+ return
+ end
local refs = vm.getFields(src.node, 0)
local fields = {}
+ local empty = true
for _, ref in ipairs(refs) do
if ref.type == 'getfield' or ref.type == 'getmethod' then
goto CONTINUE
@@ -25,9 +39,15 @@ return function (uri, callback)
goto CONTINUE
end
fields[name] = true
+ empty = false
::CONTINUE::
end
+ -- 没找到任何 field,跳过检查
+ if empty then
+ return
+ end
+
if not fields[fieldName] then
local message = lang.script('DIAG_UNDEF_FIELD', fieldName)
callback {
diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua
index 0e932ede..9c934bd5 100644
--- a/test/diagnostics/init.lua
+++ b/test/diagnostics/init.lua
@@ -842,4 +842,7 @@ print(<!v.field3!> + 1)
print(v:method1())
print(v.method2())
print(<!v:method3!>())
+
+local v1 = {}
+print(v1.abc)
]]