|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -module.exports = function formatLocation(loc) { |
6 | | - if(typeof loc === "string") |
7 | | - return loc; |
8 | | - if(typeof loc === "number") |
9 | | - return loc + ""; |
10 | | - if(loc && typeof loc === "object") { |
11 | | - if(loc.start && loc.end) { |
12 | | - if(typeof loc.start.line === "number" && typeof loc.end.line === "number" && typeof loc.end.column === "number" && loc.start.line === loc.end.line) |
13 | | - return formatPosition(loc.start) + "-" + loc.end.column; |
14 | | - return formatPosition(loc.start) + "-" + formatPosition(loc.end); |
15 | | - } |
16 | | - if(loc.start) |
17 | | - return formatPosition(loc.start); |
18 | | - return formatPosition(loc); |
19 | | - } |
20 | | - return ""; |
21 | 5 |
|
22 | | - function formatPosition(pos) { |
23 | | - if(typeof pos === "string") |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const formatPosition = (pos) => { |
| 9 | + if(pos === null) |
| 10 | + return ""; |
| 11 | + const typeOfPos = typeof pos; |
| 12 | + switch(typeOfPos) { |
| 13 | + case "string": |
24 | 14 | return pos; |
25 | | - if(typeof pos === "number") |
26 | | - return pos + ""; |
27 | | - if(pos && typeof pos === "object") { |
| 15 | + case "number": |
| 16 | + return `${pos}`; |
| 17 | + case "object": |
28 | 18 | if(typeof pos.line === "number" && typeof pos.column === "number") |
29 | | - return pos.line + ":" + pos.column; |
30 | | - if(typeof pos.line === "number") |
31 | | - return pos.line + ":?"; |
32 | | - if(typeof pos.index === "number") |
33 | | - return "+" + pos.index; |
34 | | - } |
| 19 | + return `${pos.line}:${pos.column}`; |
| 20 | + else if(typeof pos.line === "number") |
| 21 | + return `${pos.line}:?`; |
| 22 | + else if(typeof pos.index === "number") |
| 23 | + return `+${pos.index}`; |
| 24 | + else |
| 25 | + return ""; |
| 26 | + default: |
| 27 | + return ""; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const formatLocation = (loc) => { |
| 32 | + if(loc === null) |
35 | 33 | return ""; |
| 34 | + const typeOfLoc = typeof loc; |
| 35 | + switch(typeOfLoc) { |
| 36 | + case "string": |
| 37 | + return loc; |
| 38 | + case "number": |
| 39 | + return `${loc}`; |
| 40 | + case "object": |
| 41 | + if(loc.start && loc.end) { |
| 42 | + if(typeof loc.start.line === "number" && typeof loc.end.line === "number" && typeof loc.end.column === "number" && loc.start.line === loc.end.line) |
| 43 | + return `${formatPosition(loc.start)}-${loc.end.column}`; |
| 44 | + return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`; |
| 45 | + } |
| 46 | + if(loc.start) |
| 47 | + return formatPosition(loc.start); |
| 48 | + return formatPosition(loc); |
| 49 | + default: |
| 50 | + return ""; |
36 | 51 | } |
37 | 52 | }; |
| 53 | + |
| 54 | +module.exports = formatLocation; |
0 commit comments