You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.codeFrameFromAst = codeFrameFromAst;
  6. exports.codeFrameFromSource = codeFrameFromSource;
  7. var _wastPrinter = require("@webassemblyjs/wast-printer");
  8. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  9. var SHOW_LINES_AROUND_POINTER = 5;
  10. function repeat(char, nb) {
  11. return Array(nb).fill(char).join("");
  12. } // TODO(sven): allow arbitrary ast nodes
  13. function codeFrameFromAst(ast, loc) {
  14. return codeFrameFromSource((0, _wastPrinter.print)(ast), loc);
  15. }
  16. function codeFrameFromSource(source, loc) {
  17. var start = loc.start,
  18. end = loc.end;
  19. var length = 1;
  20. if (_typeof(end) === "object") {
  21. length = end.column - start.column + 1;
  22. }
  23. return source.split("\n").reduce(function (acc, line, lineNbr) {
  24. if (Math.abs(start.line - lineNbr) < SHOW_LINES_AROUND_POINTER) {
  25. acc += line + "\n";
  26. } // Add a new line with the pointer padded left
  27. if (lineNbr === start.line - 1) {
  28. acc += repeat(" ", start.column - 1);
  29. acc += repeat("^", length);
  30. acc += "\n";
  31. }
  32. return acc;
  33. }, "");
  34. }