19
19
--- @param func parser.object
20
20
--- @return integer min
21
21
--- @return number max
22
+ --- @return integer def
22
23
function vm .countParamsOfFunction (func )
23
24
local min = 0
24
25
local max = 0
26
+ local def = 0
25
27
if func .type == ' function' then
26
28
if func .args then
27
29
max = # func .args
30
+ def = max
28
31
for i = # func .args , 1 , - 1 do
29
32
local arg = func .args [i ]
30
33
if arg .type == ' ...' then
@@ -44,6 +47,7 @@ function vm.countParamsOfFunction(func)
44
47
if func .type == ' doc.type.function' then
45
48
if func .args then
46
49
max = # func .args
50
+ def = max
47
51
for i = # func .args , 1 , - 1 do
48
52
local arg = func .args [i ]
49
53
if arg .name and arg .name [1 ] == ' ...' then
@@ -55,56 +59,58 @@ function vm.countParamsOfFunction(func)
55
59
end
56
60
end
57
61
end
58
- return min , max
62
+ return min , max , def
59
63
end
60
64
61
65
--- @param node vm.node
62
66
--- @return integer min
63
67
--- @return number max
68
+ --- @return integer def
64
69
function vm .countParamsOfNode (node )
65
- local min , max
70
+ local min , max , def
66
71
for n in node :eachObject () do
67
72
if n .type == ' function'
68
73
or n .type == ' doc.type.function' then
69
74
--- @cast n parser.object
70
- local fmin , fmax = vm .countParamsOfFunction (n )
75
+ local fmin , fmax , fdef = vm .countParamsOfFunction (n )
71
76
if not min or fmin < min then
72
77
min = fmin
73
78
end
74
79
if not max or fmax > max then
75
80
max = fmax
76
81
end
82
+ if not def or fdef > def then
83
+ def = fdef
84
+ end
77
85
end
78
86
end
79
- return min or 0 , max or math.huge
87
+ return min or 0 , max or math.huge , def or 0
80
88
end
81
89
82
90
--- @param func parser.object
83
91
--- @param onlyDoc ? boolean
84
92
--- @param mark ? table
85
93
--- @return integer min
86
94
--- @return number max
95
+ --- @return integer def
87
96
function vm .countReturnsOfFunction (func , onlyDoc , mark )
88
97
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
93
100
local hasDocReturn
94
101
if func .bindDocs then
95
102
local lastReturn
96
103
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
101
106
for _ , doc in ipairs (func .bindDocs ) do
102
107
if doc .type == ' doc.return' then
103
108
hasDocReturn = true
104
109
for _ , ret in ipairs (doc .returns ) do
105
110
n = n + 1
106
111
lastReturn = ret
107
112
dmax = n
113
+ ddef = n
108
114
if (not ret .name or ret .name [1 ] ~= ' ...' )
109
115
and not vm .compileNode (ret ):isNullable () then
110
116
dmin = n
@@ -123,19 +129,25 @@ function vm.countReturnsOfFunction(func, onlyDoc, mark)
123
129
if dmax and (not max or (dmax > max )) then
124
130
max = dmax
125
131
end
132
+ if ddef and (not def or (ddef > def )) then
133
+ def = ddef
134
+ end
126
135
end
127
136
if not onlyDoc and not hasDocReturn and func .returns then
128
137
for _ , ret in ipairs (func .returns ) do
129
- local rmin , rmax = vm .countList (ret , mark )
138
+ local rmin , rmax , ddef = vm .countList (ret , mark )
130
139
if not min or rmin < min then
131
140
min = rmin
132
141
end
133
142
if not max or rmax > max then
134
143
max = rmax
135
144
end
145
+ if not def or ddef > def then
146
+ def = ddef
147
+ end
136
148
end
137
149
end
138
- return min or 0 , max or math.huge
150
+ return min or 0 , max or math.huge , def or 0
139
151
end
140
152
if func .type == ' doc.type.function' then
141
153
return vm .countList (func .returns )
@@ -147,52 +159,55 @@ end
147
159
--- @param mark ? table
148
160
--- @return integer min
149
161
--- @return number max
162
+ --- @return integer def
150
163
function vm .countReturnsOfCall (func , args , mark )
151
164
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
156
167
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 )
158
169
if not min or rmin < min then
159
170
min = rmin
160
171
end
161
172
if not max or rmax > max then
162
173
max = rmax
163
174
end
175
+ if not def or rdef > def then
176
+ def = rdef
177
+ end
164
178
end
165
- return min or 0 , max or math.huge
179
+ return min or 0 , max or math.huge , def or 0
166
180
end
167
181
168
182
--- @param list parser.object[] ?
169
183
--- @param mark ? table
170
184
--- @return integer min
171
185
--- @return number max
186
+ --- @return integer def
172
187
function vm .countList (list , mark )
173
188
if not list then
174
- return 0 , 0
189
+ return 0 , 0 , 0
175
190
end
176
191
local lastArg = list [# list ]
177
192
if not lastArg then
178
- return 0 , 0
193
+ return 0 , 0 , 0
179
194
end
180
195
if lastArg .type == ' ...'
181
196
or lastArg .type == ' varargs' then
182
- return # list - 1 , math.huge
197
+ return # list - 1 , math.huge , # list
183
198
end
184
199
if lastArg .type == ' call' then
185
200
if not mark then
186
201
mark = {}
187
202
end
188
203
if mark [lastArg ] then
189
- return # list - 1 , math.huge
204
+ return # list - 1 , math.huge , # list
190
205
end
191
206
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
194
209
end
195
- return # list , # list
210
+ return # list , # list , # list
196
211
end
197
212
198
213
--- @param func parser.object
0 commit comments