1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const formatPosition = pos => {
|
17 | if (pos === null) return "";
|
18 |
|
19 | if (typeof pos === "string") return pos;
|
20 | if (typeof pos === "number") return `${pos}`;
|
21 | if (typeof pos === "object") {
|
22 | if ("line" in pos && "column" in pos) {
|
23 | return `${pos.line}:${pos.column}`;
|
24 | } else if ("line" in pos) {
|
25 | return `${pos.line}:?`;
|
26 | } else if ("index" in pos) {
|
27 |
|
28 | return `+${pos.index}`;
|
29 | } else {
|
30 | return "";
|
31 | }
|
32 | }
|
33 | return "";
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | const formatLocation = loc => {
|
42 | if (loc === null) return "";
|
43 |
|
44 | if (typeof loc === "string") return loc;
|
45 | if (typeof loc === "number") return `${loc}`;
|
46 | if (typeof loc === "object") {
|
47 | if ("start" in loc && loc.start && "end" in loc && loc.end) {
|
48 | if (
|
49 | typeof loc.start === "object" &&
|
50 | typeof loc.start.line === "number" &&
|
51 | typeof loc.end === "object" &&
|
52 | typeof loc.end.line === "number" &&
|
53 | typeof loc.end.column === "number" &&
|
54 | loc.start.line === loc.end.line
|
55 | ) {
|
56 | return `${formatPosition(loc.start)}-${loc.end.column}`;
|
57 | } else {
|
58 | return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
|
59 | }
|
60 | }
|
61 | if ("start" in loc && loc.start) {
|
62 | return formatPosition(loc.start);
|
63 | }
|
64 | if ("name" in loc && "index" in loc) {
|
65 | return `${loc.name}[${loc.index}]`;
|
66 | }
|
67 | if ("name" in loc) {
|
68 | return loc.name;
|
69 | }
|
70 | return formatPosition(loc);
|
71 | }
|
72 | return "";
|
73 | };
|
74 |
|
75 | module.exports = formatLocation;
|