vfile.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @fileoverview Virtual file
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Type Definitions
  8. //-----------------------------------------------------------------------------
  9. /** @typedef {import("@eslint/core").File} File */
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Determines if a given value has a byte order mark (BOM).
  15. * @param {string|Uint8Array} value The value to check.
  16. * @returns {boolean} `true` if the value has a BOM, `false` otherwise.
  17. */
  18. function hasUnicodeBOM(value) {
  19. return typeof value === "string"
  20. ? value.charCodeAt(0) === 0xFEFF
  21. : value[0] === 0xEF && value[1] === 0xBB && value[2] === 0xBF;
  22. }
  23. /**
  24. * Strips Unicode BOM from the given value.
  25. * @param {string|Uint8Array} value The value to remove the BOM from.
  26. * @returns {string|Uint8Array} The stripped value.
  27. */
  28. function stripUnicodeBOM(value) {
  29. if (!hasUnicodeBOM(value)) {
  30. return value;
  31. }
  32. if (typeof value === "string") {
  33. /*
  34. * Check Unicode BOM.
  35. * In JavaScript, string data is stored as UTF-16, so BOM is 0xFEFF.
  36. * http://www.ecma-international.org/ecma-262/6.0/#sec-unicode-format-control-characters
  37. */
  38. return value.slice(1);
  39. }
  40. /*
  41. * In a Uint8Array, the BOM is represented by three bytes: 0xEF, 0xBB, and 0xBF,
  42. * so we can just remove the first three bytes.
  43. */
  44. return value.slice(3);
  45. }
  46. //------------------------------------------------------------------------------
  47. // Exports
  48. //------------------------------------------------------------------------------
  49. /**
  50. * Represents a virtual file inside of ESLint.
  51. * @implements {File}
  52. */
  53. class VFile {
  54. /**
  55. * The file path including any processor-created virtual path.
  56. * @type {string}
  57. * @readonly
  58. */
  59. path;
  60. /**
  61. * The file path on disk.
  62. * @type {string}
  63. * @readonly
  64. */
  65. physicalPath;
  66. /**
  67. * The file contents.
  68. * @type {string|Uint8Array}
  69. * @readonly
  70. */
  71. body;
  72. /**
  73. * The raw body of the file, including a BOM if present.
  74. * @type {string|Uint8Array}
  75. * @readonly
  76. */
  77. rawBody;
  78. /**
  79. * Indicates whether the file has a byte order mark (BOM).
  80. * @type {boolean}
  81. * @readonly
  82. */
  83. bom;
  84. /**
  85. * Creates a new instance.
  86. * @param {string} path The file path.
  87. * @param {string|Uint8Array} body The file contents.
  88. * @param {Object} [options] Additional options.
  89. * @param {string} [options.physicalPath] The file path on disk.
  90. */
  91. constructor(path, body, { physicalPath } = {}) {
  92. this.path = path;
  93. this.physicalPath = physicalPath ?? path;
  94. this.bom = hasUnicodeBOM(body);
  95. this.body = stripUnicodeBOM(body);
  96. this.rawBody = body;
  97. }
  98. }
  99. module.exports = { VFile };