SyntaxError.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { createCustomError } from '../utils/create-custom-error.js';
  2. const MAX_LINE_LENGTH = 100;
  3. const OFFSET_CORRECTION = 60;
  4. const TAB_REPLACEMENT = ' ';
  5. function sourceFragment({ source, line, column, baseLine, baseColumn }, extraLines) {
  6. function processLines(start, end) {
  7. return lines
  8. .slice(start, end)
  9. .map((line, idx) =>
  10. String(start + idx + 1).padStart(maxNumLength) + ' |' + line
  11. ).join('\n');
  12. }
  13. const prelines = '\n'.repeat(Math.max(baseLine - 1, 0));
  14. const precolumns = ' '.repeat(Math.max(baseColumn - 1, 0));
  15. const lines = (prelines + precolumns + source).split(/\r\n?|\n|\f/);
  16. const startLine = Math.max(1, line - extraLines) - 1;
  17. const endLine = Math.min(line + extraLines, lines.length + 1);
  18. const maxNumLength = Math.max(4, String(endLine).length) + 1;
  19. let cutLeft = 0;
  20. // column correction according to replaced tab before column
  21. column += (TAB_REPLACEMENT.length - 1) * (lines[line - 1].substr(0, column - 1).match(/\t/g) || []).length;
  22. if (column > MAX_LINE_LENGTH) {
  23. cutLeft = column - OFFSET_CORRECTION + 3;
  24. column = OFFSET_CORRECTION - 2;
  25. }
  26. for (let i = startLine; i <= endLine; i++) {
  27. if (i >= 0 && i < lines.length) {
  28. lines[i] = lines[i].replace(/\t/g, TAB_REPLACEMENT);
  29. lines[i] =
  30. (cutLeft > 0 && lines[i].length > cutLeft ? '\u2026' : '') +
  31. lines[i].substr(cutLeft, MAX_LINE_LENGTH - 2) +
  32. (lines[i].length > cutLeft + MAX_LINE_LENGTH - 1 ? '\u2026' : '');
  33. }
  34. }
  35. return [
  36. processLines(startLine, line),
  37. new Array(column + maxNumLength + 2).join('-') + '^',
  38. processLines(line, endLine)
  39. ].filter(Boolean)
  40. .join('\n')
  41. .replace(/^(\s+\d+\s+\|\n)+/, '')
  42. .replace(/\n(\s+\d+\s+\|)+$/, '');
  43. }
  44. export function SyntaxError(message, source, offset, line, column, baseLine = 1, baseColumn = 1) {
  45. const error = Object.assign(createCustomError('SyntaxError', message), {
  46. source,
  47. offset,
  48. line,
  49. column,
  50. sourceFragment(extraLines) {
  51. return sourceFragment({ source, line, column, baseLine, baseColumn }, isNaN(extraLines) ? 0 : extraLines);
  52. },
  53. get formattedMessage() {
  54. return (
  55. `Parse error: ${message}\n` +
  56. sourceFragment({ source, line, column, baseLine, baseColumn }, 2)
  57. );
  58. }
  59. });
  60. return error;
  61. }