22local vm = require ' vm.vm'
33local guide = require ' parser.guide'
44local config = require ' config.config'
5+ local util = require ' utility'
56
67--- @param object vm.node.object
78--- @return string ?
@@ -40,6 +41,51 @@ local function getNodeName(object)
4041 return nil
4142end
4243
44+ --- @param parentName string
45+ --- @param child vm.node.object
46+ --- @param uri uri
47+ --- @return boolean ?
48+ local function checkEnum (parentName , child , uri )
49+ local parentClass = vm .getGlobal (' type' , parentName )
50+ if not parentClass then
51+ return nil
52+ end
53+ for _ , set in ipairs (parentClass :getSets (uri )) do
54+ if set .type == ' doc.enum' then
55+ if child .type ~= ' string'
56+ and child .type ~= ' doc.type.string'
57+ and child .type ~= ' integer'
58+ and child .type ~= ' number'
59+ and child .type ~= ' doc.type.integer' then
60+ return false
61+ end
62+ return util .arrayHas (set ._enums , child [1 ])
63+ end
64+ end
65+
66+ return nil
67+ end
68+
69+ --- @param parent vm.node.object
70+ --- @param child vm.node.object
71+ --- @return boolean
72+ local function checkValue (parent , child )
73+ if parent .type == ' doc.type.integer' then
74+ if child .type == ' integer'
75+ or child .type == ' doc.type.integer'
76+ or child .type == ' number' then
77+ return parent [1 ] == child [1 ]
78+ end
79+ elseif parent .type == ' doc.type.string' then
80+ if child .type == ' string'
81+ or child .type == ' doc.type.string' then
82+ return parent [1 ] == child [1 ]
83+ end
84+ end
85+
86+ return true
87+ end
88+
4389--- @param uri uri
4490--- @param child vm.node | string | vm.node.object
4591--- @param parent vm.node | string | vm.node.object
@@ -121,6 +167,9 @@ function vm.isSubType(uri, child, parent, mark)
121167 end
122168
123169 if childName == parentName then
170+ if not checkValue (parent , child ) then
171+ return false
172+ end
124173 return true
125174 end
126175
@@ -144,6 +193,11 @@ function vm.isSubType(uri, child, parent, mark)
144193 return true
145194 end
146195
196+ local isEnum = checkEnum (parentName , child , uri )
197+ if isEnum ~= nil then
198+ return isEnum
199+ end
200+
147201 -- TODO: check duck
148202 if parentName == ' table' and not guide .isBasicType (childName ) then
149203 return true
0 commit comments