buffer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. exports.__esModule = true;
  3. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  4. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  5. var _trimEnd = require("lodash/trimEnd");
  6. var _trimEnd2 = _interopRequireDefault(_trimEnd);
  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. return {
  31. code: (0, _trimEnd2.default)(this._buf.join("")),
  32. map: this._map ? this._map.get() : null
  33. };
  34. };
  35. Buffer.prototype.append = function append(str) {
  36. this._flush();
  37. var _sourcePosition = this._sourcePosition;
  38. var line = _sourcePosition.line;
  39. var column = _sourcePosition.column;
  40. var filename = _sourcePosition.filename;
  41. var identifierName = _sourcePosition.identifierName;
  42. this._append(str, line, column, identifierName, filename);
  43. };
  44. Buffer.prototype.queue = function queue(str) {
  45. if (str === "\n") while (this._queue.length > 0 && SPACES_RE.test(this._queue[0][0])) {
  46. this._queue.shift();
  47. }var _sourcePosition2 = this._sourcePosition;
  48. var line = _sourcePosition2.line;
  49. var column = _sourcePosition2.column;
  50. var filename = _sourcePosition2.filename;
  51. var identifierName = _sourcePosition2.identifierName;
  52. this._queue.unshift([str, line, column, identifierName, filename]);
  53. };
  54. Buffer.prototype._flush = function _flush() {
  55. var item = void 0;
  56. while (item = this._queue.pop()) {
  57. this._append.apply(this, item);
  58. }
  59. };
  60. Buffer.prototype._append = function _append(str, line, column, identifierName, filename) {
  61. if (this._map && str[0] !== "\n") {
  62. this._map.mark(this._position.line, this._position.column, line, column, identifierName, filename);
  63. }
  64. this._buf.push(str);
  65. this._last = str[str.length - 1];
  66. for (var i = 0; i < str.length; i++) {
  67. if (str[i] === "\n") {
  68. this._position.line++;
  69. this._position.column = 0;
  70. } else {
  71. this._position.column++;
  72. }
  73. }
  74. };
  75. Buffer.prototype.removeTrailingNewline = function removeTrailingNewline() {
  76. if (this._queue.length > 0 && this._queue[0][0] === "\n") this._queue.shift();
  77. };
  78. Buffer.prototype.removeLastSemicolon = function removeLastSemicolon() {
  79. if (this._queue.length > 0 && this._queue[0][0] === ";") this._queue.shift();
  80. };
  81. Buffer.prototype.endsWith = function endsWith(suffix) {
  82. if (suffix.length === 1) {
  83. var last = void 0;
  84. if (this._queue.length > 0) {
  85. var str = this._queue[0][0];
  86. last = str[str.length - 1];
  87. } else {
  88. last = this._last;
  89. }
  90. return last === suffix;
  91. }
  92. var end = this._last + this._queue.reduce(function (acc, item) {
  93. return item[0] + acc;
  94. }, "");
  95. if (suffix.length <= end.length) {
  96. return end.slice(-suffix.length) === suffix;
  97. }
  98. return false;
  99. };
  100. Buffer.prototype.hasContent = function hasContent() {
  101. return this._queue.length > 0 || !!this._last;
  102. };
  103. Buffer.prototype.source = function source(prop, loc) {
  104. if (prop && !loc) return;
  105. var pos = loc ? loc[prop] : null;
  106. this._sourcePosition.identifierName = loc && loc.identifierName || null;
  107. this._sourcePosition.line = pos ? pos.line : null;
  108. this._sourcePosition.column = pos ? pos.column : null;
  109. this._sourcePosition.filename = loc && loc.filename || null;
  110. };
  111. Buffer.prototype.withSource = function withSource(prop, loc, cb) {
  112. if (!this._map) return cb();
  113. var originalLine = this._sourcePosition.line;
  114. var originalColumn = this._sourcePosition.column;
  115. var originalFilename = this._sourcePosition.filename;
  116. var originalIdentifierName = this._sourcePosition.identifierName;
  117. this.source(prop, loc);
  118. cb();
  119. this._sourcePosition.line = originalLine;
  120. this._sourcePosition.column = originalColumn;
  121. this._sourcePosition.filename = originalFilename;
  122. this._sourcePosition.identifierName = originalIdentifierName;
  123. };
  124. Buffer.prototype.getCurrentColumn = function getCurrentColumn() {
  125. var extra = this._queue.reduce(function (acc, item) {
  126. return item[0] + acc;
  127. }, "");
  128. var lastIndex = extra.lastIndexOf("\n");
  129. return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
  130. };
  131. Buffer.prototype.getCurrentLine = function getCurrentLine() {
  132. var extra = this._queue.reduce(function (acc, item) {
  133. return item[0] + acc;
  134. }, "");
  135. var count = 0;
  136. for (var i = 0; i < extra.length; i++) {
  137. if (extra[i] === "\n") count++;
  138. }
  139. return this._position.line + count;
  140. };
  141. return Buffer;
  142. }();
  143. exports.default = Buffer;
  144. module.exports = exports["default"];