You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

formatLocation.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  7. /** @typedef {import("./Dependency").SourcePosition} SourcePosition */
  8. // TODO webpack 5: pos must be SourcePosition
  9. /**
  10. * @param {SourcePosition|DependencyLocation|string} pos position
  11. * @returns {string} formatted position
  12. */
  13. const formatPosition = pos => {
  14. if (pos === null) return "";
  15. // TODO webpack 5: Simplify this
  16. if (typeof pos === "string") return pos;
  17. if (typeof pos === "number") return `${pos}`;
  18. if (typeof pos === "object") {
  19. if ("line" in pos && "column" in pos) {
  20. return `${pos.line}:${pos.column}`;
  21. } else if ("line" in pos) {
  22. return `${pos.line}:?`;
  23. } else if ("index" in pos) {
  24. // TODO webpack 5 remove this case
  25. return `+${pos.index}`;
  26. } else {
  27. return "";
  28. }
  29. }
  30. return "";
  31. };
  32. // TODO webpack 5: loc must be DependencyLocation
  33. /**
  34. * @param {DependencyLocation|SourcePosition|string} loc location
  35. * @returns {string} formatted location
  36. */
  37. const formatLocation = loc => {
  38. if (loc === null) return "";
  39. // TODO webpack 5: Simplify this
  40. if (typeof loc === "string") return loc;
  41. if (typeof loc === "number") return `${loc}`;
  42. if (typeof loc === "object") {
  43. if ("start" in loc && loc.start && "end" in loc && loc.end) {
  44. if (
  45. typeof loc.start === "object" &&
  46. typeof loc.start.line === "number" &&
  47. typeof loc.end === "object" &&
  48. typeof loc.end.line === "number" &&
  49. typeof loc.end.column === "number" &&
  50. loc.start.line === loc.end.line
  51. ) {
  52. return `${formatPosition(loc.start)}-${loc.end.column}`;
  53. } else {
  54. return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`;
  55. }
  56. }
  57. if ("start" in loc && loc.start) {
  58. return formatPosition(loc.start);
  59. }
  60. if ("name" in loc && "index" in loc) {
  61. return `${loc.name}[${loc.index}]`;
  62. }
  63. if ("name" in loc) {
  64. return loc.name;
  65. }
  66. return formatPosition(loc);
  67. }
  68. return "";
  69. };
  70. module.exports = formatLocation;