buffer.js 5.7 KB

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