| [9af201e] | 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 | * Returns formatted position.
|
|---|
| 13 | * @param {SourcePosition} pos position
|
|---|
| 14 | * @returns {string} formatted position
|
|---|
| 15 | */
|
|---|
| 16 | const formatPosition = (pos) => {
|
|---|
| 17 | if (pos && typeof pos === "object") {
|
|---|
| 18 | if ("line" in pos && "column" in pos) {
|
|---|
| 19 | return `${pos.line}:${pos.column}`;
|
|---|
| 20 | } else if ("line" in pos) {
|
|---|
| 21 | return `${pos.line}:?`;
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 | return "";
|
|---|
| 25 | };
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Returns formatted location.
|
|---|
| 29 | * @param {DependencyLocation} loc location
|
|---|
| 30 | * @returns {string} formatted location
|
|---|
| 31 | */
|
|---|
| 32 | const formatLocation = (loc) => {
|
|---|
| 33 | if (loc && typeof loc === "object") {
|
|---|
| 34 | if ("start" in loc && loc.start && "end" in loc && loc.end) {
|
|---|
| 35 | if (
|
|---|
| 36 | typeof loc.start === "object" &&
|
|---|
| 37 | typeof loc.start.line === "number" &&
|
|---|
| 38 | typeof loc.end === "object" &&
|
|---|
| 39 | typeof loc.end.line === "number" &&
|
|---|
| 40 | typeof loc.end.column === "number" &&
|
|---|
| 41 | loc.start.line === loc.end.line
|
|---|
| 42 | ) {
|
|---|
| 43 | return `${formatPosition(loc.start)}-${loc.end.column}`;
|
|---|
| 44 | } else if (
|
|---|
| 45 | typeof loc.start === "object" &&
|
|---|
| 46 | typeof loc.start.line === "number" &&
|
|---|
| 47 | typeof loc.start.column !== "number" &&
|
|---|
| 48 | typeof loc.end === "object" &&
|
|---|
| 49 | typeof loc.end.line === "number" &&
|
|---|
| 50 | typeof loc.end.column !== "number"
|
|---|
| 51 | ) {
|
|---|
| 52 | return `${loc.start.line}-${loc.end.line}`;
|
|---|
| 53 | }
|
|---|
| 54 | return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
|
|---|
| 55 | }
|
|---|
| 56 | if ("start" in loc && loc.start) {
|
|---|
| 57 | return formatPosition(loc.start);
|
|---|
| 58 | }
|
|---|
| 59 | if ("name" in loc && "index" in loc) {
|
|---|
| 60 | return `${loc.name}[${loc.index}]`;
|
|---|
| 61 | }
|
|---|
| 62 | if ("name" in loc) {
|
|---|
| 63 | return loc.name;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return "";
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | module.exports = formatLocation;
|
|---|