buffer.js 5.6 KB

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