From 5cdbbf0b6c979e5d3fce3fc906ca9662f86083b6 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sun, 25 Jan 2015 20:50:05 -0500 Subject: [PATCH] make sure to handle if statements --- lib/index.js | 55 ++++++++++++++++++++++++++++++------------- test/fixture/test2.js | 16 +++++++++++++ test/index.js | 13 ++++++++++ 3 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 test/fixture/test2.js diff --git a/lib/index.js b/lib/index.js index deffa3d..a2c3f5e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,31 +9,51 @@ var mapName = function(elm){ var findReturns = function(expr){ var returns = [] - expr.body.forEach(function(expr){ - if(expr.type === 'ReturnStatement'){ - if(expr.argument.type === 'Literal'){ - returns.push(expr.argument.value); - } else if(expr.argument.type === 'ObjectExpression'){ - returns.push('Object'); - } else if(expr.argument.type === 'ArrayExpression'){ - returns.push('Array'); - } else if(expr.argument.type === 'FunctionExpression'){ - returns.push('Function'); + if(expr.type === 'BlockStatement'){ + expr.body.forEach(function(expr){ + if(expr.type === 'ReturnStatement'){ + if(expr.argument.type === 'Literal'){ + returns.push(expr.argument.value); + } else if(expr.argument.type === 'ObjectExpression'){ + returns.push('Object'); + } else if(expr.argument.type === 'ArrayExpression'){ + returns.push('Array'); + } else if(expr.argument.type === 'FunctionExpression'){ + returns.push('Function'); + } else if(expr.argument.type === 'Identifier'){ + returns.push(expr.argument.name); + } + } else if(expr.type === 'IfStatement'){ + returns = returns.concat(findReturns(expr)); } + }); + } else if(expr.type === 'IfStatement'){ + returns = returns.concat(findReturns(expr.consequent)); + if(expr.alternate){ + returns = returns.concat(findReturns(expr.alternate)); } - }); + } return returns; }; var findRaises = function(expr){ var raises = [] - expr.body.forEach(function(expr){ - if(expr.type === 'ThrowStatement'){ - if(expr.argument.type === 'NewExpression'){ - raises.push(expr.argument.callee.name); + if(expr.type === 'BlockStatement'){ + expr.body.forEach(function(expr){ + if(expr.type === 'ThrowStatement'){ + if(expr.argument.type === 'NewExpression'){ + raises.push(expr.argument.callee.name); + } + } else if(expr.type === 'IfStatement'){ + raises = raises.concat(findRaises(expr)); } + }); + } else if(expr.type === 'IfStatement'){ + raises = raises.concat(findRaises(expr.consequent)); + if(expr.alternate){ + raises = raises.concat(findRaises(expr.alternate)); } - }); + } return raises; }; @@ -87,6 +107,9 @@ var parseExpressions = function(expr){ returns: findReturns(expr.init.body), raises: findRaises(expr.init.body), }; + } else if(expr.type === 'IfStatement'){ + _parse(expr.consequent); + _parse(expr.alternate); } else { _parse(expr.expression || expr.callee || expr.body || expr.declarations); } diff --git a/test/fixture/test2.js b/test/fixture/test2.js new file mode 100644 index 0000000..fffe8b4 --- /dev/null +++ b/test/fixture/test2.js @@ -0,0 +1,16 @@ +/* + * This function is super cool and does all sorts of cool stuffs + */ +function some(cool, stuff){ + if(typeof cool === undefined || typeof stuff === undefined){ + throw new Exception('must provide "cool" or "stuff" parameter'); + } + + if(cool > stuff){ + return stuff; + } else if(stuff > cool){ + return cool; + } else { + return null; + }; +} diff --git a/test/index.js b/test/index.js index f71910f..4cf6252 100644 --- a/test/index.js +++ b/test/index.js @@ -59,5 +59,18 @@ describe('docast', function(){ assert.deepEqual(class2_method1.returns, []); assert.deepEqual(class2_method1.raises, []); }); + + it('should properly parse ./fixture/test2.js', function(){ + var comments = docast.parse(__dirname + '/fixture/test2.js'); + + assert.equal(comments.length, 1); + + var some = comments[0]; + assert.ok(~some.doc.indexOf('This function is super cool and does all sorts of cool stuffs')); + assert.equal(some.name, 'some'); + assert.deepEqual(some.params, ['cool', 'stuff']); + assert.deepEqual(some.returns, ['stuff', 'cool', null]); + assert.deepEqual(some.raises, ['Exception']); + }); }); });