Browse Source

make sure to handle if statements

formatter
Brett Langdon 11 years ago
parent
commit
5cdbbf0b6c
3 changed files with 68 additions and 16 deletions
  1. +39
    -16
      lib/index.js
  2. +16
    -0
      test/fixture/test2.js
  3. +13
    -0
      test/index.js

+ 39
- 16
lib/index.js View File

@ -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);
}


+ 16
- 0
test/fixture/test2.js View File

@ -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;
};
}

+ 13
- 0
test/index.js View File

@ -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']);
});
});
});

Loading…
Cancel
Save