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