1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
|
---|
9 | /** @typedef {import("./Dependency").SourcePosition} SourcePosition */
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @param {SourcePosition} pos position
|
---|
13 | * @returns {string} formatted position
|
---|
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 | * @param {DependencyLocation} loc location
|
---|
28 | * @returns {string} formatted location
|
---|
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;
|
---|