util-empty-scalar-position.js 871 B

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