description.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getJoiner = void 0;
  6. const primitives_js_1 = require("../../primitives.cjs");
  7. /**
  8. * Makes no changes to `spec.lines[].tokens` but joins them into `spec.description`
  9. * following given spacing srtategy
  10. * @param {Spacing} spacing tells how to handle the whitespace
  11. * @param {BlockMarkers} markers tells how to handle comment block delimitation
  12. */
  13. function descriptionTokenizer(spacing = 'compact', markers = primitives_js_1.Markers) {
  14. const join = getJoiner(spacing);
  15. return spec => {
  16. spec.description = join(spec.source, markers);
  17. return spec;
  18. };
  19. }
  20. exports.default = descriptionTokenizer;
  21. function getJoiner(spacing) {
  22. if (spacing === 'compact') return compactJoiner;
  23. if (spacing === 'preserve') return preserveJoiner;
  24. return spacing;
  25. }
  26. exports.getJoiner = getJoiner;
  27. function compactJoiner(lines, markers = primitives_js_1.Markers) {
  28. return lines.map(({
  29. tokens: {
  30. description
  31. }
  32. }) => description.trim()).filter(description => description !== '').join(' ');
  33. }
  34. const lineNo = (num, {
  35. tokens
  36. }, i) => tokens.type === '' ? num : i;
  37. const getDescription = ({
  38. tokens
  39. }) => (tokens.delimiter === '' ? tokens.start : tokens.postDelimiter.slice(1)) + tokens.description;
  40. function preserveJoiner(lines, markers = primitives_js_1.Markers) {
  41. if (lines.length === 0) return ''; // skip the opening line with no description
  42. if (lines[0].tokens.description === '' && lines[0].tokens.delimiter === markers.start) lines = lines.slice(1); // skip the closing line with no description
  43. const lastLine = lines[lines.length - 1];
  44. if (lastLine !== undefined && lastLine.tokens.description === '' && lastLine.tokens.end.endsWith(markers.end)) lines = lines.slice(0, -1); // description starts at the last line of type definition
  45. lines = lines.slice(lines.reduce(lineNo, 0));
  46. return lines.map(getDescription).join('\n');
  47. }
  48. //# sourceMappingURL=description.cjs.map