processors.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Skip if class name doesn't end with 'Service'
  39. if ( ! path.node.id.name.endsWith('Service') ) {
  40. context.skip = true;
  41. return;
  42. }
  43. context.doc_item = context.doc_module.add_service();
  44. }
  45. context.doc_item.name = path.node.id.name;
  46. if ( context.comment === '' ) return;
  47. context.doc_item.provide_comment(context.comment);
  48. }
  49. }
  50. });
  51. processors.push({
  52. title: 'provide methods and listeners for services',
  53. match (context) {
  54. return context.type === 'service';
  55. },
  56. traverse: {
  57. ClassDeclaration (path, context) {
  58. path.node.body.body.forEach(member => {
  59. if ( member.type !== 'ClassMethod' ) return;
  60. const key = member.key.name ?? member.key.value;
  61. const comment = member.leadingComments?.[0]?.value ?? '';
  62. if ( key.startsWith('__on_') ) {
  63. // 2nd argument is always an object destructuring;
  64. // we want the list of keys in the object:
  65. const params = member.params?.[1]?.properties ?? [];
  66. context.doc_item.provide_listener({
  67. key: key.slice(5),
  68. comment,
  69. params,
  70. });
  71. } else {
  72. // Method overrides
  73. if ( key.startsWith('_') ) return;
  74. // Private methods
  75. if ( key.endsWith('_') ) return;
  76. const params = member.params ?? [];
  77. context.doc_item.provide_method({
  78. key,
  79. comment,
  80. params,
  81. });
  82. }
  83. });
  84. }
  85. }
  86. });
  87. processors.push({
  88. title: 'provide library function documentation',
  89. match (context) {
  90. return context.type === 'lib';
  91. },
  92. traverse: {
  93. VariableDeclaration (path, context) {
  94. // skip non-const declarations
  95. if ( path.node.kind !== 'const' ) return;
  96. // skip declarations with multiple declarators
  97. if ( path.node.declarations.length !== 1 ) return;
  98. // skip declarations without an initializer
  99. if ( ! path.node.declarations[0].init ) return;
  100. // skip declarations that aren't in the root scope
  101. if ( path.scope.parent ) return;
  102. console.log('path.node', path.node.declarations);
  103. // is it a function?
  104. if ( ! ['FunctionExpression', 'ArrowFunctionExpression'].includes(
  105. path.node.declarations[0].init.type
  106. ) ) return;
  107. // get the name of the function
  108. const name = path.node.declarations[0].id.name;
  109. // get the comment
  110. const comment = path.node.leadingComments?.[0]?.value ?? '';
  111. // get the parameters
  112. const params = path.node.declarations[0].init.params ?? [];
  113. context.doc_item.provide_function({
  114. key: name,
  115. comment,
  116. params,
  117. });
  118. }
  119. }
  120. });
  121. module.exports = processors;