summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/diagnostics/undefined-global.lua15
-rw-r--r--script/core/noder.lua15
-rw-r--r--script/parser/guide.lua38
-rw-r--r--test/full/init.lua2
4 files changed, 48 insertions, 22 deletions
diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua
index 549a1922..c53029ba 100644
--- a/script/core/diagnostics/undefined-global.lua
+++ b/script/core/diagnostics/undefined-global.lua
@@ -1,8 +1,10 @@
-local files = require 'files'
-local vm = require 'vm'
-local lang = require 'language'
-local config = require 'config'
-local guide = require 'parser.guide'
+local files = require 'files'
+local vm = require 'vm'
+local lang = require 'language'
+local config = require 'config'
+local guide = require 'parser.guide'
+local noder = require 'core.noder'
+local collector = require 'core.collector'
local requireLike = {
['include'] = true,
@@ -33,7 +35,8 @@ return function (uri, callback)
if node.tag ~= '_ENV' then
return
end
- if #vm.getDefs(src) == 0 then
+ local id = 'def:' .. noder.getID(src)
+ if not collector.has(id) then
local message = lang.script('DIAG_UNDEF_GLOBAL', key)
if requireLike[key:lower()] then
message = ('%s(%s)'):format(message, lang.script('DIAG_REQUIRE_LIKE', key))
diff --git a/script/core/noder.lua b/script/core/noder.lua
index 4c454d66..a7f71d3e 100644
--- a/script/core/noder.lua
+++ b/script/core/noder.lua
@@ -179,6 +179,9 @@ local function getKey(source)
end
local function checkMode(source)
+ if guide.isGlobal(source) then
+ return 'g:'
+ end
if source.type == 'table' then
return 't:'
end
@@ -254,9 +257,6 @@ local function checkMode(source)
end
return id
end
- if guide.isGlobal(source) then
- return 'g:'
- end
if source.type == 'getlocal'
or source.type == 'setlocal' then
source = source.node
@@ -302,15 +302,10 @@ local function getID(source)
if not node then
break
end
- current = node
- if current.special == '_G' then
- for i = index, 2, -1 do
- if IDList[i] == '"_G"' then
- IDList[i] = nil
- end
- end
+ if guide.isGlobal(current) then
break
end
+ current = node
::CONTINUE::
end
if index == 0 then
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 227282bc..85bdec27 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -975,6 +975,10 @@ function m.isGlobal(source)
if source._isGlobal ~= nil then
return source._isGlobal
end
+ if source.special == '_G' then
+ source._isGlobal = true
+ return true
+ end
if source.type == 'setglobal'
or source.type == 'getglobal' then
if source.node and source.node.tag == '_ENV' then
@@ -982,12 +986,36 @@ function m.isGlobal(source)
return true
end
end
- if source.type == 'field' then
- source = source.parent
+ if source.type == 'setfield'
+ or source.type == 'getfield'
+ or source.type == 'setindex'
+ or source.type == 'getindex' then
+ local current = source
+ while current do
+ local node = current.node
+ if not node then
+ break
+ end
+ if node.special == '_G' then
+ source._isGlobal = true
+ return true
+ end
+ if m.getKeyName(node) ~= '_G' then
+ break
+ end
+ current = node
+ end
end
- if source.special == '_G' then
- source._isGlobal = true
- return true
+ if source.type == 'call' then
+ local node = source.node
+ if node.special == 'rawget'
+ or node.special == 'rawset' then
+ if source.args[1] then
+ local isGlobal = source.args[1].special == '_G'
+ source._isGlobal = isGlobal
+ return isGlobal
+ end
+ end
end
source._isGlobal = false
return false
diff --git a/test/full/init.lua b/test/full/init.lua
index b410e3b3..aec361de 100644
--- a/test/full/init.lua
+++ b/test/full/init.lua
@@ -19,11 +19,11 @@ local function startCollectDiagTimes()
DIAGTIMES = {}
end
+startCollectDiagTimes()
require 'full.normal'
require 'full.example'
require 'full.dirty'
require 'full.projects'
-startCollectDiagTimes()
require 'full.self'
for name, time in util.sortPairs(DIAGTIMES, function (k1, k2)