From 0414a4e0179c158dc66d46e22d2c7cb2c3b3608e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= <sumneko@hotmail.com>
Date: Fri, 7 Dec 2018 11:57:21 +0800
Subject: =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 server/libs/lua53/table.lni     |  5 ++++
 server/src/matcher/find_lib.lua | 13 +++++++++--
 server/test/find_lib/init.lua   | 51 +++++++++++++++++++++++++++++++++++++++++
 server/test/main.lua            |  1 +
 4 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 server/test/find_lib/init.lua

(limited to 'server')

diff --git a/server/libs/lua53/table.lni b/server/libs/lua53/table.lni
index 5cb9a951..94e60664 100644
--- a/server/libs/lua53/table.lni
+++ b/server/libs/lua53/table.lni
@@ -3,3 +3,8 @@ type = 'function'
 
 [table]
 type = 'table'
+[[.source]]
+type = 'global'
+[[.source]]
+type = 'library'
+name = 'table'
diff --git a/server/src/matcher/find_lib.lua b/server/src/matcher/find_lib.lua
index d4cb6bf2..84cf44c5 100644
--- a/server/src/matcher/find_lib.lua
+++ b/server/src/matcher/find_lib.lua
@@ -122,6 +122,15 @@ local function checkLib(var, name, lib)
     if not lib.source then
         return checkLibAsGlobal(var, name)
     end
+    for _, source in ipairs(lib.source) do
+        if source.type == 'global' then
+            local fullkey = checkLibAsGlobal(var, name)
+            if fullkey then
+                return fullkey
+            end
+        end
+    end
+    return nil
 end
 
 local function findLib(var, libs)
@@ -136,6 +145,6 @@ end
 
 return function (var)
     local libs = getLibs()
-    local lib, key = findLib(var, libs)
-    return lib, key
+    local lib, fullkey = findLib(var, libs)
+    return lib, fullkey
 end
diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua
new file mode 100644
index 00000000..29297bcf
--- /dev/null
+++ b/server/test/find_lib/init.lua
@@ -0,0 +1,51 @@
+local matcher    = require 'matcher'
+local parser     = require 'parser'
+local findResult = require 'matcher.find_result'
+local findLib    = require 'matcher.find_lib'
+
+rawset(_G, 'TEST', true)
+
+function TEST(fullkey)
+    return function (script)
+        local start  = script:find('<?', 1, true)
+        local finish = script:find('?>', 1, true)
+        local pos = (start + finish) // 2 + 1
+        local new_script = script:gsub('<[!?]', '  '):gsub('[!?]>', '  ')
+        local ast = parser:ast(new_script)
+        assert(ast)
+        local results = matcher.compile(ast)
+        assert(results)
+        local result = findResult(results, pos)
+        assert(result)
+        assert(result.type == 'var')
+        local var = result.var
+        assert(var)
+        local _, name = findLib(var)
+        assert(name)
+        assert(name == fullkey)
+    end
+end
+
+TEST 'require' [[
+<?require?> 'xxx'
+]]
+
+TEST 'require' [[
+local <?req?> = require
+]]
+
+TEST 'require' [[
+local req = require
+local t = {
+    xx = req,
+}
+t[<?'xx'?>]()
+]]
+
+TEST 'table' [[
+<?table?>.unpack()
+]]
+
+TEST 'table' [[
+local <?xx?> = require 'table'
+]]
diff --git a/server/test/main.lua b/server/test/main.lua
index 58394989..9b6e2c02 100644
--- a/server/test/main.lua
+++ b/server/test/main.lua
@@ -21,6 +21,7 @@ local function main()
     end
 
     test 'definition'
+    test 'find_lib'
 
     print('测试完成')
 end
-- 
cgit v1.2.3