Skip to content

Commit e94fe00

Browse files
committed
tools,doc: fix json result of 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]])
1 parent 82c2996 commit e94fe00

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

tools/doc/json.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,30 @@ function parseSignature(text, sig) {
272272
var params = text.match(paramExpr);
273273
if (!params) return;
274274
params = params[1];
275-
// the [ is irrelevant. ] indicates optionalness.
276-
params = params.replace(/\[/g, '');
277275
params = params.split(/,/);
276+
var optionalLevel = 0;
277+
var optionalCharDict = {'[': 1, ' ': 0, ']': -1};
278278
params.forEach(function(p, i, _) {
279279
p = p.trim();
280280
if (!p) return;
281281
var param = sig.params[i];
282282
var optional = false;
283283
var def;
284-
// [foo] -> optional
285-
if (p.charAt(p.length - 1) === ']') {
286-
optional = true;
287-
p = p.replace(/\]/g, '');
288-
p = p.trim();
284+
285+
// for grouped optional params such as someMethod(a[, b[, c]])
286+
var pos;
287+
for (pos = 0; pos < p.length; pos++) {
288+
if (optionalCharDict[p[pos]] === undefined) { break; }
289+
optionalLevel += optionalCharDict[p[pos]];
290+
}
291+
p = p.substring(pos);
292+
optional = (optionalLevel > 0);
293+
for (pos = p.length - 1; pos >= 0; pos--) {
294+
if (optionalCharDict[p[pos]] === undefined) { break; }
295+
optionalLevel += optionalCharDict[p[pos]];
289296
}
297+
p = p.substring(0, pos + 1);
298+
290299
var eq = p.indexOf('=');
291300
if (eq !== -1) {
292301
def = p.substr(eq + 1);

0 commit comments

Comments
 (0)