buffer.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _trimRight() {
  7. var data = _interopRequireDefault(require("trim-right"));
  8. _trimRight = function _trimRight() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. var SPACES_RE = /^[ \t]+$/;
  15. var Buffer = function () {
  16. function Buffer(map) {
  17. this._map = null;
  18. this._buf = [];
  19. this._last = "";
  20. this._queue = [];
  21. this._position = {
  22. line: 1,
  23. column: 0
  24. };
  25. this._sourcePosition = {
  26. identifierName: null,
  27. line: null,
  28. column: null,
  29. filename: null
  30. };
  31. this._map = map;
  32. }
  33. var _proto = Buffer.prototype;
  34. _proto.get = function get() {
  35. this._flush();
  36. var map = this._map;
  37. var result = {
  38. code: (0, _trimRight().default)(this._buf.join("")),
  39. map: null,
  40. rawMappings: map && map.getRawMappings()
  41. };
  42. if (map) {
  43. Object.defineProperty(result, "map", {
  44. configurable: true,
  45. enumerable: true,
  46. get: function get() {
  47. return this.map = map.get();
  48. },
  49. set: function set(value) {
  50. Object.defineProperty(this, "map", {
  51. value: value,
  52. writable: true
  53. });
  54. }
  55. });
  56. }
  57. return result;
  58. };
  59. _proto.append = function append(str) {
  60. this._flush();
  61. var _sourcePosition = this._sourcePosition,
  62. line = _sourcePosition.line,
  63. column = _sourcePosition.column,
  64. filename = _sourcePosition.filename,
  65. identifierName = _sourcePosition.identifierName;
  66. this._append(str, line, column, identifierName, filename);
  67. };
  68. _proto.queue = function queue(str) {
  69. if (str === "\n") {
  70. while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
  71. this._queue.shift();
  72. }
  73. }
  74. var _sourcePosition2 = this._sourcePosition,
  75. line = _sourcePosition2.line,
  76. column = _sourcePosition2.column,
  77. filename = _sourcePosition2.filename,
  78. identifierName = _sourcePosition2.identifierName;
  79. this._queue.unshift([str, line, column, identifierName, filename]);
  80. };
  81. _proto._flush = function _flush() {
  82. var item;
  83. while (item = this._queue.pop()) {
  84. this._append.apply(this, item);
  85. }
  86. };
  87. _proto._append = function _append(str, line, column, identifierName, filename) {
  88. if (this._map && str[0] !== "\n") {
  89. this._map.mark(this._position.line, this._position.column, line, column, identifierName, filename);
  90. }
  91. this._buf.push(str);
  92. this._last = str[str.length - 1];
  93. for (var i = 0; i < str.length; i++) {
  94. if (str[i] === "\n") {
  95. this._position.line++;
  96. this._position.column = 0;
  97. } else {
  98. this._position.column++;
  99. }
  100. }
  101. };
  102. _proto.removeTrailingNewline = function removeTrailingNewline() {
  103. if (this._queue.length > 0 && this._queue[0][0] === "\n") {
  104. this._queue.shift();
  105. }
  106. };
  107. _proto.removeLastSemicolon = function removeLastSemicolon() {
  108. if (this._queue.length > 0 && this._queue[0][0] === ";") {
  109. this._queue.shift();
  110. }
  111. };
  112. _proto.endsWith = function endsWith(suffix) {
  113. if (suffix.length === 1) {
  114. var last;
  115. if (this._queue.length > 0) {
  116. var str = this._queue[0][0];
  117. last = str[str.length - 1];
  118. } else {
  119. last = this._last;
  120. }
  121. return last === suffix;
  122. }
  123. var end = this._last + this._queue.reduce(function (acc, item) {
  124. return item[0] + acc;
  125. }, "");
  126. if (suffix.length <= end.length) {
  127. return end.slice(-suffix.length) === suffix;
  128. }
  129. return false;
  130. };
  131. _proto.hasContent = function hasContent() {
  132. return this._queue.length > 0 || !!this._last;
  133. };
  134. _proto.source = function source(prop, loc) {
  135. if (prop && !loc) return;
  136. var pos = loc ? loc[prop] : null;
  137. this._sourcePosition.identifierName = loc && loc.identifierName || null;
  138. this._sourcePosition.line = pos ? pos.line : null;
  139. this._sourcePosition.column = pos ? pos.column : null;
  140. this._sourcePosition.filename = loc && loc.filename || null;
  141. };
  142. _proto.withSource = function withSource(prop, loc, cb) {
  143. if (!this._map) return cb();
  144. var originalLine = this._sourcePosition.line;
  145. var originalColumn = this._sourcePosition.column;
  146. var originalFilename = this._sourcePosition.filename;
  147. var originalIdentifierName = this._sourcePosition.identifierName;
  148. this.source(prop, loc);
  149. cb();
  150. this._sourcePosition.line = originalLine;
  151. this._sourcePosition.column = originalColumn;
  152. this._sourcePosition.filename = originalFilename;
  153. this._sourcePosition.identifierName = originalIdentifierName;
  154. };
  155. _proto.getCurrentColumn = function getCurrentColumn() {
  156. var extra = this._queue.reduce(function (acc, item) {
  157. return item[0] + acc;
  158. }, "");
  159. var lastIndex = extra.lastIndexOf("\n");
  160. return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
  161. };
  162. _proto.getCurrentLine = function getCurrentLine() {
  163. var extra = this._queue.reduce(function (acc, item) {
  164. return item[0] + acc;
  165. }, "");
  166. var count = 0;
  167. for (var i = 0; i < extra.length; i++) {
  168. if (extra[i] === "\n") count++;
  169. }
  170. return this._position.line + count;
  171. };
  172. return Buffer;
  173. }();
  174. exports.default = Buffer;