printLocation.js.flow 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // @flow strict
  2. import type { Source } from './source';
  3. import type { Location } from './ast';
  4. import type { SourceLocation } from './location';
  5. import { getLocation } from './location';
  6. /**
  7. * Render a helpful description of the location in the GraphQL Source document.
  8. */
  9. export function printLocation(location: Location): string {
  10. return printSourceLocation(
  11. location.source,
  12. getLocation(location.source, location.start),
  13. );
  14. }
  15. /**
  16. * Render a helpful description of the location in the GraphQL Source document.
  17. */
  18. export function printSourceLocation(
  19. source: Source,
  20. sourceLocation: SourceLocation,
  21. ): string {
  22. const firstLineColumnOffset = source.locationOffset.column - 1;
  23. const body = whitespace(firstLineColumnOffset) + source.body;
  24. const lineIndex = sourceLocation.line - 1;
  25. const lineOffset = source.locationOffset.line - 1;
  26. const lineNum = sourceLocation.line + lineOffset;
  27. const columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
  28. const columnNum = sourceLocation.column + columnOffset;
  29. const locationStr = `${source.name}:${lineNum}:${columnNum}\n`;
  30. const lines = body.split(/\r\n|[\n\r]/g);
  31. const locationLine = lines[lineIndex];
  32. // Special case for minified documents
  33. if (locationLine.length > 120) {
  34. const subLineIndex = Math.floor(columnNum / 80);
  35. const subLineColumnNum = columnNum % 80;
  36. const subLines = [];
  37. for (let i = 0; i < locationLine.length; i += 80) {
  38. subLines.push(locationLine.slice(i, i + 80));
  39. }
  40. return (
  41. locationStr +
  42. printPrefixedLines([
  43. [`${lineNum}`, subLines[0]],
  44. ...subLines.slice(1, subLineIndex + 1).map((subLine) => ['', subLine]),
  45. [' ', whitespace(subLineColumnNum - 1) + '^'],
  46. ['', subLines[subLineIndex + 1]],
  47. ])
  48. );
  49. }
  50. return (
  51. locationStr +
  52. printPrefixedLines([
  53. // Lines specified like this: ["prefix", "string"],
  54. [`${lineNum - 1}`, lines[lineIndex - 1]],
  55. [`${lineNum}`, locationLine],
  56. ['', whitespace(columnNum - 1) + '^'],
  57. [`${lineNum + 1}`, lines[lineIndex + 1]],
  58. ])
  59. );
  60. }
  61. function printPrefixedLines(lines: $ReadOnlyArray<[string, string]>): string {
  62. const existingLines = lines.filter(([_, line]) => line !== undefined);
  63. const padLen = Math.max(...existingLines.map(([prefix]) => prefix.length));
  64. return existingLines
  65. .map(
  66. ([prefix, line]) =>
  67. leftPad(padLen, prefix) + (line ? ' | ' + line : ' |'),
  68. )
  69. .join('\n');
  70. }
  71. function whitespace(len: number): string {
  72. return Array(len + 1).join(' ');
  73. }
  74. function leftPad(len: number, str: string): string {
  75. return whitespace(len - str.length) + str;
  76. }