From 4c73ab43028792dfb993a999266ada26ec846cc3 Mon Sep 17 00:00:00 2001 From: firedfox Date: Thu, 31 Mar 2016 23:39:02 +0800 Subject: [PATCH] tools,doc: fix json for grouped optional params MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/5977 Fixes: https://github.com/nodejs/node/issues/5976 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Robert Lindstädt Reviewed-By: Roman Reiss --- tools/doc/json.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tools/doc/json.js b/tools/doc/json.js index b2165c15d23c90..3d08026daaabd8 100644 --- a/tools/doc/json.js +++ b/tools/doc/json.js @@ -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);