processors.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const processors = [];
  2. processors.push({
  3. title: 'track all require calls',
  4. match () { return true; },
  5. traverse: {
  6. CallExpression (path, context) {
  7. const callee = path.get('callee');
  8. if ( ! callee.isIdentifier() ) return;
  9. if ( callee.node.name === 'require' ) {
  10. context.doc_module.requires.push(path.node.arguments[0].value);
  11. }
  12. }
  13. }
  14. });
  15. processors.push({
  16. title: 'get leading comment',
  17. match () { return true; },
  18. traverse: {
  19. ClassDeclaration (path, context) {
  20. const node = path.node;
  21. const comment = (node.leadingComments && (
  22. node.leadingComments.length < 1 ? '' :
  23. node.leadingComments[node.leadingComments.length - 1]
  24. )) ?? '';
  25. context.comment = comment;
  26. }
  27. }
  28. });
  29. processors.push({
  30. title: 'provide name and comment for modules and services',
  31. match (context) {
  32. return context.type === 'module' || context.type === 'service';
  33. },
  34. traverse: {
  35. ClassDeclaration (path, context) {
  36. context.doc_item = context.doc_module;
  37. if ( context.type === 'service' ) {
  38. context.doc_item = context.doc_module.add_service();
  39. }
  40. context.doc_item.name = path.node.id.name;
  41. context.doc_item.provide_comment(context.comment);
  42. }
  43. }
  44. });
  45. processors.push({
  46. title: 'provide methods and listeners for services',
  47. match (context) {
  48. return context.type === 'service';
  49. },
  50. traverse: {
  51. ClassDeclaration (path, context) {
  52. path.node.body.body.forEach(member => {
  53. if ( member.type !== 'ClassMethod' ) return;
  54. const key = member.key.name ?? member.key.value;
  55. const comment = member.leadingComments?.[0]?.value ?? '';
  56. if ( key.startsWith('__on_') ) {
  57. // 2nd argument is always an object destructuring;
  58. // we want the list of keys in the object:
  59. const params = member.params?.[1]?.properties ?? [];
  60. context.doc_item.provide_listener({
  61. key: key.slice(5),
  62. comment,
  63. params,
  64. });
  65. } else {
  66. // Method overrides
  67. if ( key.startsWith('_') ) return;
  68. // Private methods
  69. if ( key.endsWith('_') ) return;
  70. const params = member.params ?? [];
  71. context.doc_item.provide_method({
  72. key,
  73. comment,
  74. params,
  75. });
  76. }
  77. });
  78. }
  79. }
  80. });
  81. module.exports = processors;