You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Module dependencies.
  3. */
  4. var types = require('ast-types');
  5. var esprima = require('esprima');
  6. var escodegen = require('escodegen');
  7. /**
  8. * Helper functions.
  9. */
  10. var n = types.namedTypes;
  11. var b = types.builders;
  12. /**
  13. * Module exports.
  14. */
  15. module.exports = degenerator;
  16. /**
  17. * Turns sync JavaScript code into an JavaScript with async Generator Functions.
  18. *
  19. * @param {String} jsStr JavaScript string to convert
  20. * @param {Array} names Array of function names to add `yield` operators to
  21. * @return {String} Converted JavaScript string with Generator functions injected
  22. * @api public
  23. */
  24. function degenerator (jsStr, names) {
  25. if (!Array.isArray(names)) {
  26. throw new TypeError('an array of async function "names" is required');
  27. }
  28. var ast = esprima.parse(jsStr);
  29. // duplicate the `names` array since it's rude to augment the user-provided
  30. // array
  31. names = names.slice(0);
  32. // first pass is to find the `function` nodes and turn them into `function *`
  33. // generator functions. We also add the names of the functions to the `names`
  34. // array
  35. types.visit(ast, {
  36. visitFunction: function(path) {
  37. if (path.node.id) {
  38. // got a "function" expression/statement,
  39. // convert it into a "generator function"
  40. path.node.generator = true;
  41. // add function name to `names` array
  42. names.push(path.node.id.name);
  43. }
  44. this.traverse(path);
  45. }
  46. });
  47. // second pass is for adding `yield` statements to any function
  48. // invocations that match the given `names` array.
  49. types.visit(ast, {
  50. visitCallExpression: function(path) {
  51. if (checkNames(path.node, names)) {
  52. // a "function invocation" expression,
  53. // we need to inject a `YieldExpression`
  54. var name = path.name;
  55. var parent = path.parent.node;
  56. var delegate = false;
  57. var expr = b.yieldExpression(path.node, delegate);
  58. if (parent['arguments']) {
  59. // parent is a `CallExpression` type
  60. parent['arguments'][name] = expr;
  61. } else {
  62. parent[name] = expr;
  63. }
  64. }
  65. this.traverse(path);
  66. }
  67. });
  68. return escodegen.generate(ast);
  69. }
  70. /**
  71. * Returns `true` if `node` has a matching name to one of the entries in the
  72. * `names` array.
  73. *
  74. * @param {types.Node} node
  75. * @param {Array} names Array of function names to return true for
  76. * @return {Boolean}
  77. * @api private
  78. */
  79. function checkNames (node, names) {
  80. var name;
  81. var callee = node.callee;
  82. if ('Identifier' == callee.type) {
  83. name = callee.name;
  84. } else if ('MemberExpression' == callee.type) {
  85. name = callee.object.name + '.' + (callee.property.name || callee.property.raw);
  86. } else if ('FunctionExpression' == callee.type) {
  87. if (callee.id) {
  88. name = callee.id.name;
  89. } else {
  90. return false;
  91. }
  92. } else {
  93. throw new Error('don\'t know how to get name for: ' + callee.type);
  94. }
  95. // now that we have the `name`, check if any entries match in the `names` array
  96. var n;
  97. for (var i = 0; i < names.length; i++) {
  98. n = names[i];
  99. if (n.test) {
  100. // regexp
  101. if (n.test(name)) return true;
  102. } else {
  103. if (name == n) return true;
  104. }
  105. }
  106. return false;
  107. }