blockString.js.flow 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @flow strict
  2. /**
  3. * Produces the value of a block string from its parsed raw value, similar to
  4. * CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
  5. *
  6. * This implements the GraphQL spec's BlockStringValue() static algorithm.
  7. *
  8. * @internal
  9. */
  10. export function dedentBlockStringValue(rawString: string): string {
  11. // Expand a block string's raw value into independent lines.
  12. const lines = rawString.split(/\r\n|[\n\r]/g);
  13. // Remove common indentation from all lines but first.
  14. const commonIndent = getBlockStringIndentation(lines);
  15. if (commonIndent !== 0) {
  16. for (let i = 1; i < lines.length; i++) {
  17. lines[i] = lines[i].slice(commonIndent);
  18. }
  19. }
  20. // Remove leading and trailing blank lines.
  21. while (lines.length > 0 && isBlank(lines[0])) {
  22. lines.shift();
  23. }
  24. while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
  25. lines.pop();
  26. }
  27. // Return a string of the lines joined with U+000A.
  28. return lines.join('\n');
  29. }
  30. /**
  31. * @internal
  32. */
  33. export function getBlockStringIndentation(
  34. lines: $ReadOnlyArray<string>,
  35. ): number {
  36. let commonIndent = null;
  37. for (let i = 1; i < lines.length; i++) {
  38. const line = lines[i];
  39. const indent = leadingWhitespace(line);
  40. if (indent === line.length) {
  41. continue; // skip empty lines
  42. }
  43. if (commonIndent === null || indent < commonIndent) {
  44. commonIndent = indent;
  45. if (commonIndent === 0) {
  46. break;
  47. }
  48. }
  49. }
  50. return commonIndent === null ? 0 : commonIndent;
  51. }
  52. function leadingWhitespace(str) {
  53. let i = 0;
  54. while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
  55. i++;
  56. }
  57. return i;
  58. }
  59. function isBlank(str) {
  60. return leadingWhitespace(str) === str.length;
  61. }
  62. /**
  63. * Print a block string in the indented block form by adding a leading and
  64. * trailing blank line. However, if a block string starts with whitespace and is
  65. * a single-line, adding a leading blank line would strip that whitespace.
  66. *
  67. * @internal
  68. */
  69. export function printBlockString(
  70. value: string,
  71. indentation?: string = '',
  72. preferMultipleLines?: boolean = false,
  73. ): string {
  74. const isSingleLine = value.indexOf('\n') === -1;
  75. const hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  76. const hasTrailingQuote = value[value.length - 1] === '"';
  77. const hasTrailingSlash = value[value.length - 1] === '\\';
  78. const printAsMultipleLines =
  79. !isSingleLine ||
  80. hasTrailingQuote ||
  81. hasTrailingSlash ||
  82. preferMultipleLines;
  83. let result = '';
  84. // Format a multi-line block quote to account for leading space.
  85. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  86. result += '\n' + indentation;
  87. }
  88. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  89. if (printAsMultipleLines) {
  90. result += '\n';
  91. }
  92. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  93. }