buffer.js 8.1 KB

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