annotation.js 955 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var annotate = function() {
  2. var args = Array.prototype.slice.call(arguments);
  3. var fn = args.pop();
  4. fn.$inject = args;
  5. return fn;
  6. };
  7. // Current limitations:
  8. // - can't put into "function arg" comments
  9. // function /* (no parenthesis like this) */ (){}
  10. // function abc( /* xx (no parenthesis like this) */ a, b) {}
  11. //
  12. // Just put the comment before function or inside:
  13. // /* (((this is fine))) */ function(a, b) {}
  14. // function abc(a) { /* (((this is fine))) */}
  15. var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
  16. var FN_ARG = /\/\*([^\*]*)\*\//m;
  17. var parse = function(fn) {
  18. if (typeof fn !== 'function') {
  19. throw new Error('Can not annotate "' + fn + '". Expected a function!');
  20. }
  21. var match = fn.toString().match(FN_ARGS);
  22. return match[1] && match[1].split(',').map(function(arg) {
  23. match = arg.match(FN_ARG);
  24. return match ? match[1].trim() : arg.trim();
  25. }) || [];
  26. };
  27. exports.annotate = annotate;
  28. exports.parse = parse;