Skip to content

Commit

Permalink
tools,doc: fix json for grouped optional params
Browse files Browse the repository at this point in the history
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 <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Lindstädt <robert.lindstaedt@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
  • Loading branch information
firedfox authored and Myles Borins committed Apr 20, 2016
1 parent a2dd848 commit 69eb4a6
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions tools/doc/json.js
Expand Up @@ -280,21 +280,30 @@ function parseSignature(text, sig) {
var params = text.match(paramExpr);
if (!params) return;
params = params[1];
// the [ is irrelevant. ] indicates optionalness.
params = params.replace(/\[/g, '');
params = params.split(/,/);
var optionalLevel = 0;
var optionalCharDict = {'[': 1, ' ': 0, ']': -1};
params.forEach(function(p, i, _) {
p = p.trim();
if (!p) return;
var param = sig.params[i];
var optional = false;
var def;
// [foo] -> optional
if (p.charAt(p.length - 1) === ']') {
optional = true;
p = p.replace(/\]/g, '');
p = p.trim();

// for grouped optional params such as someMethod(a[, b[, c]])
var pos;
for (pos = 0; pos < p.length; pos++) {
if (optionalCharDict[p[pos]] === undefined) { break; }
optionalLevel += optionalCharDict[p[pos]];
}
p = p.substring(pos);
optional = (optionalLevel > 0);
for (pos = p.length - 1; pos >= 0; pos--) {
if (optionalCharDict[p[pos]] === undefined) { break; }
optionalLevel += optionalCharDict[p[pos]];
}
p = p.substring(0, pos + 1);

var eq = p.indexOf('=');
if (eq !== -1) {
def = p.substr(eq + 1);
Expand Down

0 comments on commit 69eb4a6

Please sign in to comment.