Skip to content

Commit 4de84f3

Browse files
committed
fix: improve type narrow with literal alias param during completion
1 parent 3894968 commit 4de84f3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

script/core/signature.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ local function isEventNotMatch(call, src)
114114
end
115115
local eventLiteral = event.extends.types[1] and guide.getLiteral(event.extends.types[1])
116116
if eventLiteral == nil then
117-
return false
117+
-- extra checking when function param is not pure literal
118+
-- eg: it maybe an alias type with literal values
119+
local eventMap = vm.getLiterals(event.extends.types[1])
120+
if not eventMap then
121+
return false
122+
end
123+
return not eventMap[literal]
118124
end
119125
return eventLiteral ~= literal
120126
end

script/vm/compiler.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,26 @@ local function compileCallArgNode(arg, call, callNode, fixIndex, myIndex)
962962
local function dealDocFunc(n)
963963
local myEvent
964964
if n.args[eventIndex] then
965+
if eventMap and myIndex > eventIndex then
966+
-- if call param has literal types, then also check if function def param has literal types
967+
-- 1. has no literal values => not enough info, thus allowed by default
968+
-- 2. has literal values and >= 1 matches call param's literal types => allowed
969+
-- 3. has literal values but none matches call param's literal types => filtered
970+
local myEventMap = vm.getLiterals(n.args[eventIndex])
971+
if myEventMap then
972+
local found = false
973+
for k in pairs(eventMap) do
974+
if myEventMap[k] then
975+
-- there is a matching literal
976+
found = true
977+
break
978+
end
979+
end
980+
if not found then
981+
return
982+
end
983+
end
984+
end
965985
local argNode = vm.compileNode(n.args[eventIndex])
966986
myEvent = argNode:get(1)
967987
end

0 commit comments

Comments
 (0)