borrow.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * A collection of functions relating to resolving @borrows tags in JSDoc symbols.
  3. * @module jsdoc/borrow
  4. */
  5. const doop = require('jsdoc/util/doop');
  6. const SCOPE = require('jsdoc/name').SCOPE;
  7. function cloneBorrowedDoclets({borrowed, longname}, doclets) {
  8. borrowed.forEach(({from, as}) => {
  9. const borrowedDoclets = doclets.index.longname[from];
  10. let borrowedAs = as || from;
  11. let parts;
  12. let scopePunc;
  13. if (borrowedDoclets) {
  14. borrowedAs = borrowedAs.replace(/^prototype\./, SCOPE.PUNC.INSTANCE);
  15. doop(borrowedDoclets).forEach(clone => {
  16. // TODO: this will fail on longnames like '"Foo#bar".baz'
  17. parts = borrowedAs.split(SCOPE.PUNC.INSTANCE);
  18. if (parts.length === 2) {
  19. clone.scope = SCOPE.NAMES.INSTANCE;
  20. scopePunc = SCOPE.PUNC.INSTANCE;
  21. }
  22. else {
  23. clone.scope = SCOPE.NAMES.STATIC;
  24. scopePunc = SCOPE.PUNC.STATIC;
  25. }
  26. clone.name = parts.pop();
  27. clone.memberof = longname;
  28. clone.longname = clone.memberof + scopePunc + clone.name;
  29. doclets.push(clone);
  30. });
  31. }
  32. });
  33. }
  34. /**
  35. Take a copy of the docs for borrowed symbols and attach them to the
  36. docs for the borrowing symbol. This process changes the symbols involved,
  37. moving docs from the "borrowed" array and into the general docs, then
  38. deleting the "borrowed" array.
  39. */
  40. exports.resolveBorrows = doclets => {
  41. for (let doclet of doclets.index.borrowed) {
  42. cloneBorrowedDoclets(doclet, doclets);
  43. delete doclet.borrowed;
  44. }
  45. doclets.index.borrowed = [];
  46. };