resolve-end.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. function resolveEnd(end, offset, reqSpace, onError) {
  2. let comment = '';
  3. if (end) {
  4. let hasSpace = false;
  5. let sep = '';
  6. for (const token of end) {
  7. const { source, type } = token;
  8. switch (type) {
  9. case 'space':
  10. hasSpace = true;
  11. break;
  12. case 'comment': {
  13. if (reqSpace && !hasSpace)
  14. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  15. const cb = source.substring(1) || ' ';
  16. if (!comment)
  17. comment = cb;
  18. else
  19. comment += sep + cb;
  20. sep = '';
  21. break;
  22. }
  23. case 'newline':
  24. if (comment)
  25. sep += source;
  26. hasSpace = true;
  27. break;
  28. default:
  29. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
  30. }
  31. offset += source.length;
  32. }
  33. }
  34. return { comment, offset };
  35. }
  36. export { resolveEnd };