buffer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _trimRight() {
  7. const data = _interopRequireDefault(require("trim-right"));
  8. _trimRight = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. const SPACES_RE = /^[ \t]+$/;
  15. class Buffer {
  16. constructor(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._disallowedPop = null;
  32. this._map = map;
  33. }
  34. get() {
  35. this._flush();
  36. const map = this._map;
  37. const 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() {
  47. return this.map = map.get();
  48. },
  49. set(value) {
  50. Object.defineProperty(this, "map", {
  51. value,
  52. writable: true
  53. });
  54. }
  55. });
  56. }
  57. return result;
  58. }
  59. append(str) {
  60. this._flush();
  61. const {
  62. line,
  63. column,
  64. filename,
  65. identifierName,
  66. force
  67. } = this._sourcePosition;
  68. this._append(str, line, column, identifierName, filename, force);
  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. force
  82. } = this._sourcePosition;
  83. this._queue.unshift([str, line, column, identifierName, filename, force]);
  84. }
  85. _flush() {
  86. let item;
  87. while (item = this._queue.pop()) this._append(...item);
  88. }
  89. _append(str, line, column, identifierName, filename, force) {
  90. if (this._map && str[0] !== "\n") {
  91. this._map.mark(this._position.line, this._position.column, line, column, identifierName, filename, force);
  92. }
  93. this._buf.push(str);
  94. this._last = str[str.length - 1];
  95. for (let i = 0; i < str.length; i++) {
  96. if (str[i] === "\n") {
  97. this._position.line++;
  98. this._position.column = 0;
  99. } else {
  100. this._position.column++;
  101. }
  102. }
  103. }
  104. removeTrailingNewline() {
  105. if (this._queue.length > 0 && this._queue[0][0] === "\n") {
  106. this._queue.shift();
  107. }
  108. }
  109. removeLastSemicolon() {
  110. if (this._queue.length > 0 && this._queue[0][0] === ";") {
  111. this._queue.shift();
  112. }
  113. }
  114. endsWith(suffix) {
  115. if (suffix.length === 1) {
  116. let last;
  117. if (this._queue.length > 0) {
  118. const str = this._queue[0][0];
  119. last = str[str.length - 1];
  120. } else {
  121. last = this._last;
  122. }
  123. return last === suffix;
  124. }
  125. const end = this._last + this._queue.reduce((acc, item) => item[0] + acc, "");
  126. if (suffix.length <= end.length) {
  127. return end.slice(-suffix.length) === suffix;
  128. }
  129. return false;
  130. }
  131. hasContent() {
  132. return this._queue.length > 0 || !!this._last;
  133. }
  134. exactSource(loc, cb) {
  135. this.source("start", loc, true);
  136. cb();
  137. this.source("end", loc);
  138. this._disallowPop("start", loc);
  139. }
  140. source(prop, loc, force) {
  141. if (prop && !loc) return;
  142. this._normalizePosition(prop, loc, this._sourcePosition, force);
  143. }
  144. withSource(prop, loc, cb) {
  145. if (!this._map) return cb();
  146. const originalLine = this._sourcePosition.line;
  147. const originalColumn = this._sourcePosition.column;
  148. const originalFilename = this._sourcePosition.filename;
  149. const originalIdentifierName = this._sourcePosition.identifierName;
  150. this.source(prop, loc);
  151. cb();
  152. if ((!this._sourcePosition.force || this._sourcePosition.line !== originalLine || this._sourcePosition.column !== originalColumn || this._sourcePosition.filename !== originalFilename) && (!this._disallowedPop || this._disallowedPop.line !== originalLine || this._disallowedPop.column !== originalColumn || this._disallowedPop.filename !== originalFilename)) {
  153. this._sourcePosition.line = originalLine;
  154. this._sourcePosition.column = originalColumn;
  155. this._sourcePosition.filename = originalFilename;
  156. this._sourcePosition.identifierName = originalIdentifierName;
  157. this._sourcePosition.force = false;
  158. this._disallowedPop = null;
  159. }
  160. }
  161. _disallowPop(prop, loc) {
  162. if (prop && !loc) return;
  163. this._disallowedPop = this._normalizePosition(prop, loc);
  164. }
  165. _normalizePosition(prop, loc, targetObj, force) {
  166. const pos = loc ? loc[prop] : null;
  167. if (targetObj === undefined) {
  168. targetObj = {
  169. identifierName: null,
  170. line: null,
  171. column: null,
  172. filename: null,
  173. force: false
  174. };
  175. }
  176. const origLine = targetObj.line;
  177. const origColumn = targetObj.column;
  178. const origFilename = targetObj.filename;
  179. targetObj.identifierName = prop === "start" && loc && loc.identifierName || null;
  180. targetObj.line = pos ? pos.line : null;
  181. targetObj.column = pos ? pos.column : null;
  182. targetObj.filename = loc && loc.filename || null;
  183. if (force || targetObj.line !== origLine || targetObj.column !== origColumn || targetObj.filename !== origFilename) {
  184. targetObj.force = force;
  185. }
  186. return targetObj;
  187. }
  188. getCurrentColumn() {
  189. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  190. const lastIndex = extra.lastIndexOf("\n");
  191. return lastIndex === -1 ? this._position.column + extra.length : extra.length - 1 - lastIndex;
  192. }
  193. getCurrentLine() {
  194. const extra = this._queue.reduce((acc, item) => item[0] + acc, "");
  195. let count = 0;
  196. for (let i = 0; i < extra.length; i++) {
  197. if (extra[i] === "\n") count++;
  198. }
  199. return this._position.line + count;
  200. }
  201. }
  202. exports.default = Buffer;