Skip to content

Commit 69eb4a6

Browse files
firedfoxMyles Borins
authored and
Myles Borins
committed
tools,doc: fix json for grouped optional params
Current tools/doc/json.js only supports one bracket style for optional params methodName(param0[,param1],param2). Add support to other styles such as methodName(param0,[param1,]param2) or methodName(param0[,param1,param2]) or methodName(param0[,param1[,param2]]). PR-URL: #5977 Fixes: #5976 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Robert Lindstädt <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent a2dd848 commit 69eb4a6

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

tools/doc/json.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,30 @@ function parseSignature(text, sig) {
280280
var params = text.match(paramExpr);
281281
if (!params) return;
282282
params = params[1];
283-
// the [ is irrelevant. ] indicates optionalness.
284-
params = params.replace(/\[/g, '');
285283
params = params.split(/,/);
284+
var optionalLevel = 0;
285+
var optionalCharDict = {'[': 1, ' ': 0, ']': -1};
286286
params.forEach(function(p, i, _) {
287287
p = p.trim();
288288
if (!p) return;
289289
var param = sig.params[i];
290290
var optional = false;
291291
var def;
292-
// [foo] -> optional
293-
if (p.charAt(p.length - 1) === ']') {
294-
optional = true;
295-
p = p.replace(/\]/g, '');
296-
p = p.trim();
292+
293+
// for grouped optional params such as someMethod(a[, b[, c]])
294+
var pos;
295+
for (pos = 0; pos < p.length; pos++) {
296+
if (optionalCharDict[p[pos]] === undefined) { break; }
297+
optionalLevel += optionalCharDict[p[pos]];
298+
}
299+
p = p.substring(pos);
300+
optional = (optionalLevel > 0);
301+
for (pos = p.length - 1; pos >= 0; pos--) {
302+
if (optionalCharDict[p[pos]] === undefined) { break; }
303+
optionalLevel += optionalCharDict[p[pos]];
297304
}
305+
p = p.substring(0, pos + 1);
306+
298307
var eq = p.indexOf('=');
299308
if (eq !== -1) {
300309
def = p.substr(eq + 1);

0 commit comments

Comments
 (0)