summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-03-10 20:11:36 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-03-10 20:11:36 +0800
commit38a2b7f9df15b8737ba6fba27861eba50b2277b6 (patch)
tree5c48cf092b813837801b8ff09e52f99e392fbe84
parentc5170054e2873592c3967d440cb4debb42a09324 (diff)
downloadlua-language-server-38a2b7f9df15b8737ba6fba27861eba50b2277b6.zip
update
-rw-r--r--script/parser/luadoc.lua7
-rw-r--r--script/vm/infer.lua62
-rw-r--r--script/vm/union.lua7
-rw-r--r--test/type_inference/init.lua26
4 files changed, 89 insertions, 13 deletions
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 9b041870..810faa8c 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -638,6 +638,13 @@ function parseType(parent)
end
elseif tp == 'string' then
nextToken()
+ -- compatibility
+ if content:sub(1, 1) == '"'
+ or content:sub(1, 1) == "'" then
+ if content:sub(1, 1) == content:sub(-1, -1) then
+ content = content:sub(2, -2)
+ end
+ end
local typeEnum = {
type = 'doc.type.enum',
start = getStart(),
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index 95a01658..c396f3ec 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -1,5 +1,5 @@
-local util = require 'utility'
-local nodeMgr = require 'vm.node'
+local util = require 'utility'
+local nodeMgr = require 'vm.node'
---@class vm.infer-manager
local m = {}
@@ -33,6 +33,10 @@ local viewNodeMap = util.switch()
return source.name
end
end)
+ : case 'doc.type.name'
+ : call(function (source, options)
+ return source[1]
+ end)
: case 'doc.type.array'
: call(function (source, options)
options['hasClass'] = true
@@ -40,7 +44,7 @@ local viewNodeMap = util.switch()
end)
: case 'doc.type.enum'
: call(function (source, options)
- return source[1]
+ return ('%q'):format(source[1])
end)
: getMap()
@@ -52,12 +56,36 @@ local function viewNode(node, options)
end
end
+local function eraseAlias(node, viewMap)
+ for n in nodeMgr.eachNode(node) do
+ if n.type == 'global' and n.cate == 'type' then
+ for _, set in ipairs(n:getSets()) do
+ if set.type == 'doc.alias' then
+ for _, ext in ipairs(set.extends.types) do
+ local view = viewNode(ext, {})
+ if view and view ~= n.name then
+ viewMap[view] = nil
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
---@param source parser.object
-function m.viewType(source)
+---@return table<string, boolean>
+function m.getViews(source)
local compiler = require 'vm.compiler'
local node = compiler.compileNode(source)
- local views = {}
- local options = {}
+ if node.type == 'union' and node.lastViews then
+ return node.lastViews
+ end
+ local views = {}
+ local options = {}
+ if node.type == 'union' then
+ node.lastViews = views
+ end
for n in nodeMgr.eachNode(node) do
local view = viewNode(n, options)
if view then
@@ -70,13 +98,25 @@ function m.viewType(source)
if options['hasTable'] and not options['hasClass'] then
views['table'] = true
end
+ if options['hasClass'] then
+ eraseAlias(node, views)
+ end
+ return views
+end
+
+---@param source parser.object
+---@return string
+function m.viewType(source)
+ local views = m.getViews(source)
+ if not next(views) then
+ return 'unknown'
+ end
+
local array = {}
for view in pairs(views) do
array[#array+1] = view
end
- if #array == 0 then
- return 'unknown'
- end
+
table.sort(array, function (a, b)
local sa = inferSorted[a] or 0
local sb = inferSorted[b] or 0
@@ -85,7 +125,9 @@ function m.viewType(source)
end
return sa < sb
end)
- return table.concat(array, '|')
+ local view = table.concat(array, '|')
+
+ return view
end
return m
diff --git a/script/vm/union.lua b/script/vm/union.lua
index 1a7d75f4..9f0cb767 100644
--- a/script/vm/union.lua
+++ b/script/vm/union.lua
@@ -2,9 +2,10 @@ local localMgr = require 'vm.local-manager'
---@class vm.node.union
local mt = {}
-mt.__index = mt
-mt.type = 'union'
-mt.falsy = nil
+mt.__index = mt
+mt.type = 'union'
+mt.falsy = nil
+mt.lastViews = nil
---@param node vm.node
function mt:merge(node)
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 5ebe5778..37aa24b0 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -381,10 +381,36 @@ local <?x?>
]]
TEST '"enum1"|"enum2"' [[
+---@type 'enum1' | 'enum2'
+local <?x?>
+]]
+
+TEST '"enum1"|"enum2"' [[
---@type '"enum1"' | '"enum2"'
local <?x?>
]]
+TEST 'A' [[
+---@alias A 'enum1' | 'enum2'
+
+---@type A
+local <?x?>
+]]
+
+TEST 'A' [[
+---@alias A 'enum1' | 'enum2' | A
+
+---@type A
+local <?x?>
+]]
+
+TEST 'A' [[
+---@alias A 'enum1' | 'enum2' | B
+
+---@type A
+local <?x?>
+]]
+
TEST 'fun()' [[
---@type fun()
local <?x?>