buffer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. const SPACES_RE = /^[ \t]+$/;
  7. class Buffer {
  8. constructor(map) {
  9. this._map = null;
  10. this._buf = [];
  11. this._last = "";
  12. this._queue = [];
  13. this._position = {
  14. line: 1,
  15. column: 0
  16. };
  17. this._sourcePosition = {
  18. identifierName: null,
  19. line: null,
  20. column: null,
  21. filename: null
  22. };
  23. this._disallowedPop = null;
  24. this._map = map;
  25. }
  26. get() {
  27. this._flush();
  28. const map = this._map;
  29. const result = {
  30. code: this._buf.join("").trimRight(),
  31. map: null,
  32. rawMappings: map == null ? void 0 : map.getRawMappings()
  33. };
  34. if (map) {
  35. Object.defineProperty(result, "map", {
  36. configurable: true,
  37. enumerable: true,
  38. get() {
  39. return this.map = map.get();
  40. },
  41. set(value) {
  42. Object.defineProperty(this, "map", {
  43. value,
  44. writable: true
  45. });
  46. }
  47. });
  48. }
  49. return result;
  50. }
  51. append(str) {
  52. this._flush();
  53. const {
  54. line,
  55. column,
  56. filename,
  57. identifierName,
  58. force
  59. } = this._sourcePosition;
  60. this._append(str, line, column, identifierName, filename, force);
  61. }
  62. queue(str) {
  63. if (str === "\n") {
  64. while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
  65. this._queue.shift();
  66. }
  67. }
  68. const {
  69. line,
  70. column,
  71. filename,
  72. identifierName,
  73. force
  74. } = this._sourcePosition;
  75. this._queue.unshift([str, line, column, identifierName, filename, force]);
  76. }
  77. _flush() {
  78. let item;
  79. while (item = this._queue.pop()) this._append(...item);
  80. }
  81. _append(str, line, column, identifierName, filename, force) {
  82. this._buf.push(str);
  83. this._last = str[str.length - 1];
  84. let i = str.indexOf("\n");
  85. let last = 0;
  86. if (i !== 0) {
  87. this._mark(line, column, identifierName, filename, force);
  88. }
  89. while (i !== -1) {
  90. this._position.line++;
  91. this._position.column = 0;
  92. last = i + 1;
  93. if (last < str.length) {
  94. this._mark(++line, 0, identifierName, filename, force);
  95. }
  96. i = str.indexOf("\n", last);
  97. }
  98. this._position.column += str.length - last;
  99. }
  100. _mark(line, column, identifierName, filename, force) {
  101. var _this$_map;
  102. (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
  103. }
  104. removeTrailingNewline() {
  105. if (this._queue.length > 0 && this._queue[0][0] === "\n") {
  106. this._queue.shift();
  107. }
  108. }
  109. removeLastSemicolon() {
  110. if (this._queue.length > 0 && this._queue[0][0] === ";") {
  111. this._queue.shift();
  112. }
  113. }
  114. endsWith(suffix) {
  115. if (suffix.length === 1) {
  116. let last;
  117. if (this._queue.length > 0) {
  118. const str = this._queue[0][0];
  119. last = str[str.length - 1];
  120. } else {
  121. last = this._last;
  122. }
  123. return last === suffix;
  124. }
  125. const end = this._last + this._queue.reduce((acc, item) => item[0] + acc, "");
  126. if (suffix.length <= end.length) {
  127. return end.slice(-suffix.length) === suffix;
  128. }
  129. return false;
  130. }
  131. hasContent() {
  132. return this._queue.length > 0 || !!this._last;
  133. }
  134. exactSource(loc, cb) {
  135. this.source("start", loc, true);
  136. cb();
  137. this.source("end", loc);
  138. this._disallowPop("start", loc);
  139. }
  140. source(prop, loc, force) {
  141. if (prop && !loc) return;
  142. this._normalizePosition(prop, loc, this._sourcePosition, force);
  143. }
  144. withSource(prop, loc, cb) {
  145. if (!this._map) return cb();
  146. const originalLine = this._sourcePosition.line;
  147. const originalColumn = this._sourcePosition.column;
  148. const originalFilename = this._sourcePosition.filename;
  149. const originalIdentifierName = this._sourcePosition.identifierName;
  150. this.source(prop, loc);
  151. cb();
  152. if ((!this._sourcePosition.force || this._sourcePosition.line !== originalLine || this._sourcePosition.column !== originalColumn || this._sourcePosition.filename !== originalFilename) && (!this._disallowedPop || this._disallowedPop.line !== originalLine || this._disallowedPop.column !== originalColumn || this._disallowedPop.filename !== originalFilename)) {
  153. this._sourcePosition.line = originalLine;
  154. this._sourcePosition.column = originalColumn;
  155. this._sourcePosition.filename = originalFilename;
  156. this._sourcePosition.identifierName = originalIdentifierName;
  157. this._sourcePosition.force = false;
  158. this._disallowedPop = null;
  159. }
  160. }
  161. _disallowPop(prop, loc) {
  162. if (prop && !loc) return;
  163. this._disallowedPop = this._normalizePosition(prop, loc);
  164. }
  165. _normalizePosition(prop, loc, targetObj, force) {
  166. const pos = loc ? loc[prop] : null;
  167. if (targetObj === undefined) {
  168. targetObj = {
  169. identifierName: null,
  170. line: null,
  171. column: null,
  172. filename: null,
  173. force: false
  174. };
  175. }
  176. const origLine = targetObj.line;
  177. const origColumn = targetObj.column;
  178. const origFilename = targetObj.filename;
  179. targetObj.identifierName = prop === "start" && (loc == null ? void 0 : loc.identifierName) || null;
  180. targetObj.line = pos == null ? void 0 : pos.line;
  181. targetObj.column = pos == null ? void 0 : pos.column;
  182. targetObj.filename = loc == null ? void 0 : loc.filename;
  183. if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
  184. targetObj.force = force;
  185. }
  186. return targetObj;
  187. }
  188. getCurrentColumn() {
  189. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  190. const lastIndex = extra.lastIndexOf("\n");
  191. return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
  192. }
  193. getCurrentLine() {
  194. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  195. let count = 0;
  196. for (let i = 0; i < extra.length; i++) {
  197. if (extra[i] === "\n") count++;
  198. }
  199. return this._position.line + count;
  200. }
  201. }
  202. exports.default = Buffer;