buffer.js 8.5 KB

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