componentMethodsJsDocHandler.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import parseJsDoc from '../utils/parseJsDoc.js';
  2. function removeEmpty(obj) {
  3. return Object.fromEntries(Object.entries(obj).filter(([, v]) => v != null));
  4. }
  5. function merge(obj1, obj2) {
  6. if (obj1 == null && obj2 == null) {
  7. return null;
  8. }
  9. const merged = {
  10. ...removeEmpty(obj1 ?? {}),
  11. ...removeEmpty(obj2 ?? {}),
  12. };
  13. return merged;
  14. }
  15. /**
  16. * Extract info from the methods jsdoc blocks. Must be run after
  17. * componentMethodsHandler.
  18. */
  19. const componentMethodsJsDocHandler = function (documentation) {
  20. let methods = documentation.get('methods');
  21. if (!methods) {
  22. return;
  23. }
  24. methods = methods.map((method) => {
  25. if (!method.docblock) {
  26. return method;
  27. }
  28. const jsDoc = parseJsDoc(method.docblock);
  29. const returns = merge(jsDoc.returns, method.returns);
  30. const params = method.params.map((param) => {
  31. const jsDocParam = jsDoc.params.find((p) => p.name === param.name);
  32. return merge(jsDocParam, param);
  33. });
  34. return {
  35. ...method,
  36. description: jsDoc.description || null,
  37. returns,
  38. params,
  39. };
  40. });
  41. documentation.set('methods', methods);
  42. };
  43. export default componentMethodsJsDocHandler;