1919--- @param func parser.object
2020--- @return integer min
2121--- @return number max
22+ --- @return integer def
2223function vm .countParamsOfFunction (func )
2324 local min = 0
2425 local max = 0
26+ local def = 0
2527 if func .type == ' function' then
2628 if func .args then
2729 max = # func .args
30+ def = max
2831 for i = # func .args , 1 , - 1 do
2932 local arg = func .args [i ]
3033 if arg .type == ' ...' then
@@ -44,6 +47,7 @@ function vm.countParamsOfFunction(func)
4447 if func .type == ' doc.type.function' then
4548 if func .args then
4649 max = # func .args
50+ def = max
4751 for i = # func .args , 1 , - 1 do
4852 local arg = func .args [i ]
4953 if arg .name and arg .name [1 ] == ' ...' then
@@ -55,56 +59,58 @@ function vm.countParamsOfFunction(func)
5559 end
5660 end
5761 end
58- return min , max
62+ return min , max , def
5963end
6064
6165--- @param node vm.node
6266--- @return integer min
6367--- @return number max
68+ --- @return integer def
6469function vm .countParamsOfNode (node )
65- local min , max
70+ local min , max , def
6671 for n in node :eachObject () do
6772 if n .type == ' function'
6873 or n .type == ' doc.type.function' then
6974 --- @cast n parser.object
70- local fmin , fmax = vm .countParamsOfFunction (n )
75+ local fmin , fmax , fdef = vm .countParamsOfFunction (n )
7176 if not min or fmin < min then
7277 min = fmin
7378 end
7479 if not max or fmax > max then
7580 max = fmax
7681 end
82+ if not def or fdef > def then
83+ def = fdef
84+ end
7785 end
7886 end
79- return min or 0 , max or math.huge
87+ return min or 0 , max or math.huge , def or 0
8088end
8189
8290--- @param func parser.object
8391--- @param onlyDoc ? boolean
8492--- @param mark ? table
8593--- @return integer min
8694--- @return number max
95+ --- @return integer def
8796function vm .countReturnsOfFunction (func , onlyDoc , mark )
8897 if func .type == ' function' then
89- --- @type integer ?
90- local min
91- --- @type number ?
92- local max
98+ --- @type integer ?, number ?, integer ?
99+ local min , max , def
93100 local hasDocReturn
94101 if func .bindDocs then
95102 local lastReturn
96103 local n = 0
97- --- @type integer ?
98- local dmin
99- --- @type number ?
100- local dmax
104+ --- @type integer ?, number ?, integer ?
105+ local dmin , dmax , ddef
101106 for _ , doc in ipairs (func .bindDocs ) do
102107 if doc .type == ' doc.return' then
103108 hasDocReturn = true
104109 for _ , ret in ipairs (doc .returns ) do
105110 n = n + 1
106111 lastReturn = ret
107112 dmax = n
113+ ddef = n
108114 if (not ret .name or ret .name [1 ] ~= ' ...' )
109115 and not vm .compileNode (ret ):isNullable () then
110116 dmin = n
@@ -123,19 +129,25 @@ function vm.countReturnsOfFunction(func, onlyDoc, mark)
123129 if dmax and (not max or (dmax > max )) then
124130 max = dmax
125131 end
132+ if ddef and (not def or (ddef > def )) then
133+ def = ddef
134+ end
126135 end
127136 if not onlyDoc and not hasDocReturn and func .returns then
128137 for _ , ret in ipairs (func .returns ) do
129- local rmin , rmax = vm .countList (ret , mark )
138+ local rmin , rmax , ddef = vm .countList (ret , mark )
130139 if not min or rmin < min then
131140 min = rmin
132141 end
133142 if not max or rmax > max then
134143 max = rmax
135144 end
145+ if not def or ddef > def then
146+ def = ddef
147+ end
136148 end
137149 end
138- return min or 0 , max or math.huge
150+ return min or 0 , max or math.huge , def or 0
139151 end
140152 if func .type == ' doc.type.function' then
141153 return vm .countList (func .returns )
@@ -147,52 +159,55 @@ end
147159--- @param mark ? table
148160--- @return integer min
149161--- @return number max
162+ --- @return integer def
150163function vm .countReturnsOfCall (func , args , mark )
151164 local funcs = vm .getMatchedFunctions (func , args , mark )
152- --- @type integer ?
153- local min
154- --- @type number ?
155- local max
165+ --- @type integer ?, number ?, integer ?
166+ local min , max , def
156167 for _ , f in ipairs (funcs ) do
157- local rmin , rmax = vm .countReturnsOfFunction (f , false , mark )
168+ local rmin , rmax , rdef = vm .countReturnsOfFunction (f , false , mark )
158169 if not min or rmin < min then
159170 min = rmin
160171 end
161172 if not max or rmax > max then
162173 max = rmax
163174 end
175+ if not def or rdef > def then
176+ def = rdef
177+ end
164178 end
165- return min or 0 , max or math.huge
179+ return min or 0 , max or math.huge , def or 0
166180end
167181
168182--- @param list parser.object[] ?
169183--- @param mark ? table
170184--- @return integer min
171185--- @return number max
186+ --- @return integer def
172187function vm .countList (list , mark )
173188 if not list then
174- return 0 , 0
189+ return 0 , 0 , 0
175190 end
176191 local lastArg = list [# list ]
177192 if not lastArg then
178- return 0 , 0
193+ return 0 , 0 , 0
179194 end
180195 if lastArg .type == ' ...'
181196 or lastArg .type == ' varargs' then
182- return # list - 1 , math.huge
197+ return # list - 1 , math.huge , # list
183198 end
184199 if lastArg .type == ' call' then
185200 if not mark then
186201 mark = {}
187202 end
188203 if mark [lastArg ] then
189- return # list - 1 , math.huge
204+ return # list - 1 , math.huge , # list
190205 end
191206 mark [lastArg ] = true
192- local rmin , rmax = vm .countReturnsOfCall (lastArg .node , lastArg .args , mark )
193- return # list - 1 + rmin , # list - 1 + rmax
207+ local rmin , rmax , rdef = vm .countReturnsOfCall (lastArg .node , lastArg .args , mark )
208+ return # list - 1 + rmin , # list - 1 + rmax , # list - 1 + rdef
194209 end
195- return # list , # list
210+ return # list , # list , # list
196211end
197212
198213--- @param func parser.object
0 commit comments