dedentLines.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.dedentLines = void 0;
  6. /**
  7. * Copyright (c) Meta Platforms, Inc. and affiliates.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. const getIndentationLength = line => {
  13. const result = /^( {2})+/.exec(line);
  14. return result === null ? 0 : result[0].length;
  15. };
  16. const dedentLine = line => line.slice(getIndentationLength(line));
  17. // Return true if:
  18. // "key": "value has multiple lines\n…
  19. // "key has multiple lines\n…
  20. const hasUnmatchedDoubleQuoteMarks = string => {
  21. let n = 0;
  22. let i = string.indexOf('"', 0);
  23. while (i !== -1) {
  24. if (i === 0 || string[i - 1] !== '\\') {
  25. n += 1;
  26. }
  27. i = string.indexOf('"', i + 1);
  28. }
  29. return n % 2 !== 0;
  30. };
  31. const isFirstLineOfTag = line => /^( {2})*</.test(line);
  32. // The length of the output array is the index of the next input line.
  33. // Push dedented lines of start tag onto output and return true;
  34. // otherwise return false because:
  35. // * props include a multiline string (or text node, if props have markup)
  36. // * start tag does not close
  37. const dedentStartTag = (input, output) => {
  38. let line = input[output.length];
  39. output.push(dedentLine(line));
  40. if (line.includes('>')) {
  41. return true;
  42. }
  43. while (output.length < input.length) {
  44. line = input[output.length];
  45. if (hasUnmatchedDoubleQuoteMarks(line)) {
  46. return false; // because props include a multiline string
  47. } else if (isFirstLineOfTag(line)) {
  48. // Recursion only if props have markup.
  49. if (!dedentMarkup(input, output)) {
  50. return false;
  51. }
  52. } else {
  53. output.push(dedentLine(line));
  54. if (line.includes('>')) {
  55. return true;
  56. }
  57. }
  58. }
  59. return false;
  60. };
  61. // Push dedented lines of markup onto output and return true;
  62. // otherwise return false because:
  63. // * props include a multiline string
  64. // * text has more than one adjacent line
  65. // * markup does not close
  66. const dedentMarkup = (input, output) => {
  67. let line = input[output.length];
  68. if (!dedentStartTag(input, output)) {
  69. return false;
  70. }
  71. if (input[output.length - 1].includes('/>')) {
  72. return true;
  73. }
  74. let isText = false;
  75. const stack = [];
  76. stack.push(getIndentationLength(line));
  77. while (stack.length > 0 && output.length < input.length) {
  78. line = input[output.length];
  79. if (isFirstLineOfTag(line)) {
  80. if (line.includes('</')) {
  81. output.push(dedentLine(line));
  82. stack.pop();
  83. } else {
  84. if (!dedentStartTag(input, output)) {
  85. return false;
  86. }
  87. if (!input[output.length - 1].includes('/>')) {
  88. stack.push(getIndentationLength(line));
  89. }
  90. }
  91. isText = false;
  92. } else {
  93. if (isText) {
  94. return false; // because text has more than one adjacent line
  95. }
  96. const indentationLengthOfTag = stack[stack.length - 1];
  97. output.push(line.slice(indentationLengthOfTag + 2));
  98. isText = true;
  99. }
  100. }
  101. return stack.length === 0;
  102. };
  103. // Return lines unindented by heuristic;
  104. // otherwise return null because:
  105. // * props include a multiline string
  106. // * text has more than one adjacent line
  107. // * markup does not close
  108. const dedentLines = input => {
  109. const output = [];
  110. while (output.length < input.length) {
  111. const line = input[output.length];
  112. if (hasUnmatchedDoubleQuoteMarks(line)) {
  113. return null;
  114. } else if (isFirstLineOfTag(line)) {
  115. if (!dedentMarkup(input, output)) {
  116. return null;
  117. }
  118. } else {
  119. output.push(dedentLine(line));
  120. }
  121. }
  122. return output;
  123. };
  124. exports.dedentLines = dedentLines;