buffer.js 5.5 KB

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