processors.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2024-present Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. const processors = [];
  20. processors.push({
  21. title: 'track all require calls',
  22. match () { return true; },
  23. traverse: {
  24. CallExpression (path, context) {
  25. const callee = path.get('callee');
  26. if ( ! callee.isIdentifier() ) return;
  27. if ( callee.node.name === 'require' ) {
  28. context.doc_module.requires.push(path.node.arguments[0].value);
  29. }
  30. }
  31. }
  32. });
  33. processors.push({
  34. title: 'get leading comment',
  35. match () { return true; },
  36. traverse: {
  37. ClassDeclaration (path, context) {
  38. const node = path.node;
  39. const comment = (node.leadingComments && (
  40. node.leadingComments.length < 1 ? '' :
  41. node.leadingComments[node.leadingComments.length - 1]
  42. )) ?? '';
  43. context.comment = comment;
  44. }
  45. }
  46. });
  47. processors.push({
  48. title: 'provide name and comment for modules and services',
  49. match (context) {
  50. return context.type === 'module' || context.type === 'service';
  51. },
  52. traverse: {
  53. ClassDeclaration (path, context) {
  54. context.doc_item = context.doc_module;
  55. if ( context.type === 'service' ) {
  56. // Skip if class name doesn't end with 'Service'
  57. if ( ! path.node.id.name.endsWith('Service') ) {
  58. context.skip = true;
  59. return;
  60. }
  61. context.doc_item = context.doc_module.add_service();
  62. }
  63. context.doc_item.name = path.node.id.name;
  64. if ( context.comment === '' ) return;
  65. context.doc_item.provide_comment(context.comment);
  66. }
  67. }
  68. });
  69. processors.push({
  70. title: 'provide methods and listeners for services',
  71. match (context) {
  72. return context.type === 'service';
  73. },
  74. traverse: {
  75. ClassDeclaration (path, context) {
  76. path.node.body.body.forEach(member => {
  77. if ( member.type !== 'ClassMethod' ) return;
  78. const key = member.key.name ?? member.key.value;
  79. const comment = member.leadingComments?.[0]?.value ?? '';
  80. if ( key.startsWith('__on_') ) {
  81. // 2nd argument is always an object destructuring;
  82. // we want the list of keys in the object:
  83. const params = member.params?.[1]?.properties ?? [];
  84. context.doc_item.provide_listener({
  85. key: key.slice(5),
  86. comment,
  87. params,
  88. });
  89. } else {
  90. // Method overrides
  91. if ( key.startsWith('_') ) return;
  92. // Private methods
  93. if ( key.endsWith('_') ) return;
  94. const params = member.params ?? [];
  95. context.doc_item.provide_method({
  96. key,
  97. comment,
  98. params,
  99. });
  100. }
  101. });
  102. }
  103. }
  104. });
  105. processors.push({
  106. title: 'provide library function documentation',
  107. match (context) {
  108. return context.type === 'lib';
  109. },
  110. traverse: {
  111. VariableDeclaration (path, context) {
  112. // skip non-const declarations
  113. if ( path.node.kind !== 'const' ) return;
  114. // skip declarations with multiple declarators
  115. if ( path.node.declarations.length !== 1 ) return;
  116. // skip declarations without an initializer
  117. if ( ! path.node.declarations[0].init ) return;
  118. // skip declarations that aren't in the root scope
  119. if ( path.scope.parent ) return;
  120. console.log('path.node', path.node.declarations);
  121. // is it a function?
  122. if ( ! ['FunctionExpression', 'ArrowFunctionExpression'].includes(
  123. path.node.declarations[0].init.type
  124. ) ) return;
  125. // get the name of the function
  126. const name = path.node.declarations[0].id.name;
  127. // get the comment
  128. const comment = path.node.leadingComments?.[0]?.value ?? '';
  129. // get the parameters
  130. const params = path.node.declarations[0].init.params ?? [];
  131. context.doc_item.provide_function({
  132. key: name,
  133. comment,
  134. params,
  135. });
  136. }
  137. }
  138. });
  139. module.exports = processors;