util-empty-scalar-position.js 837 B

123456789101112131415161718192021222324252627
  1. function emptyScalarPosition(offset, before, pos) {
  2. if (before) {
  3. if (pos === null)
  4. pos = before.length;
  5. for (let i = pos - 1; i >= 0; --i) {
  6. let st = before[i];
  7. switch (st.type) {
  8. case 'space':
  9. case 'comment':
  10. case 'newline':
  11. offset -= st.source.length;
  12. continue;
  13. }
  14. // Technically, an empty scalar is immediately after the last non-empty
  15. // node, but it's more useful to place it after any whitespace.
  16. st = before[++i];
  17. while (st?.type === 'space') {
  18. offset += st.source.length;
  19. st = before[++i];
  20. }
  21. break;
  22. }
  23. }
  24. return offset;
  25. }
  26. export { emptyScalarPosition };