printLocation.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.printLocation = printLocation;
  6. exports.printSourceLocation = printSourceLocation;
  7. var _location = require("../language/location");
  8. /**
  9. * Render a helpful description of the location in the GraphQL Source document.
  10. */
  11. function printLocation(location) {
  12. return printSourceLocation(location.source, (0, _location.getLocation)(location.source, location.start));
  13. }
  14. /**
  15. * Render a helpful description of the location in the GraphQL Source document.
  16. */
  17. function printSourceLocation(source, sourceLocation) {
  18. var firstLineColumnOffset = source.locationOffset.column - 1;
  19. var body = whitespace(firstLineColumnOffset) + source.body;
  20. var lineIndex = sourceLocation.line - 1;
  21. var lineOffset = source.locationOffset.line - 1;
  22. var lineNum = sourceLocation.line + lineOffset;
  23. var columnOffset = sourceLocation.line === 1 ? firstLineColumnOffset : 0;
  24. var columnNum = sourceLocation.column + columnOffset;
  25. var locationStr = "".concat(source.name, ":").concat(lineNum, ":").concat(columnNum, "\n");
  26. var lines = body.split(/\r\n|[\n\r]/g);
  27. var locationLine = lines[lineIndex]; // Special case for minified documents
  28. if (locationLine.length > 120) {
  29. var sublineIndex = Math.floor(columnNum / 80);
  30. var sublineColumnNum = columnNum % 80;
  31. var sublines = [];
  32. for (var i = 0; i < locationLine.length; i += 80) {
  33. sublines.push(locationLine.slice(i, i + 80));
  34. }
  35. return locationStr + printPrefixedLines([["".concat(lineNum), sublines[0]]].concat(sublines.slice(1, sublineIndex + 1).map(function (subline) {
  36. return ['', subline];
  37. }), [[' ', whitespace(sublineColumnNum - 1) + '^'], ['', sublines[sublineIndex + 1]]]));
  38. }
  39. return locationStr + printPrefixedLines([// Lines specified like this: ["prefix", "string"],
  40. ["".concat(lineNum - 1), lines[lineIndex - 1]], ["".concat(lineNum), locationLine], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1), lines[lineIndex + 1]]]);
  41. }
  42. function printPrefixedLines(lines) {
  43. var existingLines = lines.filter(function (_ref) {
  44. var _ = _ref[0],
  45. line = _ref[1];
  46. return line !== undefined;
  47. });
  48. var padLen = Math.max.apply(Math, existingLines.map(function (_ref2) {
  49. var prefix = _ref2[0];
  50. return prefix.length;
  51. }));
  52. return existingLines.map(function (_ref3) {
  53. var prefix = _ref3[0],
  54. line = _ref3[1];
  55. return lpad(padLen, prefix) + (line ? ' | ' + line : ' |');
  56. }).join('\n');
  57. }
  58. function whitespace(len) {
  59. return Array(len + 1).join(' ');
  60. }
  61. function lpad(len, str) {
  62. return whitespace(len - str.length) + str;
  63. }