blockString.js.flow 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(rawString);
  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. let startLine = 0;
  22. while (startLine < lines.length && isBlank(lines[startLine])) {
  23. ++startLine;
  24. }
  25. let endLine = lines.length;
  26. while (endLine > startLine && isBlank(lines[endLine - 1])) {
  27. --endLine;
  28. }
  29. // Return a string of the lines joined with U+000A.
  30. return lines.slice(startLine, endLine).join('\n');
  31. }
  32. function isBlank(str: string): boolean {
  33. for (let i = 0; i < str.length; ++i) {
  34. if (str[i] !== ' ' && str[i] !== '\t') {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. /**
  41. * @internal
  42. */
  43. export function getBlockStringIndentation(value: string): number {
  44. let isFirstLine = true;
  45. let isEmptyLine = true;
  46. let indent = 0;
  47. let commonIndent = null;
  48. for (let i = 0; i < value.length; ++i) {
  49. switch (value.charCodeAt(i)) {
  50. case 13: // \r
  51. if (value.charCodeAt(i + 1) === 10) {
  52. ++i; // skip \r\n as one symbol
  53. }
  54. // falls through
  55. case 10: // \n
  56. isFirstLine = false;
  57. isEmptyLine = true;
  58. indent = 0;
  59. break;
  60. case 9: // \t
  61. case 32: // <space>
  62. ++indent;
  63. break;
  64. default:
  65. if (
  66. isEmptyLine &&
  67. !isFirstLine &&
  68. (commonIndent === null || indent < commonIndent)
  69. ) {
  70. commonIndent = indent;
  71. }
  72. isEmptyLine = false;
  73. }
  74. }
  75. return commonIndent ?? 0;
  76. }
  77. /**
  78. * Print a block string in the indented block form by adding a leading and
  79. * trailing blank line. However, if a block string starts with whitespace and is
  80. * a single-line, adding a leading blank line would strip that whitespace.
  81. *
  82. * @internal
  83. */
  84. export function printBlockString(
  85. value: string,
  86. indentation: string = '',
  87. preferMultipleLines: boolean = false,
  88. ): string {
  89. const isSingleLine = value.indexOf('\n') === -1;
  90. const hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  91. const hasTrailingQuote = value[value.length - 1] === '"';
  92. const hasTrailingSlash = value[value.length - 1] === '\\';
  93. const printAsMultipleLines =
  94. !isSingleLine ||
  95. hasTrailingQuote ||
  96. hasTrailingSlash ||
  97. preferMultipleLines;
  98. let result = '';
  99. // Format a multi-line block quote to account for leading space.
  100. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  101. result += '\n' + indentation;
  102. }
  103. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  104. if (printAsMultipleLines) {
  105. result += '\n';
  106. }
  107. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  108. }