buffer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. const spaceIndents = [];
  7. for (let i = 0; i < 32; i++) {
  8. spaceIndents.push(" ".repeat(i * 2));
  9. }
  10. class Buffer {
  11. constructor(map, indentChar) {
  12. this._map = null;
  13. this._buf = "";
  14. this._str = "";
  15. this._appendCount = 0;
  16. this._last = 0;
  17. this._canMarkIdName = true;
  18. this._indentChar = "";
  19. this._queuedChar = 0;
  20. this._position = {
  21. line: 1,
  22. column: 0
  23. };
  24. this._sourcePosition = {
  25. identifierName: undefined,
  26. identifierNamePos: undefined,
  27. line: undefined,
  28. column: undefined,
  29. filename: undefined
  30. };
  31. this._map = map;
  32. this._indentChar = indentChar;
  33. }
  34. get() {
  35. const {
  36. _map,
  37. _last
  38. } = this;
  39. if (this._queuedChar !== 32) {
  40. this._flush();
  41. }
  42. const code = _last === 10 ? (this._buf + this._str).trimRight() : this._buf + this._str;
  43. if (_map === null) {
  44. return {
  45. code: code,
  46. decodedMap: undefined,
  47. map: null,
  48. rawMappings: undefined
  49. };
  50. }
  51. const result = {
  52. code: code,
  53. decodedMap: _map.getDecoded(),
  54. get __mergedMap() {
  55. return this.map;
  56. },
  57. get map() {
  58. const resultMap = _map.get();
  59. result.map = resultMap;
  60. return resultMap;
  61. },
  62. set map(value) {
  63. Object.defineProperty(result, "map", {
  64. value,
  65. writable: true
  66. });
  67. },
  68. get rawMappings() {
  69. const mappings = _map.getRawMappings();
  70. result.rawMappings = mappings;
  71. return mappings;
  72. },
  73. set rawMappings(value) {
  74. Object.defineProperty(result, "rawMappings", {
  75. value,
  76. writable: true
  77. });
  78. }
  79. };
  80. return result;
  81. }
  82. append(str, maybeNewline) {
  83. this._flush();
  84. this._append(str, maybeNewline);
  85. }
  86. appendChar(char) {
  87. this._flush();
  88. this._appendChar(char, 1, true);
  89. }
  90. queue(char) {
  91. this._flush();
  92. this._queuedChar = char;
  93. }
  94. _flush() {
  95. const queuedChar = this._queuedChar;
  96. if (queuedChar !== 0) {
  97. this._appendChar(queuedChar, 1, true);
  98. this._queuedChar = 0;
  99. }
  100. }
  101. _appendChar(char, repeat, useSourcePos) {
  102. this._last = char;
  103. if (char === -1) {
  104. const indent = repeat >= 64 ? this._indentChar.repeat(repeat) : spaceIndents[repeat / 2];
  105. this._str += indent;
  106. } else {
  107. this._str += repeat > 1 ? String.fromCharCode(char).repeat(repeat) : String.fromCharCode(char);
  108. }
  109. const isSpace = char === 32;
  110. const position = this._position;
  111. if (char !== 10) {
  112. if (this._map) {
  113. const sourcePos = this._sourcePosition;
  114. if (useSourcePos && sourcePos) {
  115. this._map.mark(position, sourcePos.line, sourcePos.column, isSpace ? undefined : sourcePos.identifierName, isSpace ? undefined : sourcePos.identifierNamePos, sourcePos.filename);
  116. if (!isSpace && this._canMarkIdName) {
  117. sourcePos.identifierName = undefined;
  118. sourcePos.identifierNamePos = undefined;
  119. }
  120. } else {
  121. this._map.mark(position);
  122. }
  123. }
  124. position.column += repeat;
  125. } else {
  126. position.line++;
  127. position.column = 0;
  128. }
  129. }
  130. _append(str, maybeNewline) {
  131. const len = str.length;
  132. const position = this._position;
  133. const sourcePos = this._sourcePosition;
  134. this._last = -1;
  135. if (++this._appendCount > 4096) {
  136. +this._str;
  137. this._buf += this._str;
  138. this._str = str;
  139. this._appendCount = 0;
  140. } else {
  141. this._str += str;
  142. }
  143. const hasMap = this._map !== null;
  144. if (!maybeNewline && !hasMap) {
  145. position.column += len;
  146. return;
  147. }
  148. const {
  149. column,
  150. identifierName,
  151. identifierNamePos,
  152. filename
  153. } = sourcePos;
  154. let line = sourcePos.line;
  155. if ((identifierName != null || identifierNamePos != null) && this._canMarkIdName) {
  156. sourcePos.identifierName = undefined;
  157. sourcePos.identifierNamePos = undefined;
  158. }
  159. let i = str.indexOf("\n");
  160. let last = 0;
  161. if (hasMap && i !== 0) {
  162. this._map.mark(position, line, column, identifierName, identifierNamePos, filename);
  163. }
  164. while (i !== -1) {
  165. position.line++;
  166. position.column = 0;
  167. last = i + 1;
  168. if (last < len && line !== undefined) {
  169. line++;
  170. if (hasMap) {
  171. this._map.mark(position, line, 0, undefined, undefined, filename);
  172. }
  173. }
  174. i = str.indexOf("\n", last);
  175. }
  176. position.column += len - last;
  177. }
  178. removeLastSemicolon() {
  179. if (this._queuedChar === 59) {
  180. this._queuedChar = 0;
  181. }
  182. }
  183. getLastChar(checkQueue) {
  184. if (!checkQueue) {
  185. return this._last;
  186. }
  187. const queuedChar = this._queuedChar;
  188. return queuedChar !== 0 ? queuedChar : this._last;
  189. }
  190. getNewlineCount() {
  191. return this._queuedChar === 0 && this._last === 10 ? 1 : 0;
  192. }
  193. hasContent() {
  194. return this._last !== 0;
  195. }
  196. exactSource(loc, cb) {
  197. if (!this._map) {
  198. cb();
  199. return;
  200. }
  201. this.source("start", loc);
  202. const identifierName = loc.identifierName;
  203. const sourcePos = this._sourcePosition;
  204. if (identifierName != null) {
  205. this._canMarkIdName = false;
  206. sourcePos.identifierName = identifierName;
  207. }
  208. cb();
  209. if (identifierName != null) {
  210. this._canMarkIdName = true;
  211. sourcePos.identifierName = undefined;
  212. sourcePos.identifierNamePos = undefined;
  213. }
  214. this.source("end", loc);
  215. }
  216. source(prop, loc) {
  217. if (!this._map) return;
  218. this._normalizePosition(prop, loc, 0);
  219. }
  220. sourceWithOffset(prop, loc, columnOffset) {
  221. if (!this._map) return;
  222. this._normalizePosition(prop, loc, columnOffset);
  223. }
  224. _normalizePosition(prop, loc, columnOffset) {
  225. this._flush();
  226. const pos = loc[prop];
  227. const target = this._sourcePosition;
  228. if (pos) {
  229. target.line = pos.line;
  230. target.column = Math.max(pos.column + columnOffset, 0);
  231. target.filename = loc.filename;
  232. }
  233. }
  234. getCurrentColumn() {
  235. return this._position.column + (this._queuedChar ? 1 : 0);
  236. }
  237. getCurrentLine() {
  238. return this._position.line;
  239. }
  240. }
  241. exports.default = Buffer;
  242. //# sourceMappingURL=buffer.js.map