UNPKG

1.45 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.codeFrameFromAst = codeFrameFromAst;
7exports.codeFrameFromSource = codeFrameFromSource;
8
9var _wastPrinter = require("@webassemblyjs/wast-printer");
10
11function _typeof(obj) { "@babel/helpers - typeof"; 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); }
12
13var SHOW_LINES_AROUND_POINTER = 5;
14
15function repeat(_char, nb) {
16 return Array(nb).fill(_char).join("");
17} // TODO(sven): allow arbitrary ast nodes
18
19
20function codeFrameFromAst(ast, loc) {
21 return codeFrameFromSource((0, _wastPrinter.print)(ast), loc);
22}
23
24function codeFrameFromSource(source, loc) {
25 var start = loc.start,
26 end = loc.end;
27 var length = 1;
28
29 if (_typeof(end) === "object") {
30 length = end.column - start.column + 1;
31 }
32
33 return source.split("\n").reduce(function (acc, line, lineNbr) {
34 if (Math.abs(start.line - lineNbr) < SHOW_LINES_AROUND_POINTER) {
35 acc += line + "\n";
36 } // Add a new line with the pointer padded left
37
38
39 if (lineNbr === start.line - 1) {
40 acc += repeat(" ", start.column - 1);
41 acc += repeat("^", length);
42 acc += "\n";
43 }
44
45 return acc;
46 }, "");
47}
\No newline at end of file