buffer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. class Buffer {
  7. constructor(map) {
  8. this._map = null;
  9. this._buf = "";
  10. this._str = "";
  11. this._appendCount = 0;
  12. this._last = 0;
  13. this._queue = [];
  14. this._queueCursor = 0;
  15. this._position = {
  16. line: 1,
  17. column: 0
  18. };
  19. this._sourcePosition = {
  20. identifierName: undefined,
  21. line: undefined,
  22. column: undefined,
  23. filename: undefined
  24. };
  25. this._map = map;
  26. this._allocQueue();
  27. }
  28. _allocQueue() {
  29. const queue = this._queue;
  30. for (let i = 0; i < 16; i++) {
  31. queue.push({
  32. char: 0,
  33. repeat: 1,
  34. line: undefined,
  35. column: undefined,
  36. identifierName: undefined,
  37. filename: ""
  38. });
  39. }
  40. }
  41. _pushQueue(char, repeat, line, column, identifierName, filename) {
  42. const cursor = this._queueCursor;
  43. if (cursor === this._queue.length) {
  44. this._allocQueue();
  45. }
  46. const item = this._queue[cursor];
  47. item.char = char;
  48. item.repeat = repeat;
  49. item.line = line;
  50. item.column = column;
  51. item.identifierName = identifierName;
  52. item.filename = filename;
  53. this._queueCursor++;
  54. }
  55. _popQueue() {
  56. if (this._queueCursor === 0) {
  57. throw new Error("Cannot pop from empty queue");
  58. }
  59. return this._queue[--this._queueCursor];
  60. }
  61. get() {
  62. this._flush();
  63. const map = this._map;
  64. const result = {
  65. code: (this._buf + this._str).trimRight(),
  66. decodedMap: map == null ? void 0 : map.getDecoded(),
  67. get map() {
  68. const resultMap = map ? map.get() : null;
  69. result.map = resultMap;
  70. return resultMap;
  71. },
  72. set map(value) {
  73. Object.defineProperty(result, "map", {
  74. value,
  75. writable: true
  76. });
  77. },
  78. get rawMappings() {
  79. const mappings = map == null ? void 0 : map.getRawMappings();
  80. result.rawMappings = mappings;
  81. return mappings;
  82. },
  83. set rawMappings(value) {
  84. Object.defineProperty(result, "rawMappings", {
  85. value,
  86. writable: true
  87. });
  88. }
  89. };
  90. return result;
  91. }
  92. append(str, maybeNewline) {
  93. this._flush();
  94. this._append(str, this._sourcePosition, maybeNewline);
  95. }
  96. appendChar(char) {
  97. this._flush();
  98. this._appendChar(char, 1, this._sourcePosition);
  99. }
  100. queue(char) {
  101. if (char === 10) {
  102. while (this._queueCursor !== 0) {
  103. const char = this._queue[this._queueCursor - 1].char;
  104. if (char !== 32 && char !== 9) {
  105. break;
  106. }
  107. this._queueCursor--;
  108. }
  109. }
  110. const sourcePosition = this._sourcePosition;
  111. this._pushQueue(char, 1, sourcePosition.line, sourcePosition.column, sourcePosition.identifierName, sourcePosition.filename);
  112. }
  113. queueIndentation(char, repeat) {
  114. this._pushQueue(char, repeat, undefined, undefined, undefined, undefined);
  115. }
  116. _flush() {
  117. const queueCursor = this._queueCursor;
  118. const queue = this._queue;
  119. for (let i = 0; i < queueCursor; i++) {
  120. const item = queue[i];
  121. this._appendChar(item.char, item.repeat, item);
  122. }
  123. this._queueCursor = 0;
  124. }
  125. _appendChar(char, repeat, sourcePos) {
  126. this._last = char;
  127. this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char);
  128. if (char !== 10) {
  129. this._mark(sourcePos.line, sourcePos.column, sourcePos.identifierName, sourcePos.filename);
  130. this._position.column += repeat;
  131. } else {
  132. this._position.line++;
  133. this._position.column = 0;
  134. }
  135. }
  136. _append(str, sourcePos, maybeNewline) {
  137. const len = str.length;
  138. const position = this._position;
  139. this._last = str.charCodeAt(len - 1);
  140. if (++this._appendCount > 4096) {
  141. +this._str;
  142. this._buf += this._str;
  143. this._str = str;
  144. this._appendCount = 0;
  145. } else {
  146. this._str += str;
  147. }
  148. if (!maybeNewline && !this._map) {
  149. position.column += len;
  150. return;
  151. }
  152. const {
  153. column,
  154. identifierName,
  155. filename
  156. } = sourcePos;
  157. let line = sourcePos.line;
  158. let i = str.indexOf("\n");
  159. let last = 0;
  160. if (i !== 0) {
  161. this._mark(line, column, identifierName, filename);
  162. }
  163. while (i !== -1) {
  164. position.line++;
  165. position.column = 0;
  166. last = i + 1;
  167. if (last < len) {
  168. this._mark(++line, 0, identifierName, filename);
  169. }
  170. i = str.indexOf("\n", last);
  171. }
  172. position.column += len - last;
  173. }
  174. _mark(line, column, identifierName, filename) {
  175. var _this$_map;
  176. (_this$_map = this._map) == null ? void 0 : _this$_map.mark(this._position, line, column, identifierName, filename);
  177. }
  178. removeTrailingNewline() {
  179. const queueCursor = this._queueCursor;
  180. if (queueCursor !== 0 && this._queue[queueCursor - 1].char === 10) {
  181. this._queueCursor--;
  182. }
  183. }
  184. removeLastSemicolon() {
  185. const queueCursor = this._queueCursor;
  186. if (queueCursor !== 0 && this._queue[queueCursor - 1].char === 59) {
  187. this._queueCursor--;
  188. }
  189. }
  190. getLastChar() {
  191. const queueCursor = this._queueCursor;
  192. return queueCursor !== 0 ? this._queue[queueCursor - 1].char : this._last;
  193. }
  194. getNewlineCount() {
  195. const queueCursor = this._queueCursor;
  196. let count = 0;
  197. if (queueCursor === 0) return this._last === 10 ? 1 : 0;
  198. for (let i = queueCursor - 1; i >= 0; i--) {
  199. if (this._queue[i].char !== 10) {
  200. break;
  201. }
  202. count++;
  203. }
  204. return count === queueCursor && this._last === 10 ? count + 1 : count;
  205. }
  206. endsWithCharAndNewline() {
  207. const queue = this._queue;
  208. const queueCursor = this._queueCursor;
  209. if (queueCursor !== 0) {
  210. const lastCp = queue[queueCursor - 1].char;
  211. if (lastCp !== 10) return;
  212. if (queueCursor > 1) {
  213. return queue[queueCursor - 2].char;
  214. } else {
  215. return this._last;
  216. }
  217. }
  218. }
  219. hasContent() {
  220. return this._queueCursor !== 0 || !!this._last;
  221. }
  222. exactSource(loc, cb) {
  223. if (!this._map) return cb();
  224. this.source("start", loc);
  225. cb();
  226. this.source("end", loc);
  227. }
  228. source(prop, loc) {
  229. if (!this._map) return;
  230. this._normalizePosition(prop, loc, 0, 0);
  231. }
  232. sourceWithOffset(prop, loc, lineOffset, columnOffset) {
  233. if (!this._map) return;
  234. this._normalizePosition(prop, loc, lineOffset, columnOffset);
  235. }
  236. withSource(prop, loc, cb) {
  237. if (!this._map) return cb();
  238. this.source(prop, loc);
  239. cb();
  240. }
  241. _normalizePosition(prop, loc, lineOffset, columnOffset) {
  242. const pos = loc[prop];
  243. const target = this._sourcePosition;
  244. target.identifierName = prop === "start" && loc.identifierName || undefined;
  245. if (pos) {
  246. target.line = pos.line + lineOffset;
  247. target.column = pos.column + columnOffset;
  248. target.filename = loc.filename;
  249. }
  250. }
  251. getCurrentColumn() {
  252. const queue = this._queue;
  253. const queueCursor = this._queueCursor;
  254. let lastIndex = -1;
  255. let len = 0;
  256. for (let i = 0; i < queueCursor; i++) {
  257. const item = queue[i];
  258. if (item.char === 10) {
  259. lastIndex = len;
  260. }
  261. len += item.repeat;
  262. }
  263. return lastIndex === -1 ? this._position.column + len : len - 1 - lastIndex;
  264. }
  265. getCurrentLine() {
  266. let count = 0;
  267. const queue = this._queue;
  268. for (let i = 0; i < this._queueCursor; i++) {
  269. if (queue[i].char === 10) {
  270. count++;
  271. }
  272. }
  273. return this._position.line + count;
  274. }
  275. }
  276. exports.default = Buffer;
  277. //# sourceMappingURL=buffer.js.map