You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.7 KiB

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