1
|
"use strict";
|
2
|
|
3
|
Object.defineProperty(exports, "__esModule", {
|
4
|
value: true
|
5
|
});
|
6
|
exports.codeFrameFromAst = codeFrameFromAst;
|
7
|
exports.codeFrameFromSource = codeFrameFromSource;
|
8
|
|
9
|
var _wastPrinter = require("@webassemblyjs/wast-printer");
|
10
|
|
11
|
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); }
|
12
|
|
13
|
var SHOW_LINES_AROUND_POINTER = 5;
|
14
|
|
15
|
function repeat(char, nb) {
|
16
|
return Array(nb).fill(char).join("");
|
17
|
} // TODO(sven): allow arbitrary ast nodes
|
18
|
|
19
|
|
20
|
function codeFrameFromAst(ast, loc) {
|
21
|
return codeFrameFromSource((0, _wastPrinter.print)(ast), loc);
|
22
|
}
|
23
|
|
24
|
function 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
|
}
|