版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

chunk-magic-string.3a794426-QZYMQJYE.mjs 37 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. import {
  2. __spreadProps,
  3. __spreadValues
  4. } from "./chunk-E3WVRKG7.mjs";
  5. // node_modules/.pnpm/vitest@0.29.1/node_modules/vitest/dist/chunk-magic-string.3a794426.js
  6. var comma = ",".charCodeAt(0);
  7. var semicolon = ";".charCodeAt(0);
  8. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  9. var intToChar = new Uint8Array(64);
  10. var charToInt = new Uint8Array(128);
  11. for (let i = 0; i < chars.length; i++) {
  12. const c = chars.charCodeAt(i);
  13. intToChar[i] = c;
  14. charToInt[c] = i;
  15. }
  16. var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
  17. decode(buf) {
  18. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  19. return out.toString();
  20. }
  21. } : {
  22. decode(buf) {
  23. let out = "";
  24. for (let i = 0; i < buf.length; i++) {
  25. out += String.fromCharCode(buf[i]);
  26. }
  27. return out;
  28. }
  29. };
  30. function encode(decoded) {
  31. const state = new Int32Array(5);
  32. const bufLength = 1024 * 16;
  33. const subLength = bufLength - 36;
  34. const buf = new Uint8Array(bufLength);
  35. const sub = buf.subarray(0, subLength);
  36. let pos = 0;
  37. let out = "";
  38. for (let i = 0; i < decoded.length; i++) {
  39. const line = decoded[i];
  40. if (i > 0) {
  41. if (pos === bufLength) {
  42. out += td.decode(buf);
  43. pos = 0;
  44. }
  45. buf[pos++] = semicolon;
  46. }
  47. if (line.length === 0)
  48. continue;
  49. state[0] = 0;
  50. for (let j = 0; j < line.length; j++) {
  51. const segment = line[j];
  52. if (pos > subLength) {
  53. out += td.decode(sub);
  54. buf.copyWithin(0, subLength, pos);
  55. pos -= subLength;
  56. }
  57. if (j > 0)
  58. buf[pos++] = comma;
  59. pos = encodeInteger(buf, pos, state, segment, 0);
  60. if (segment.length === 1)
  61. continue;
  62. pos = encodeInteger(buf, pos, state, segment, 1);
  63. pos = encodeInteger(buf, pos, state, segment, 2);
  64. pos = encodeInteger(buf, pos, state, segment, 3);
  65. if (segment.length === 4)
  66. continue;
  67. pos = encodeInteger(buf, pos, state, segment, 4);
  68. }
  69. }
  70. return out + td.decode(buf.subarray(0, pos));
  71. }
  72. function encodeInteger(buf, pos, state, segment, j) {
  73. const next = segment[j];
  74. let num = next - state[j];
  75. state[j] = next;
  76. num = num < 0 ? -num << 1 | 1 : num << 1;
  77. do {
  78. let clamped = num & 31;
  79. num >>>= 5;
  80. if (num > 0)
  81. clamped |= 32;
  82. buf[pos++] = intToChar[clamped];
  83. } while (num > 0);
  84. return pos;
  85. }
  86. var BitSet = class {
  87. constructor(arg) {
  88. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  89. }
  90. add(n2) {
  91. this.bits[n2 >> 5] |= 1 << (n2 & 31);
  92. }
  93. has(n2) {
  94. return !!(this.bits[n2 >> 5] & 1 << (n2 & 31));
  95. }
  96. };
  97. var Chunk = class {
  98. constructor(start, end, content) {
  99. this.start = start;
  100. this.end = end;
  101. this.original = content;
  102. this.intro = "";
  103. this.outro = "";
  104. this.content = content;
  105. this.storeName = false;
  106. this.edited = false;
  107. {
  108. this.previous = null;
  109. this.next = null;
  110. }
  111. }
  112. appendLeft(content) {
  113. this.outro += content;
  114. }
  115. appendRight(content) {
  116. this.intro = this.intro + content;
  117. }
  118. clone() {
  119. const chunk = new Chunk(this.start, this.end, this.original);
  120. chunk.intro = this.intro;
  121. chunk.outro = this.outro;
  122. chunk.content = this.content;
  123. chunk.storeName = this.storeName;
  124. chunk.edited = this.edited;
  125. return chunk;
  126. }
  127. contains(index) {
  128. return this.start < index && index < this.end;
  129. }
  130. eachNext(fn) {
  131. let chunk = this;
  132. while (chunk) {
  133. fn(chunk);
  134. chunk = chunk.next;
  135. }
  136. }
  137. eachPrevious(fn) {
  138. let chunk = this;
  139. while (chunk) {
  140. fn(chunk);
  141. chunk = chunk.previous;
  142. }
  143. }
  144. edit(content, storeName, contentOnly) {
  145. this.content = content;
  146. if (!contentOnly) {
  147. this.intro = "";
  148. this.outro = "";
  149. }
  150. this.storeName = storeName;
  151. this.edited = true;
  152. return this;
  153. }
  154. prependLeft(content) {
  155. this.outro = content + this.outro;
  156. }
  157. prependRight(content) {
  158. this.intro = content + this.intro;
  159. }
  160. split(index) {
  161. const sliceIndex = index - this.start;
  162. const originalBefore = this.original.slice(0, sliceIndex);
  163. const originalAfter = this.original.slice(sliceIndex);
  164. this.original = originalBefore;
  165. const newChunk = new Chunk(index, this.end, originalAfter);
  166. newChunk.outro = this.outro;
  167. this.outro = "";
  168. this.end = index;
  169. if (this.edited) {
  170. newChunk.edit("", false);
  171. this.content = "";
  172. } else {
  173. this.content = originalBefore;
  174. }
  175. newChunk.next = this.next;
  176. if (newChunk.next)
  177. newChunk.next.previous = newChunk;
  178. newChunk.previous = this;
  179. this.next = newChunk;
  180. return newChunk;
  181. }
  182. toString() {
  183. return this.intro + this.content + this.outro;
  184. }
  185. trimEnd(rx) {
  186. this.outro = this.outro.replace(rx, "");
  187. if (this.outro.length)
  188. return true;
  189. const trimmed = this.content.replace(rx, "");
  190. if (trimmed.length) {
  191. if (trimmed !== this.content) {
  192. this.split(this.start + trimmed.length).edit("", void 0, true);
  193. }
  194. return true;
  195. } else {
  196. this.edit("", void 0, true);
  197. this.intro = this.intro.replace(rx, "");
  198. if (this.intro.length)
  199. return true;
  200. }
  201. }
  202. trimStart(rx) {
  203. this.intro = this.intro.replace(rx, "");
  204. if (this.intro.length)
  205. return true;
  206. const trimmed = this.content.replace(rx, "");
  207. if (trimmed.length) {
  208. if (trimmed !== this.content) {
  209. this.split(this.end - trimmed.length);
  210. this.edit("", void 0, true);
  211. }
  212. return true;
  213. } else {
  214. this.edit("", void 0, true);
  215. this.outro = this.outro.replace(rx, "");
  216. if (this.outro.length)
  217. return true;
  218. }
  219. }
  220. };
  221. function getBtoa() {
  222. if (typeof window !== "undefined" && typeof window.btoa === "function") {
  223. return (str) => window.btoa(unescape(encodeURIComponent(str)));
  224. } else if (typeof Buffer === "function") {
  225. return (str) => Buffer.from(str, "utf-8").toString("base64");
  226. } else {
  227. return () => {
  228. throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
  229. };
  230. }
  231. }
  232. var btoa = /* @__PURE__ */ getBtoa();
  233. var SourceMap = class {
  234. constructor(properties) {
  235. this.version = 3;
  236. this.file = properties.file;
  237. this.sources = properties.sources;
  238. this.sourcesContent = properties.sourcesContent;
  239. this.names = properties.names;
  240. this.mappings = encode(properties.mappings);
  241. }
  242. toString() {
  243. return JSON.stringify(this);
  244. }
  245. toUrl() {
  246. return "data:application/json;charset=utf-8;base64," + btoa(this.toString());
  247. }
  248. };
  249. function guessIndent(code) {
  250. const lines = code.split("\n");
  251. const tabbed = lines.filter((line) => /^\t+/.test(line));
  252. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  253. if (tabbed.length === 0 && spaced.length === 0) {
  254. return null;
  255. }
  256. if (tabbed.length >= spaced.length) {
  257. return " ";
  258. }
  259. const min = spaced.reduce((previous, current) => {
  260. const numSpaces = /^ +/.exec(current)[0].length;
  261. return Math.min(numSpaces, previous);
  262. }, Infinity);
  263. return new Array(min + 1).join(" ");
  264. }
  265. function getRelativePath(from, to) {
  266. const fromParts = from.split(/[/\\]/);
  267. const toParts = to.split(/[/\\]/);
  268. fromParts.pop();
  269. while (fromParts[0] === toParts[0]) {
  270. fromParts.shift();
  271. toParts.shift();
  272. }
  273. if (fromParts.length) {
  274. let i = fromParts.length;
  275. while (i--)
  276. fromParts[i] = "..";
  277. }
  278. return fromParts.concat(toParts).join("/");
  279. }
  280. var toString = Object.prototype.toString;
  281. function isObject(thing) {
  282. return toString.call(thing) === "[object Object]";
  283. }
  284. function getLocator(source) {
  285. const originalLines = source.split("\n");
  286. const lineOffsets = [];
  287. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  288. lineOffsets.push(pos);
  289. pos += originalLines[i].length + 1;
  290. }
  291. return function locate(index) {
  292. let i = 0;
  293. let j = lineOffsets.length;
  294. while (i < j) {
  295. const m = i + j >> 1;
  296. if (index < lineOffsets[m]) {
  297. j = m;
  298. } else {
  299. i = m + 1;
  300. }
  301. }
  302. const line = i - 1;
  303. const column = index - lineOffsets[line];
  304. return { line, column };
  305. };
  306. }
  307. var Mappings = class {
  308. constructor(hires) {
  309. this.hires = hires;
  310. this.generatedCodeLine = 0;
  311. this.generatedCodeColumn = 0;
  312. this.raw = [];
  313. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  314. this.pending = null;
  315. }
  316. addEdit(sourceIndex, content, loc, nameIndex) {
  317. if (content.length) {
  318. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  319. if (nameIndex >= 0) {
  320. segment.push(nameIndex);
  321. }
  322. this.rawSegments.push(segment);
  323. } else if (this.pending) {
  324. this.rawSegments.push(this.pending);
  325. }
  326. this.advance(content);
  327. this.pending = null;
  328. }
  329. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  330. let originalCharIndex = chunk.start;
  331. let first = true;
  332. while (originalCharIndex < chunk.end) {
  333. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  334. this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]);
  335. }
  336. if (original[originalCharIndex] === "\n") {
  337. loc.line += 1;
  338. loc.column = 0;
  339. this.generatedCodeLine += 1;
  340. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  341. this.generatedCodeColumn = 0;
  342. first = true;
  343. } else {
  344. loc.column += 1;
  345. this.generatedCodeColumn += 1;
  346. first = false;
  347. }
  348. originalCharIndex += 1;
  349. }
  350. this.pending = null;
  351. }
  352. advance(str) {
  353. if (!str)
  354. return;
  355. const lines = str.split("\n");
  356. if (lines.length > 1) {
  357. for (let i = 0; i < lines.length - 1; i++) {
  358. this.generatedCodeLine++;
  359. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  360. }
  361. this.generatedCodeColumn = 0;
  362. }
  363. this.generatedCodeColumn += lines[lines.length - 1].length;
  364. }
  365. };
  366. var n = "\n";
  367. var warned = {
  368. insertLeft: false,
  369. insertRight: false,
  370. storeName: false
  371. };
  372. var MagicString = class {
  373. constructor(string, options = {}) {
  374. const chunk = new Chunk(0, string.length, string);
  375. Object.defineProperties(this, {
  376. original: { writable: true, value: string },
  377. outro: { writable: true, value: "" },
  378. intro: { writable: true, value: "" },
  379. firstChunk: { writable: true, value: chunk },
  380. lastChunk: { writable: true, value: chunk },
  381. lastSearchedChunk: { writable: true, value: chunk },
  382. byStart: { writable: true, value: {} },
  383. byEnd: { writable: true, value: {} },
  384. filename: { writable: true, value: options.filename },
  385. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  386. sourcemapLocations: { writable: true, value: new BitSet() },
  387. storedNames: { writable: true, value: {} },
  388. indentStr: { writable: true, value: void 0 }
  389. });
  390. this.byStart[0] = chunk;
  391. this.byEnd[string.length] = chunk;
  392. }
  393. addSourcemapLocation(char) {
  394. this.sourcemapLocations.add(char);
  395. }
  396. append(content) {
  397. if (typeof content !== "string")
  398. throw new TypeError("outro content must be a string");
  399. this.outro += content;
  400. return this;
  401. }
  402. appendLeft(index, content) {
  403. if (typeof content !== "string")
  404. throw new TypeError("inserted content must be a string");
  405. this._split(index);
  406. const chunk = this.byEnd[index];
  407. if (chunk) {
  408. chunk.appendLeft(content);
  409. } else {
  410. this.intro += content;
  411. }
  412. return this;
  413. }
  414. appendRight(index, content) {
  415. if (typeof content !== "string")
  416. throw new TypeError("inserted content must be a string");
  417. this._split(index);
  418. const chunk = this.byStart[index];
  419. if (chunk) {
  420. chunk.appendRight(content);
  421. } else {
  422. this.outro += content;
  423. }
  424. return this;
  425. }
  426. clone() {
  427. const cloned = new MagicString(this.original, { filename: this.filename });
  428. let originalChunk = this.firstChunk;
  429. let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
  430. while (originalChunk) {
  431. cloned.byStart[clonedChunk.start] = clonedChunk;
  432. cloned.byEnd[clonedChunk.end] = clonedChunk;
  433. const nextOriginalChunk = originalChunk.next;
  434. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  435. if (nextClonedChunk) {
  436. clonedChunk.next = nextClonedChunk;
  437. nextClonedChunk.previous = clonedChunk;
  438. clonedChunk = nextClonedChunk;
  439. }
  440. originalChunk = nextOriginalChunk;
  441. }
  442. cloned.lastChunk = clonedChunk;
  443. if (this.indentExclusionRanges) {
  444. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  445. }
  446. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  447. cloned.intro = this.intro;
  448. cloned.outro = this.outro;
  449. return cloned;
  450. }
  451. generateDecodedMap(options) {
  452. options = options || {};
  453. const sourceIndex = 0;
  454. const names = Object.keys(this.storedNames);
  455. const mappings = new Mappings(options.hires);
  456. const locate = getLocator(this.original);
  457. if (this.intro) {
  458. mappings.advance(this.intro);
  459. }
  460. this.firstChunk.eachNext((chunk) => {
  461. const loc = locate(chunk.start);
  462. if (chunk.intro.length)
  463. mappings.advance(chunk.intro);
  464. if (chunk.edited) {
  465. mappings.addEdit(
  466. sourceIndex,
  467. chunk.content,
  468. loc,
  469. chunk.storeName ? names.indexOf(chunk.original) : -1
  470. );
  471. } else {
  472. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  473. }
  474. if (chunk.outro.length)
  475. mappings.advance(chunk.outro);
  476. });
  477. return {
  478. file: options.file ? options.file.split(/[/\\]/).pop() : null,
  479. sources: [options.source ? getRelativePath(options.file || "", options.source) : null],
  480. sourcesContent: options.includeContent ? [this.original] : [null],
  481. names,
  482. mappings: mappings.raw
  483. };
  484. }
  485. generateMap(options) {
  486. return new SourceMap(this.generateDecodedMap(options));
  487. }
  488. _ensureindentStr() {
  489. if (this.indentStr === void 0) {
  490. this.indentStr = guessIndent(this.original);
  491. }
  492. }
  493. _getRawIndentString() {
  494. this._ensureindentStr();
  495. return this.indentStr;
  496. }
  497. getIndentString() {
  498. this._ensureindentStr();
  499. return this.indentStr === null ? " " : this.indentStr;
  500. }
  501. indent(indentStr, options) {
  502. const pattern = /^[^\r\n]/gm;
  503. if (isObject(indentStr)) {
  504. options = indentStr;
  505. indentStr = void 0;
  506. }
  507. if (indentStr === void 0) {
  508. this._ensureindentStr();
  509. indentStr = this.indentStr || " ";
  510. }
  511. if (indentStr === "")
  512. return this;
  513. options = options || {};
  514. const isExcluded = {};
  515. if (options.exclude) {
  516. const exclusions = typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude;
  517. exclusions.forEach((exclusion) => {
  518. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  519. isExcluded[i] = true;
  520. }
  521. });
  522. }
  523. let shouldIndentNextCharacter = options.indentStart !== false;
  524. const replacer = (match) => {
  525. if (shouldIndentNextCharacter)
  526. return `${indentStr}${match}`;
  527. shouldIndentNextCharacter = true;
  528. return match;
  529. };
  530. this.intro = this.intro.replace(pattern, replacer);
  531. let charIndex = 0;
  532. let chunk = this.firstChunk;
  533. while (chunk) {
  534. const end = chunk.end;
  535. if (chunk.edited) {
  536. if (!isExcluded[charIndex]) {
  537. chunk.content = chunk.content.replace(pattern, replacer);
  538. if (chunk.content.length) {
  539. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
  540. }
  541. }
  542. } else {
  543. charIndex = chunk.start;
  544. while (charIndex < end) {
  545. if (!isExcluded[charIndex]) {
  546. const char = this.original[charIndex];
  547. if (char === "\n") {
  548. shouldIndentNextCharacter = true;
  549. } else if (char !== "\r" && shouldIndentNextCharacter) {
  550. shouldIndentNextCharacter = false;
  551. if (charIndex === chunk.start) {
  552. chunk.prependRight(indentStr);
  553. } else {
  554. this._splitChunk(chunk, charIndex);
  555. chunk = chunk.next;
  556. chunk.prependRight(indentStr);
  557. }
  558. }
  559. }
  560. charIndex += 1;
  561. }
  562. }
  563. charIndex = chunk.end;
  564. chunk = chunk.next;
  565. }
  566. this.outro = this.outro.replace(pattern, replacer);
  567. return this;
  568. }
  569. insert() {
  570. throw new Error(
  571. "magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)"
  572. );
  573. }
  574. insertLeft(index, content) {
  575. if (!warned.insertLeft) {
  576. console.warn(
  577. "magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"
  578. );
  579. warned.insertLeft = true;
  580. }
  581. return this.appendLeft(index, content);
  582. }
  583. insertRight(index, content) {
  584. if (!warned.insertRight) {
  585. console.warn(
  586. "magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"
  587. );
  588. warned.insertRight = true;
  589. }
  590. return this.prependRight(index, content);
  591. }
  592. move(start, end, index) {
  593. if (index >= start && index <= end)
  594. throw new Error("Cannot move a selection inside itself");
  595. this._split(start);
  596. this._split(end);
  597. this._split(index);
  598. const first = this.byStart[start];
  599. const last = this.byEnd[end];
  600. const oldLeft = first.previous;
  601. const oldRight = last.next;
  602. const newRight = this.byStart[index];
  603. if (!newRight && last === this.lastChunk)
  604. return this;
  605. const newLeft = newRight ? newRight.previous : this.lastChunk;
  606. if (oldLeft)
  607. oldLeft.next = oldRight;
  608. if (oldRight)
  609. oldRight.previous = oldLeft;
  610. if (newLeft)
  611. newLeft.next = first;
  612. if (newRight)
  613. newRight.previous = last;
  614. if (!first.previous)
  615. this.firstChunk = last.next;
  616. if (!last.next) {
  617. this.lastChunk = first.previous;
  618. this.lastChunk.next = null;
  619. }
  620. first.previous = newLeft;
  621. last.next = newRight || null;
  622. if (!newLeft)
  623. this.firstChunk = first;
  624. if (!newRight)
  625. this.lastChunk = last;
  626. return this;
  627. }
  628. overwrite(start, end, content, options) {
  629. options = options || {};
  630. return this.update(start, end, content, __spreadProps(__spreadValues({}, options), { overwrite: !options.contentOnly }));
  631. }
  632. update(start, end, content, options) {
  633. if (typeof content !== "string")
  634. throw new TypeError("replacement content must be a string");
  635. while (start < 0)
  636. start += this.original.length;
  637. while (end < 0)
  638. end += this.original.length;
  639. if (end > this.original.length)
  640. throw new Error("end is out of bounds");
  641. if (start === end)
  642. throw new Error(
  643. "Cannot overwrite a zero-length range \u2013 use appendLeft or prependRight instead"
  644. );
  645. this._split(start);
  646. this._split(end);
  647. if (options === true) {
  648. if (!warned.storeName) {
  649. console.warn(
  650. "The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"
  651. );
  652. warned.storeName = true;
  653. }
  654. options = { storeName: true };
  655. }
  656. const storeName = options !== void 0 ? options.storeName : false;
  657. const overwrite = options !== void 0 ? options.overwrite : false;
  658. if (storeName) {
  659. const original = this.original.slice(start, end);
  660. Object.defineProperty(this.storedNames, original, {
  661. writable: true,
  662. value: true,
  663. enumerable: true
  664. });
  665. }
  666. const first = this.byStart[start];
  667. const last = this.byEnd[end];
  668. if (first) {
  669. let chunk = first;
  670. while (chunk !== last) {
  671. if (chunk.next !== this.byStart[chunk.end]) {
  672. throw new Error("Cannot overwrite across a split point");
  673. }
  674. chunk = chunk.next;
  675. chunk.edit("", false);
  676. }
  677. first.edit(content, storeName, !overwrite);
  678. } else {
  679. const newChunk = new Chunk(start, end, "").edit(content, storeName);
  680. last.next = newChunk;
  681. newChunk.previous = last;
  682. }
  683. return this;
  684. }
  685. prepend(content) {
  686. if (typeof content !== "string")
  687. throw new TypeError("outro content must be a string");
  688. this.intro = content + this.intro;
  689. return this;
  690. }
  691. prependLeft(index, content) {
  692. if (typeof content !== "string")
  693. throw new TypeError("inserted content must be a string");
  694. this._split(index);
  695. const chunk = this.byEnd[index];
  696. if (chunk) {
  697. chunk.prependLeft(content);
  698. } else {
  699. this.intro = content + this.intro;
  700. }
  701. return this;
  702. }
  703. prependRight(index, content) {
  704. if (typeof content !== "string")
  705. throw new TypeError("inserted content must be a string");
  706. this._split(index);
  707. const chunk = this.byStart[index];
  708. if (chunk) {
  709. chunk.prependRight(content);
  710. } else {
  711. this.outro = content + this.outro;
  712. }
  713. return this;
  714. }
  715. remove(start, end) {
  716. while (start < 0)
  717. start += this.original.length;
  718. while (end < 0)
  719. end += this.original.length;
  720. if (start === end)
  721. return this;
  722. if (start < 0 || end > this.original.length)
  723. throw new Error("Character is out of bounds");
  724. if (start > end)
  725. throw new Error("end must be greater than start");
  726. this._split(start);
  727. this._split(end);
  728. let chunk = this.byStart[start];
  729. while (chunk) {
  730. chunk.intro = "";
  731. chunk.outro = "";
  732. chunk.edit("");
  733. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  734. }
  735. return this;
  736. }
  737. lastChar() {
  738. if (this.outro.length)
  739. return this.outro[this.outro.length - 1];
  740. let chunk = this.lastChunk;
  741. do {
  742. if (chunk.outro.length)
  743. return chunk.outro[chunk.outro.length - 1];
  744. if (chunk.content.length)
  745. return chunk.content[chunk.content.length - 1];
  746. if (chunk.intro.length)
  747. return chunk.intro[chunk.intro.length - 1];
  748. } while (chunk = chunk.previous);
  749. if (this.intro.length)
  750. return this.intro[this.intro.length - 1];
  751. return "";
  752. }
  753. lastLine() {
  754. let lineIndex = this.outro.lastIndexOf(n);
  755. if (lineIndex !== -1)
  756. return this.outro.substr(lineIndex + 1);
  757. let lineStr = this.outro;
  758. let chunk = this.lastChunk;
  759. do {
  760. if (chunk.outro.length > 0) {
  761. lineIndex = chunk.outro.lastIndexOf(n);
  762. if (lineIndex !== -1)
  763. return chunk.outro.substr(lineIndex + 1) + lineStr;
  764. lineStr = chunk.outro + lineStr;
  765. }
  766. if (chunk.content.length > 0) {
  767. lineIndex = chunk.content.lastIndexOf(n);
  768. if (lineIndex !== -1)
  769. return chunk.content.substr(lineIndex + 1) + lineStr;
  770. lineStr = chunk.content + lineStr;
  771. }
  772. if (chunk.intro.length > 0) {
  773. lineIndex = chunk.intro.lastIndexOf(n);
  774. if (lineIndex !== -1)
  775. return chunk.intro.substr(lineIndex + 1) + lineStr;
  776. lineStr = chunk.intro + lineStr;
  777. }
  778. } while (chunk = chunk.previous);
  779. lineIndex = this.intro.lastIndexOf(n);
  780. if (lineIndex !== -1)
  781. return this.intro.substr(lineIndex + 1) + lineStr;
  782. return this.intro + lineStr;
  783. }
  784. slice(start = 0, end = this.original.length) {
  785. while (start < 0)
  786. start += this.original.length;
  787. while (end < 0)
  788. end += this.original.length;
  789. let result = "";
  790. let chunk = this.firstChunk;
  791. while (chunk && (chunk.start > start || chunk.end <= start)) {
  792. if (chunk.start < end && chunk.end >= end) {
  793. return result;
  794. }
  795. chunk = chunk.next;
  796. }
  797. if (chunk && chunk.edited && chunk.start !== start)
  798. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  799. const startChunk = chunk;
  800. while (chunk) {
  801. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  802. result += chunk.intro;
  803. }
  804. const containsEnd = chunk.start < end && chunk.end >= end;
  805. if (containsEnd && chunk.edited && chunk.end !== end)
  806. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  807. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  808. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  809. result += chunk.content.slice(sliceStart, sliceEnd);
  810. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  811. result += chunk.outro;
  812. }
  813. if (containsEnd) {
  814. break;
  815. }
  816. chunk = chunk.next;
  817. }
  818. return result;
  819. }
  820. // TODO deprecate this? not really very useful
  821. snip(start, end) {
  822. const clone = this.clone();
  823. clone.remove(0, start);
  824. clone.remove(end, clone.original.length);
  825. return clone;
  826. }
  827. _split(index) {
  828. if (this.byStart[index] || this.byEnd[index])
  829. return;
  830. let chunk = this.lastSearchedChunk;
  831. const searchForward = index > chunk.end;
  832. while (chunk) {
  833. if (chunk.contains(index))
  834. return this._splitChunk(chunk, index);
  835. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  836. }
  837. }
  838. _splitChunk(chunk, index) {
  839. if (chunk.edited && chunk.content.length) {
  840. const loc = getLocator(this.original)(index);
  841. throw new Error(
  842. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} \u2013 "${chunk.original}")`
  843. );
  844. }
  845. const newChunk = chunk.split(index);
  846. this.byEnd[index] = chunk;
  847. this.byStart[index] = newChunk;
  848. this.byEnd[newChunk.end] = newChunk;
  849. if (chunk === this.lastChunk)
  850. this.lastChunk = newChunk;
  851. this.lastSearchedChunk = chunk;
  852. return true;
  853. }
  854. toString() {
  855. let str = this.intro;
  856. let chunk = this.firstChunk;
  857. while (chunk) {
  858. str += chunk.toString();
  859. chunk = chunk.next;
  860. }
  861. return str + this.outro;
  862. }
  863. isEmpty() {
  864. let chunk = this.firstChunk;
  865. do {
  866. if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim())
  867. return false;
  868. } while (chunk = chunk.next);
  869. return true;
  870. }
  871. length() {
  872. let chunk = this.firstChunk;
  873. let length = 0;
  874. do {
  875. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  876. } while (chunk = chunk.next);
  877. return length;
  878. }
  879. trimLines() {
  880. return this.trim("[\\r\\n]");
  881. }
  882. trim(charType) {
  883. return this.trimStart(charType).trimEnd(charType);
  884. }
  885. trimEndAborted(charType) {
  886. const rx = new RegExp((charType || "\\s") + "+$");
  887. this.outro = this.outro.replace(rx, "");
  888. if (this.outro.length)
  889. return true;
  890. let chunk = this.lastChunk;
  891. do {
  892. const end = chunk.end;
  893. const aborted = chunk.trimEnd(rx);
  894. if (chunk.end !== end) {
  895. if (this.lastChunk === chunk) {
  896. this.lastChunk = chunk.next;
  897. }
  898. this.byEnd[chunk.end] = chunk;
  899. this.byStart[chunk.next.start] = chunk.next;
  900. this.byEnd[chunk.next.end] = chunk.next;
  901. }
  902. if (aborted)
  903. return true;
  904. chunk = chunk.previous;
  905. } while (chunk);
  906. return false;
  907. }
  908. trimEnd(charType) {
  909. this.trimEndAborted(charType);
  910. return this;
  911. }
  912. trimStartAborted(charType) {
  913. const rx = new RegExp("^" + (charType || "\\s") + "+");
  914. this.intro = this.intro.replace(rx, "");
  915. if (this.intro.length)
  916. return true;
  917. let chunk = this.firstChunk;
  918. do {
  919. const end = chunk.end;
  920. const aborted = chunk.trimStart(rx);
  921. if (chunk.end !== end) {
  922. if (chunk === this.lastChunk)
  923. this.lastChunk = chunk.next;
  924. this.byEnd[chunk.end] = chunk;
  925. this.byStart[chunk.next.start] = chunk.next;
  926. this.byEnd[chunk.next.end] = chunk.next;
  927. }
  928. if (aborted)
  929. return true;
  930. chunk = chunk.next;
  931. } while (chunk);
  932. return false;
  933. }
  934. trimStart(charType) {
  935. this.trimStartAborted(charType);
  936. return this;
  937. }
  938. hasChanged() {
  939. return this.original !== this.toString();
  940. }
  941. _replaceRegexp(searchValue, replacement) {
  942. function getReplacement(match, str) {
  943. if (typeof replacement === "string") {
  944. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  945. if (i === "$")
  946. return "$";
  947. if (i === "&")
  948. return match[0];
  949. const num = +i;
  950. if (num < match.length)
  951. return match[+i];
  952. return `$${i}`;
  953. });
  954. } else {
  955. return replacement(...match, match.index, str, match.groups);
  956. }
  957. }
  958. function matchAll(re, str) {
  959. let match;
  960. const matches = [];
  961. while (match = re.exec(str)) {
  962. matches.push(match);
  963. }
  964. return matches;
  965. }
  966. if (searchValue.global) {
  967. const matches = matchAll(searchValue, this.original);
  968. matches.forEach((match) => {
  969. if (match.index != null)
  970. this.overwrite(
  971. match.index,
  972. match.index + match[0].length,
  973. getReplacement(match, this.original)
  974. );
  975. });
  976. } else {
  977. const match = this.original.match(searchValue);
  978. if (match && match.index != null)
  979. this.overwrite(
  980. match.index,
  981. match.index + match[0].length,
  982. getReplacement(match, this.original)
  983. );
  984. }
  985. return this;
  986. }
  987. _replaceString(string, replacement) {
  988. const { original } = this;
  989. const index = original.indexOf(string);
  990. if (index !== -1) {
  991. this.overwrite(index, index + string.length, replacement);
  992. }
  993. return this;
  994. }
  995. replace(searchValue, replacement) {
  996. if (typeof searchValue === "string") {
  997. return this._replaceString(searchValue, replacement);
  998. }
  999. return this._replaceRegexp(searchValue, replacement);
  1000. }
  1001. _replaceAllString(string, replacement) {
  1002. const { original } = this;
  1003. const stringLength = string.length;
  1004. for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
  1005. this.overwrite(index, index + stringLength, replacement);
  1006. }
  1007. return this;
  1008. }
  1009. replaceAll(searchValue, replacement) {
  1010. if (typeof searchValue === "string") {
  1011. return this._replaceAllString(searchValue, replacement);
  1012. }
  1013. if (!searchValue.global) {
  1014. throw new TypeError(
  1015. "MagicString.prototype.replaceAll called with a non-global RegExp argument"
  1016. );
  1017. }
  1018. return this._replaceRegexp(searchValue, replacement);
  1019. }
  1020. };
  1021. var hasOwnProp = Object.prototype.hasOwnProperty;
  1022. var Bundle = class {
  1023. constructor(options = {}) {
  1024. this.intro = options.intro || "";
  1025. this.separator = options.separator !== void 0 ? options.separator : "\n";
  1026. this.sources = [];
  1027. this.uniqueSources = [];
  1028. this.uniqueSourceIndexByFilename = {};
  1029. }
  1030. addSource(source) {
  1031. if (source instanceof MagicString) {
  1032. return this.addSource({
  1033. content: source,
  1034. filename: source.filename,
  1035. separator: this.separator
  1036. });
  1037. }
  1038. if (!isObject(source) || !source.content) {
  1039. throw new Error(
  1040. "bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`"
  1041. );
  1042. }
  1043. ["filename", "indentExclusionRanges", "separator"].forEach((option) => {
  1044. if (!hasOwnProp.call(source, option))
  1045. source[option] = source.content[option];
  1046. });
  1047. if (source.separator === void 0) {
  1048. source.separator = this.separator;
  1049. }
  1050. if (source.filename) {
  1051. if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
  1052. this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
  1053. this.uniqueSources.push({ filename: source.filename, content: source.content.original });
  1054. } else {
  1055. const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
  1056. if (source.content.original !== uniqueSource.content) {
  1057. throw new Error(`Illegal source: same filename (${source.filename}), different contents`);
  1058. }
  1059. }
  1060. }
  1061. this.sources.push(source);
  1062. return this;
  1063. }
  1064. append(str, options) {
  1065. this.addSource({
  1066. content: new MagicString(str),
  1067. separator: options && options.separator || ""
  1068. });
  1069. return this;
  1070. }
  1071. clone() {
  1072. const bundle = new Bundle({
  1073. intro: this.intro,
  1074. separator: this.separator
  1075. });
  1076. this.sources.forEach((source) => {
  1077. bundle.addSource({
  1078. filename: source.filename,
  1079. content: source.content.clone(),
  1080. separator: source.separator
  1081. });
  1082. });
  1083. return bundle;
  1084. }
  1085. generateDecodedMap(options = {}) {
  1086. const names = [];
  1087. this.sources.forEach((source) => {
  1088. Object.keys(source.content.storedNames).forEach((name) => {
  1089. if (!~names.indexOf(name))
  1090. names.push(name);
  1091. });
  1092. });
  1093. const mappings = new Mappings(options.hires);
  1094. if (this.intro) {
  1095. mappings.advance(this.intro);
  1096. }
  1097. this.sources.forEach((source, i) => {
  1098. if (i > 0) {
  1099. mappings.advance(this.separator);
  1100. }
  1101. const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
  1102. const magicString = source.content;
  1103. const locate = getLocator(magicString.original);
  1104. if (magicString.intro) {
  1105. mappings.advance(magicString.intro);
  1106. }
  1107. magicString.firstChunk.eachNext((chunk) => {
  1108. const loc = locate(chunk.start);
  1109. if (chunk.intro.length)
  1110. mappings.advance(chunk.intro);
  1111. if (source.filename) {
  1112. if (chunk.edited) {
  1113. mappings.addEdit(
  1114. sourceIndex,
  1115. chunk.content,
  1116. loc,
  1117. chunk.storeName ? names.indexOf(chunk.original) : -1
  1118. );
  1119. } else {
  1120. mappings.addUneditedChunk(
  1121. sourceIndex,
  1122. chunk,
  1123. magicString.original,
  1124. loc,
  1125. magicString.sourcemapLocations
  1126. );
  1127. }
  1128. } else {
  1129. mappings.advance(chunk.content);
  1130. }
  1131. if (chunk.outro.length)
  1132. mappings.advance(chunk.outro);
  1133. });
  1134. if (magicString.outro) {
  1135. mappings.advance(magicString.outro);
  1136. }
  1137. });
  1138. return {
  1139. file: options.file ? options.file.split(/[/\\]/).pop() : null,
  1140. sources: this.uniqueSources.map((source) => {
  1141. return options.file ? getRelativePath(options.file, source.filename) : source.filename;
  1142. }),
  1143. sourcesContent: this.uniqueSources.map((source) => {
  1144. return options.includeContent ? source.content : null;
  1145. }),
  1146. names,
  1147. mappings: mappings.raw
  1148. };
  1149. }
  1150. generateMap(options) {
  1151. return new SourceMap(this.generateDecodedMap(options));
  1152. }
  1153. getIndentString() {
  1154. const indentStringCounts = {};
  1155. this.sources.forEach((source) => {
  1156. const indentStr = source.content._getRawIndentString();
  1157. if (indentStr === null)
  1158. return;
  1159. if (!indentStringCounts[indentStr])
  1160. indentStringCounts[indentStr] = 0;
  1161. indentStringCounts[indentStr] += 1;
  1162. });
  1163. return Object.keys(indentStringCounts).sort((a, b) => {
  1164. return indentStringCounts[a] - indentStringCounts[b];
  1165. })[0] || " ";
  1166. }
  1167. indent(indentStr) {
  1168. if (!arguments.length) {
  1169. indentStr = this.getIndentString();
  1170. }
  1171. if (indentStr === "")
  1172. return this;
  1173. let trailingNewline = !this.intro || this.intro.slice(-1) === "\n";
  1174. this.sources.forEach((source, i) => {
  1175. const separator = source.separator !== void 0 ? source.separator : this.separator;
  1176. const indentStart = trailingNewline || i > 0 && /\r?\n$/.test(separator);
  1177. source.content.indent(indentStr, {
  1178. exclude: source.indentExclusionRanges,
  1179. indentStart
  1180. //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
  1181. });
  1182. trailingNewline = source.content.lastChar() === "\n";
  1183. });
  1184. if (this.intro) {
  1185. this.intro = indentStr + this.intro.replace(/^[^\n]/gm, (match, index) => {
  1186. return index > 0 ? indentStr + match : match;
  1187. });
  1188. }
  1189. return this;
  1190. }
  1191. prepend(str) {
  1192. this.intro = str + this.intro;
  1193. return this;
  1194. }
  1195. toString() {
  1196. const body = this.sources.map((source, i) => {
  1197. const separator = source.separator !== void 0 ? source.separator : this.separator;
  1198. const str = (i > 0 ? separator : "") + source.content.toString();
  1199. return str;
  1200. }).join("");
  1201. return this.intro + body;
  1202. }
  1203. isEmpty() {
  1204. if (this.intro.length && this.intro.trim())
  1205. return false;
  1206. if (this.sources.some((source) => !source.content.isEmpty()))
  1207. return false;
  1208. return true;
  1209. }
  1210. length() {
  1211. return this.sources.reduce(
  1212. (length, source) => length + source.content.length(),
  1213. this.intro.length
  1214. );
  1215. }
  1216. trimLines() {
  1217. return this.trim("[\\r\\n]");
  1218. }
  1219. trim(charType) {
  1220. return this.trimStart(charType).trimEnd(charType);
  1221. }
  1222. trimStart(charType) {
  1223. const rx = new RegExp("^" + (charType || "\\s") + "+");
  1224. this.intro = this.intro.replace(rx, "");
  1225. if (!this.intro) {
  1226. let source;
  1227. let i = 0;
  1228. do {
  1229. source = this.sources[i++];
  1230. if (!source) {
  1231. break;
  1232. }
  1233. } while (!source.content.trimStartAborted(charType));
  1234. }
  1235. return this;
  1236. }
  1237. trimEnd(charType) {
  1238. const rx = new RegExp((charType || "\\s") + "+$");
  1239. let source;
  1240. let i = this.sources.length - 1;
  1241. do {
  1242. source = this.sources[i--];
  1243. if (!source) {
  1244. this.intro = this.intro.replace(rx, "");
  1245. break;
  1246. }
  1247. } while (!source.content.trimEndAborted(charType));
  1248. return this;
  1249. }
  1250. };
  1251. export {
  1252. Bundle,
  1253. SourceMap,
  1254. MagicString as default
  1255. };