|
5 | 5 |
|
6 | 6 | "use strict";
|
7 | 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 | + */ |
8 | 16 | const formatPosition = pos => {
|
9 | 17 | if (pos === null) return "";
|
10 |
| - const typeOfPos = typeof pos; |
11 |
| - switch (typeOfPos) { |
12 |
| - case "string": |
13 |
| - return pos; |
14 |
| - case "number": |
15 |
| - return `${pos}`; |
16 |
| - case "object": |
17 |
| - if (typeof pos.line === "number" && typeof pos.column === "number") { |
18 |
| - return `${pos.line}:${pos.column}`; |
19 |
| - } else if (typeof pos.line === "number") { |
20 |
| - return `${pos.line}:?`; |
21 |
| - } else if (typeof pos.index === "number") { |
22 |
| - return `+${pos.index}`; |
23 |
| - } else { |
24 |
| - return ""; |
25 |
| - } |
26 |
| - default: |
| 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 { |
27 | 30 | return "";
|
| 31 | + } |
28 | 32 | }
|
| 33 | + return ""; |
29 | 34 | };
|
30 | 35 |
|
| 36 | +// TODO webpack 5: loc must be DependencyLocation |
| 37 | +/** |
| 38 | + * @param {DependencyLocation|SourcePosition|string} loc ___location |
| 39 | + * @returns {string} formatted ___location |
| 40 | + */ |
31 | 41 | const formatLocation = loc => {
|
32 | 42 | if (loc === null) return "";
|
33 |
| - const typeOfLoc = typeof loc; |
34 |
| - switch (typeOfLoc) { |
35 |
| - case "string": |
36 |
| - return loc; |
37 |
| - case "number": |
38 |
| - return `${loc}`; |
39 |
| - case "object": |
40 |
| - if (loc.start && loc.end) { |
41 |
| - if ( |
42 |
| - typeof loc.start.line === "number" && |
43 |
| - typeof loc.end.line === "number" && |
44 |
| - typeof loc.end.column === "number" && |
45 |
| - loc.start.line === loc.end.line |
46 |
| - ) { |
47 |
| - return `${formatPosition(loc.start)}-${loc.end.column}`; |
48 |
| - } else { |
49 |
| - return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`; |
50 |
| - } |
51 |
| - } |
52 |
| - if (loc.start) { |
53 |
| - return formatPosition(loc.start); |
| 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)}`; |
54 | 59 | }
|
55 |
| - return formatPosition(loc); |
56 |
| - default: |
57 |
| - return ""; |
| 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); |
58 | 71 | }
|
| 72 | + return ""; |
59 | 73 | };
|
60 | 74 |
|
61 | 75 | module.exports = formatLocation;
|
0 commit comments