buffer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 = 0;
  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.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()) {
  80. this._append(...item);
  81. }
  82. }
  83. _append(str, line, column, identifierName, filename, force) {
  84. this._buf += str;
  85. this._last = str.charCodeAt(str.length - 1);
  86. let i = str.indexOf("\n");
  87. let last = 0;
  88. if (i !== 0) {
  89. this._mark(line, column, identifierName, filename, force);
  90. }
  91. while (i !== -1) {
  92. this._position.line++;
  93. this._position.column = 0;
  94. last = i + 1;
  95. if (last < str.length) {
  96. this._mark(++line, 0, identifierName, filename, force);
  97. }
  98. i = str.indexOf("\n", last);
  99. }
  100. this._position.column += str.length - last;
  101. }
  102. _mark(line, column, identifierName, filename, force) {
  103. var _this$_map;
  104. (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
  105. }
  106. removeTrailingNewline() {
  107. if (this._queue.length > 0 && this._queue[0][0] === "\n") {
  108. this._queue.shift();
  109. }
  110. }
  111. removeLastSemicolon() {
  112. if (this._queue.length > 0 && this._queue[0][0] === ";") {
  113. this._queue.shift();
  114. }
  115. }
  116. getLastChar() {
  117. let last;
  118. if (this._queue.length > 0) {
  119. const str = this._queue[0][0];
  120. last = str.charCodeAt(0);
  121. } else {
  122. last = this._last;
  123. }
  124. return last;
  125. }
  126. endsWithCharAndNewline() {
  127. const queue = this._queue;
  128. if (queue.length > 0) {
  129. const last = queue[0][0];
  130. const lastCp = last.charCodeAt(0);
  131. if (lastCp !== 10) return;
  132. if (queue.length > 1) {
  133. const secondLast = queue[1][0];
  134. return secondLast.charCodeAt(0);
  135. } else {
  136. return this._last;
  137. }
  138. }
  139. }
  140. hasContent() {
  141. return this._queue.length > 0 || !!this._last;
  142. }
  143. exactSource(loc, cb) {
  144. this.source("start", loc, true);
  145. cb();
  146. this.source("end", loc);
  147. this._disallowPop("start", loc);
  148. }
  149. source(prop, loc, force) {
  150. if (prop && !loc) return;
  151. this._normalizePosition(prop, loc, this._sourcePosition, force);
  152. }
  153. withSource(prop, loc, cb) {
  154. if (!this._map) return cb();
  155. const originalLine = this._sourcePosition.line;
  156. const originalColumn = this._sourcePosition.column;
  157. const originalFilename = this._sourcePosition.filename;
  158. const originalIdentifierName = this._sourcePosition.identifierName;
  159. this.source(prop, loc);
  160. cb();
  161. 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)) {
  162. this._sourcePosition.line = originalLine;
  163. this._sourcePosition.column = originalColumn;
  164. this._sourcePosition.filename = originalFilename;
  165. this._sourcePosition.identifierName = originalIdentifierName;
  166. this._sourcePosition.force = false;
  167. this._disallowedPop = null;
  168. }
  169. }
  170. _disallowPop(prop, loc) {
  171. if (prop && !loc) return;
  172. this._disallowedPop = this._normalizePosition(prop, loc);
  173. }
  174. _normalizePosition(prop, loc, targetObj, force) {
  175. const pos = loc ? loc[prop] : null;
  176. if (targetObj === undefined) {
  177. targetObj = {
  178. identifierName: null,
  179. line: null,
  180. column: null,
  181. filename: null,
  182. force: false
  183. };
  184. }
  185. const origLine = targetObj.line;
  186. const origColumn = targetObj.column;
  187. const origFilename = targetObj.filename;
  188. targetObj.identifierName = prop === "start" && (loc == null ? void 0 : loc.identifierName) || null;
  189. targetObj.line = pos == null ? void 0 : pos.line;
  190. targetObj.column = pos == null ? void 0 : pos.column;
  191. targetObj.filename = loc == null ? void 0 : loc.filename;
  192. if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
  193. targetObj.force = force;
  194. }
  195. return targetObj;
  196. }
  197. getCurrentColumn() {
  198. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  199. const lastIndex = extra.lastIndexOf("\n");
  200. return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
  201. }
  202. getCurrentLine() {
  203. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  204. let count = 0;
  205. for (let i = 0; i < extra.length; i++) {
  206. if (extra[i] === "\n") count++;
  207. }
  208. return this._position.line + count;
  209. }
  210. }
  211. exports.default = Buffer;