resolve-end.js 1.2 KB

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