UNPKG

2.06 kBJavaScriptView Raw
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// TODO webpack 5: pos must be SourcePosition
12/**
13 * @param {SourcePosition|DependencyLocation|string} pos position
14 * @returns {string} formatted position
15 */
16const formatPosition = pos => {
17 if (pos === null) return "";
18 // TODO webpack 5: Simplify this
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 // TODO webpack 5 remove this case
28 return `+${pos.index}`;
29 } else {
30 return "";
31 }
32 }
33 return "";
34};
35
36// TODO webpack 5: loc must be DependencyLocation
37/**
38 * @param {DependencyLocation|SourcePosition|string} loc location
39 * @returns {string} formatted location
40 */
41const formatLocation = loc => {
42 if (loc === null) return "";
43 // TODO webpack 5: Simplify this
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
75module.exports = formatLocation;