trilean 1.13.12 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +1 -1
  2. package/dist/evaluator-operations.js +24 -4
  3. package/dist-cli/trilean.mjs +25 -5
  4. package/package.json +10 -20
  5. package/schemas/trilean.schema.json +1 -1
  6. package/dist/builtin-functions.cjs +0 -242
  7. package/dist/builtin-functions.d.cts +0 -13
  8. package/dist/complex-division.cjs +0 -47
  9. package/dist/complex-division.d.cts +0 -23
  10. package/dist/complex.cjs +0 -35
  11. package/dist/complex.d.cts +0 -23
  12. package/dist/computed-value.cjs +0 -80
  13. package/dist/computed-value.d.cts +0 -48
  14. package/dist/derived-aggregates.cjs +0 -73
  15. package/dist/derived-aggregates.d.cts +0 -9
  16. package/dist/derived-connectives.cjs +0 -39
  17. package/dist/derived-connectives.d.cts +0 -14
  18. package/dist/derived-patterns.cjs +0 -93
  19. package/dist/derived-patterns.d.cts +0 -4
  20. package/dist/derived-values.cjs +0 -23
  21. package/dist/derived-values.d.cts +0 -7
  22. package/dist/dist-ed78Tuwe.js +0 -1612
  23. package/dist/dist-ySX7cb5d.cjs +0 -1612
  24. package/dist/duration-order.cjs +0 -83
  25. package/dist/duration-order.d.cts +0 -21
  26. package/dist/evaluate-document.cjs +0 -92
  27. package/dist/evaluate-document.d.cts +0 -42
  28. package/dist/evaluation.cjs +0 -47
  29. package/dist/evaluation.d.cts +0 -29
  30. package/dist/evaluator-budget.cjs +0 -37
  31. package/dist/evaluator-budget.d.cts +0 -22
  32. package/dist/evaluator-collection.cjs +0 -33
  33. package/dist/evaluator-collection.d.cts +0 -21
  34. package/dist/evaluator-compiled.cjs +0 -310
  35. package/dist/evaluator-compiled.d.cts +0 -18
  36. package/dist/evaluator-effect.cjs +0 -57
  37. package/dist/evaluator-effect.d.cts +0 -40
  38. package/dist/evaluator-factory.cjs +0 -31
  39. package/dist/evaluator-factory.d.cts +0 -34
  40. package/dist/evaluator-literals.cjs +0 -42
  41. package/dist/evaluator-literals.d.cts +0 -8
  42. package/dist/evaluator-operations.cjs +0 -440
  43. package/dist/evaluator-operations.d.cts +0 -27
  44. package/dist/evaluator-test-helpers.cjs +0 -15030
  45. package/dist/evaluator-test-helpers.d.cts +0 -10
  46. package/dist/evaluator-test-helpers.d.ts +0 -10
  47. package/dist/evaluator-test-helpers.js +0 -14902
  48. package/dist/evaluator.cjs +0 -228
  49. package/dist/evaluator.d.cts +0 -12
  50. package/dist/functions.cjs +0 -4
  51. package/dist/functions.d.cts +0 -12
  52. package/dist/index.cjs +0 -97
  53. package/dist/index.d.cts +0 -18
  54. package/dist/input-resolvers.cjs +0 -109
  55. package/dist/input-resolvers.d.cts +0 -22
  56. package/dist/integer-power.cjs +0 -19
  57. package/dist/integer-power.d.cts +0 -11
  58. package/dist/json-value.cjs +0 -12
  59. package/dist/json-value.d.cts +0 -8
  60. package/dist/plain-record.cjs +0 -7
  61. package/dist/plain-record.d.cts +0 -2
  62. package/dist/resolvers.cjs +0 -0
  63. package/dist/resolvers.d.cts +0 -54
  64. package/dist/tree.cjs +0 -332
  65. package/dist/tree.d.cts +0 -446
@@ -1,1612 +0,0 @@
1
- // ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.6.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
2
- var comma = ",".charCodeAt(0);
3
- var semicolon = ";".charCodeAt(0);
4
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5
- var intToChar$1 = /* @__PURE__ */ new Uint8Array(64);
6
- var charToInt = /* @__PURE__ */ new Uint8Array(128);
7
- for (let i = 0; i < chars.length; i++) {
8
- const c = chars.charCodeAt(i);
9
- intToChar$1[i] = c;
10
- charToInt[c] = i;
11
- }
12
- function encodeInteger(builder, num) {
13
- do {
14
- let clamped = num & 31;
15
- num >>>= 5;
16
- if (num > 0) clamped |= 32;
17
- builder.write(intToChar$1[clamped]);
18
- } while (num > 0);
19
- }
20
- function encodeSign(num) {
21
- return num < 0 ? -num << 1 | 1 : num << 1;
22
- }
23
- var bufLength = 16384;
24
- var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? { decode(buf) {
25
- return Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString();
26
- } } : { decode(buf) {
27
- let out = "";
28
- for (let i = 0; i < buf.length; i++) out += String.fromCharCode(buf[i]);
29
- return out;
30
- } };
31
- var StringWriter = class {
32
- constructor() {
33
- this.pos = 0;
34
- this.out = "";
35
- this.buffer = new Uint8Array(bufLength);
36
- }
37
- write(v) {
38
- const { buffer } = this;
39
- buffer[this.pos++] = v;
40
- if (this.pos === bufLength) {
41
- this.out += td.decode(buffer);
42
- this.pos = 0;
43
- }
44
- }
45
- flush() {
46
- const { buffer, out, pos } = this;
47
- return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
48
- }
49
- };
50
- function encodeRangeMappings(decoded) {
51
- if (decoded.length === 0) return "";
52
- const writer = new StringWriter();
53
- for (let i = 0; i < decoded.length; i++) {
54
- const indices = decoded[i];
55
- if (i > 0) writer.write(semicolon);
56
- let index = 0;
57
- for (let j = 0; j < indices.length; j++) {
58
- const offset = indices[j];
59
- encodeInteger(writer, offset - index);
60
- index = offset;
61
- }
62
- }
63
- return writer.flush();
64
- }
65
- function encode(decoded) {
66
- const writer = new StringWriter();
67
- let sourcesIndex = 0;
68
- let sourceLine = 0;
69
- let sourceColumn = 0;
70
- let namesIndex = 0;
71
- for (let i = 0; i < decoded.length; i++) {
72
- const line = decoded[i];
73
- if (i > 0) writer.write(semicolon);
74
- if (line.length === 0) continue;
75
- let genColumn = 0;
76
- for (let j = 0; j < line.length; j++) {
77
- const segment = line[j];
78
- if (j > 0) writer.write(comma);
79
- encodeInteger(writer, encodeSign(segment[0] - genColumn));
80
- genColumn = segment[0];
81
- if (segment.length === 1) continue;
82
- encodeInteger(writer, encodeSign(segment[1] - sourcesIndex));
83
- encodeInteger(writer, encodeSign(segment[2] - sourceLine));
84
- encodeInteger(writer, encodeSign(segment[3] - sourceColumn));
85
- sourcesIndex = segment[1];
86
- sourceLine = segment[2];
87
- sourceColumn = segment[3];
88
- if (segment.length === 4) continue;
89
- encodeInteger(writer, encodeSign(segment[4] - namesIndex));
90
- namesIndex = segment[4];
91
- }
92
- }
93
- return writer.flush();
94
- }
95
- // ../../node_modules/.pnpm/magic-string@1.4.5/node_modules/magic-string/dist/index.mjs
96
- var BitSet = class BitSet {
97
- constructor(arg) {
98
- this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
99
- }
100
- add(n) {
101
- this.bits[n >> 5] |= 1 << (n & 31);
102
- }
103
- has(n) {
104
- return !!(this.bits[n >> 5] & 1 << (n & 31));
105
- }
106
- forEach(fn) {
107
- const bits = this.bits;
108
- for (let w = 0; w < bits.length; w++) {
109
- let word = bits[w];
110
- while (word) {
111
- const lowest = word & -word;
112
- fn((w << 5) + 31 - Math.clz32(lowest));
113
- word ^= lowest;
114
- }
115
- }
116
- }
117
- };
118
- var Chunk = class Chunk {
119
- constructor(start, end, content) {
120
- this.start = start;
121
- this.end = end;
122
- this.original = content;
123
- this.intro = "";
124
- this.outro = "";
125
- this.content = content;
126
- this.storeName = false;
127
- this.edited = false;
128
- this.replacesUntil = -1;
129
- this.replacedFrom = -1;
130
- this.previous = null;
131
- this.next = null;
132
- }
133
- appendLeft(content) {
134
- this.outro += content;
135
- }
136
- appendRight(content) {
137
- this.intro = this.intro + content;
138
- }
139
- clone() {
140
- const chunk = new Chunk(this.start, this.end, this.original);
141
- chunk.intro = this.intro;
142
- chunk.outro = this.outro;
143
- chunk.content = this.content;
144
- chunk.storeName = this.storeName;
145
- chunk.edited = this.edited;
146
- chunk.replacesUntil = this.replacesUntil;
147
- chunk.replacedFrom = this.replacedFrom;
148
- return chunk;
149
- }
150
- contains(index) {
151
- return this.start < index && index < this.end;
152
- }
153
- eachNext(fn) {
154
- fn(this);
155
- let chunk = this.next;
156
- while (chunk) {
157
- fn(chunk);
158
- chunk = chunk.next;
159
- }
160
- }
161
- eachPrevious(fn) {
162
- fn(this);
163
- let chunk = this.previous;
164
- while (chunk) {
165
- fn(chunk);
166
- chunk = chunk.previous;
167
- }
168
- }
169
- edit(content, storeName, contentOnly) {
170
- this.content = content;
171
- if (!contentOnly) {
172
- this.intro = "";
173
- this.outro = "";
174
- }
175
- this.storeName = storeName;
176
- this.edited = true;
177
- return this;
178
- }
179
- prependLeft(content) {
180
- this.outro = content + this.outro;
181
- }
182
- prependRight(content) {
183
- this.intro = content + this.intro;
184
- }
185
- reset() {
186
- this.intro = "";
187
- this.outro = "";
188
- this.replacesUntil = -1;
189
- this.replacedFrom = -1;
190
- if (this.edited) {
191
- this.content = this.original;
192
- this.storeName = false;
193
- this.edited = false;
194
- }
195
- }
196
- split(index) {
197
- const sliceIndex = index - this.start;
198
- const originalBefore = this.original.slice(0, sliceIndex);
199
- const originalAfter = this.original.slice(sliceIndex);
200
- this.original = originalBefore;
201
- const newChunk = new Chunk(index, this.end, originalAfter);
202
- newChunk.outro = this.outro;
203
- this.outro = "";
204
- this.end = index;
205
- if (this.edited) {
206
- newChunk.edit("", false, true);
207
- newChunk.replacedFrom = this.replacedFrom;
208
- } else this.content = originalBefore;
209
- newChunk.next = this.next;
210
- if (newChunk.next) newChunk.next.previous = newChunk;
211
- newChunk.previous = this;
212
- this.next = newChunk;
213
- return newChunk;
214
- }
215
- toString() {
216
- return this.intro + this.content + this.outro;
217
- }
218
- trimEnd(rx) {
219
- this.outro = this.outro.replace(rx, "");
220
- if (this.outro.length) return true;
221
- const trimmed = this.content.replace(rx, "");
222
- if (trimmed.length) {
223
- if (trimmed !== this.content) {
224
- if (this.edited) this.edit(trimmed, this.storeName, true);
225
- else this.split(this.start + trimmed.length).edit("", void 0, true);
226
- }
227
- return true;
228
- } else {
229
- this.edit("", void 0, true);
230
- this.intro = this.intro.replace(rx, "");
231
- if (this.intro.length) return true;
232
- }
233
- }
234
- trimStart(rx) {
235
- this.intro = this.intro.replace(rx, "");
236
- if (this.intro.length) return true;
237
- const trimmed = this.content.replace(rx, "");
238
- if (trimmed.length) {
239
- if (trimmed !== this.content) {
240
- if (this.edited) this.edit(trimmed, this.storeName, true);
241
- else {
242
- this.split(this.end - trimmed.length);
243
- this.edit("", void 0, true);
244
- }
245
- }
246
- return true;
247
- } else {
248
- this.edit("", void 0, true);
249
- this.outro = this.outro.replace(rx, "");
250
- if (this.outro.length) return true;
251
- }
252
- }
253
- };
254
- /**
255
- * The single error type thrown by MagicString.
256
- *
257
- * Every message is prefixed with `[MagicString]` so its source is obvious at a
258
- * glance, and is kept short and consistent in tone.
259
- */
260
- var MagicStringError = class extends Error {
261
- name = "MagicStringError";
262
- constructor(message, options) {
263
- super(`[MagicString] ${message}`, options);
264
- }
265
- };
266
- function getBtoa() {
267
- /* v8 ignore next -- environment fallback, unreachable when `btoa` is present */
268
- if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
269
- const buffer = globalThis["Buffer"];
270
- if (buffer) return (str) => buffer.from(str, "utf-8").toString("base64");
271
- return () => {
272
- throw new MagicStringError("unsupported environment: `btoa` or `Buffer` is required");
273
- };
274
- /* v8 ignore stop */
275
- }
276
- const btoa = /* #__PURE__ */ getBtoa();
277
- var SourceMap = class {
278
- constructor(properties) {
279
- this.version = 3;
280
- this.file = properties.file;
281
- this.sources = properties.sources;
282
- this.sourcesContent = properties.sourcesContent;
283
- this.names = properties.names;
284
- this.mappings = typeof properties.mappings === "string" ? properties.mappings : encode(properties.mappings);
285
- if (typeof properties.x_google_ignoreList !== "undefined") this.x_google_ignoreList = properties.x_google_ignoreList;
286
- if (typeof properties.debugId !== "undefined") this.debugId = properties.debugId;
287
- if (typeof properties.rangeMappings !== "undefined") {
288
- let shouldOutputRangeMapping = false;
289
- for (const line of properties.rangeMappings) if (line.length !== 0) {
290
- shouldOutputRangeMapping = true;
291
- break;
292
- }
293
- if (shouldOutputRangeMapping) this.rangeMappings = encodeRangeMappings(properties.rangeMappings);
294
- }
295
- }
296
- /**
297
- * Returns the equivalent of `JSON.stringify(map)`
298
- */
299
- toString() {
300
- return JSON.stringify(this);
301
- }
302
- /**
303
- * Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
304
- * `generateMap(options?: SourceMapOptions): SourceMap;`
305
- */
306
- toUrl() {
307
- return `data:application/json;charset=utf-8;base64,${btoa(this.toString())}`;
308
- }
309
- };
310
- function getLocator(source) {
311
- const lineOffsets = [0];
312
- for (let i = source.indexOf("\n"); i !== -1; i = source.indexOf("\n", i + 1)) lineOffsets.push(i + 1);
313
- return function locate(index) {
314
- let i = 0;
315
- let j = lineOffsets.length;
316
- while (i < j) {
317
- const m = i + j >> 1;
318
- if (index < lineOffsets[m]) j = m;
319
- else i = m + 1;
320
- }
321
- const line = i - 1;
322
- return {
323
- line,
324
- column: index - lineOffsets[line]
325
- };
326
- };
327
- }
328
- function getRelativePath(from, to) {
329
- const fromParts = from.split(/[/\\]/);
330
- const toParts = to.split(/[/\\]/);
331
- fromParts.pop();
332
- while (fromParts[0] === toParts[0]) {
333
- fromParts.shift();
334
- toParts.shift();
335
- }
336
- if (fromParts.length) {
337
- let i = fromParts.length;
338
- while (i--) fromParts[i] = "..";
339
- }
340
- return fromParts.concat(toParts).join("/");
341
- }
342
- function guessIndent(code) {
343
- const lines = code.split("\n");
344
- const tabbed = lines.filter((line) => /^\t+/.test(line));
345
- const spaced = lines.filter((line) => /^ {2,}/.test(line));
346
- if (tabbed.length === 0 && spaced.length === 0) return null;
347
- if (tabbed.length >= spaced.length) return " ";
348
- const min = spaced.reduce((previous, current) => {
349
- const numSpaces = /^ +/.exec(current)[0].length;
350
- return Math.min(numSpaces, previous);
351
- }, Infinity);
352
- return " ".repeat(min);
353
- }
354
- const toString = Object.prototype.toString;
355
- function isObject(thing) {
356
- return toString.call(thing) === "[object Object]";
357
- }
358
- const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
359
- const intToChar = /* #__PURE__ */ (() => {
360
- const chars = /* @__PURE__ */ new Uint8Array(64);
361
- for (let i = 0; i < 64; i++) chars[i] = BASE64_CHARS.charCodeAt(i);
362
- return chars;
363
- })();
364
- const COMMA = 44;
365
- const SEMICOLON = 59;
366
- const BUFFER_SIZE = 16384;
367
- const FLUSH_THRESHOLD = 16348;
368
- const scratch = /* #__PURE__ */ new Uint8Array(BUFFER_SIZE);
369
- function writeVlq(pos, num) {
370
- num = num < 0 ? -num << 1 | 1 : num << 1;
371
- do {
372
- let clamped = num & 31;
373
- num >>>= 5;
374
- if (num > 0) clamped |= 32;
375
- scratch[pos++] = intToChar[clamped];
376
- } while (num > 0);
377
- return pos;
378
- }
379
- const decoder = /* #__PURE__ */ new TextDecoder();
380
- var MappingsEncoder = class {
381
- constructor() {
382
- this.out = "";
383
- this.pos = 0;
384
- this.lines = [];
385
- this.buffered = 0;
386
- this.needsComma = false;
387
- this.prevGenColumn = 0;
388
- this.prevSourceIndex = 0;
389
- this.prevSourceLine = 0;
390
- this.prevSourceColumn = 0;
391
- this.prevNameIndex = 0;
392
- }
393
- endLine(segments) {
394
- this.lines.push(segments);
395
- this.buffered += segments.length + 1;
396
- if (this.buffered >= 4096) this.drain(null);
397
- }
398
- segments(segments) {
399
- this.drain(segments);
400
- }
401
- finish(segments) {
402
- this.drain(segments);
403
- this.flush();
404
- return this.out;
405
- }
406
- drain(trailing) {
407
- const lines = this.lines;
408
- const lineCount = lines.length;
409
- for (let l = 0; l <= lineCount; l++) {
410
- const line = l < lineCount ? lines[l] : trailing;
411
- if (line === null) break;
412
- for (let i = 0; i < line.length; i++) {
413
- if (this.pos > FLUSH_THRESHOLD) this.flush();
414
- const segment = line[i];
415
- if (this.needsComma) scratch[this.pos++] = COMMA;
416
- this.needsComma = true;
417
- this.pos = writeVlq(this.pos, segment[0] - this.prevGenColumn);
418
- this.prevGenColumn = segment[0];
419
- this.pos = writeVlq(this.pos, segment[1] - this.prevSourceIndex);
420
- this.prevSourceIndex = segment[1];
421
- this.pos = writeVlq(this.pos, segment[2] - this.prevSourceLine);
422
- this.prevSourceLine = segment[2];
423
- this.pos = writeVlq(this.pos, segment[3] - this.prevSourceColumn);
424
- this.prevSourceColumn = segment[3];
425
- if (segment.length === 5) {
426
- this.pos = writeVlq(this.pos, segment[4] - this.prevNameIndex);
427
- this.prevNameIndex = segment[4];
428
- }
429
- }
430
- if (l < lineCount) {
431
- if (this.pos > FLUSH_THRESHOLD) this.flush();
432
- scratch[this.pos++] = SEMICOLON;
433
- this.needsComma = false;
434
- this.prevGenColumn = 0;
435
- }
436
- }
437
- lines.length = 0;
438
- this.buffered = 0;
439
- this.flush();
440
- }
441
- flush() {
442
- this.out += decoder.decode(scratch.subarray(0, this.pos));
443
- this.pos = 0;
444
- }
445
- };
446
- const NEWLINE_CHAR$1 = 10;
447
- function isWordCode(code) {
448
- return code >= 97 && code <= 122 || code >= 65 && code <= 90 || code >= 48 && code <= 57 || code === 95;
449
- }
450
- var Mappings = class {
451
- constructor(hires, encoder = null) {
452
- this.hires = hires;
453
- this.generatedCodeLine = 0;
454
- this.generatedCodeColumn = 0;
455
- this.raw = [];
456
- this.rawSegments = this.raw[this.generatedCodeLine] = [];
457
- this.rawRangeMappingsIndices = [];
458
- this.rawRangeMappings = hires === "experimental-range" ? [this.rawRangeMappingsIndices] : void 0;
459
- this.encoder = encoder;
460
- }
461
- nextLine() {
462
- if (this.encoder === null) {
463
- this.generatedCodeLine += 1;
464
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
465
- } else {
466
- this.encoder.endLine(this.rawSegments);
467
- this.rawSegments = [];
468
- this.generatedCodeLine += 1;
469
- }
470
- this.generatedCodeColumn = 0;
471
- if (this.rawRangeMappings) this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
472
- }
473
- /**
474
- * Emits the mappings for one MagicString: its intro, every chunk (with the
475
- * chunk's own intro/outro) and its outro. `sourceIndex === -1` means the
476
- * string has no source of its own, so its content only advances the
477
- * generated position without mapping anything.
478
- */
479
- addMagicString(magicString, sourceIndex, nameIndex) {
480
- const { original } = magicString;
481
- const locate = getLocator(original);
482
- if (magicString.intro) this.advance(magicString.intro);
483
- magicString.firstChunk.eachNext((chunk) => {
484
- if (chunk.intro.length) this.advance(chunk.intro);
485
- if (sourceIndex === -1) this.advance(chunk.content);
486
- else if (chunk.edited) {
487
- const name = chunk.storeName ? nameIndex.get(original.slice(chunk.start, chunk.replacesUntil)) : void 0;
488
- this.addEdit(sourceIndex, chunk.content, locate(chunk.start), name ?? -1);
489
- } else this.addUneditedChunk(sourceIndex, chunk, original, locate(chunk.start), magicString.sourcemapLocations);
490
- if (chunk.outro.length) this.advance(chunk.outro);
491
- });
492
- if (magicString.outro) this.advance(magicString.outro);
493
- }
494
- addEdit(sourceIndex, content, loc, nameIndex) {
495
- if (content.length) {
496
- const contentLengthMinusOne = content.length - 1;
497
- let contentLineEnd = content.indexOf("\n", 0);
498
- let previousContentLineEnd = -1;
499
- while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
500
- const segment = [
501
- this.generatedCodeColumn,
502
- sourceIndex,
503
- loc.line,
504
- loc.column
505
- ];
506
- if (nameIndex >= 0) segment.push(nameIndex);
507
- this.rawSegments.push(segment);
508
- this.nextLine();
509
- previousContentLineEnd = contentLineEnd;
510
- contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
511
- }
512
- const segment = [
513
- this.generatedCodeColumn,
514
- sourceIndex,
515
- loc.line,
516
- loc.column
517
- ];
518
- if (nameIndex >= 0) segment.push(nameIndex);
519
- this.rawSegments.push(segment);
520
- this.advance(content.slice(previousContentLineEnd + 1));
521
- }
522
- }
523
- addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
524
- const end = chunk.end;
525
- let i = chunk.start;
526
- if (this.hires) {
527
- const boundary = this.hires === "boundary";
528
- const experimentalRange = this.hires === "experimental-range";
529
- const encoder = experimentalRange ? null : this.encoder;
530
- let charInHiresBoundary = false;
531
- while (i < end) {
532
- if (encoder !== null && this.rawSegments.length >= 4096) {
533
- encoder.segments(this.rawSegments);
534
- this.rawSegments.length = 0;
535
- }
536
- if (experimentalRange && i + 1 >= end) this.rawSegments.push([
537
- this.generatedCodeColumn,
538
- sourceIndex,
539
- loc.line,
540
- loc.column
541
- ]);
542
- const code = original.charCodeAt(i);
543
- if (code === NEWLINE_CHAR$1) {
544
- loc.line += 1;
545
- loc.column = 0;
546
- this.nextLine();
547
- charInHiresBoundary = false;
548
- } else {
549
- if (boundary) {
550
- if (isWordCode(code)) {
551
- if (!charInHiresBoundary) {
552
- this.rawSegments.push([
553
- this.generatedCodeColumn,
554
- sourceIndex,
555
- loc.line,
556
- loc.column
557
- ]);
558
- charInHiresBoundary = true;
559
- }
560
- } else {
561
- this.rawSegments.push([
562
- this.generatedCodeColumn,
563
- sourceIndex,
564
- loc.line,
565
- loc.column
566
- ]);
567
- charInHiresBoundary = false;
568
- }
569
- } else if (experimentalRange) {
570
- if (i === chunk.start) {
571
- this.rawRangeMappingsIndices.push(this.rawSegments.length);
572
- this.rawSegments.push([
573
- this.generatedCodeColumn,
574
- sourceIndex,
575
- loc.line,
576
- loc.column
577
- ]);
578
- }
579
- } else this.rawSegments.push([
580
- this.generatedCodeColumn,
581
- sourceIndex,
582
- loc.line,
583
- loc.column
584
- ]);
585
- loc.column += 1;
586
- this.generatedCodeColumn += 1;
587
- }
588
- i += 1;
589
- }
590
- } else {
591
- const bits = sourcemapLocations.bits;
592
- while (i < end) {
593
- let newline = original.indexOf("\n", i);
594
- if (newline === -1 || newline > end) newline = end;
595
- if (newline > i) {
596
- this.rawSegments.push([
597
- this.generatedCodeColumn,
598
- sourceIndex,
599
- loc.line,
600
- loc.column
601
- ]);
602
- for (let w = i + 1 >> 5, last = newline - 1 >> 5; w <= last; w++) {
603
- let word = bits[w];
604
- if (!word) continue;
605
- const base = w << 5;
606
- while (word) {
607
- const lowest = word & -word;
608
- const index = base + 31 - Math.clz32(lowest);
609
- if (index > i && index < newline) {
610
- const offset = index - i;
611
- this.rawSegments.push([
612
- this.generatedCodeColumn + offset,
613
- sourceIndex,
614
- loc.line,
615
- loc.column + offset
616
- ]);
617
- }
618
- word ^= lowest;
619
- }
620
- }
621
- loc.column += newline - i;
622
- this.generatedCodeColumn += newline - i;
623
- }
624
- if (newline === end) break;
625
- loc.line += 1;
626
- loc.column = 0;
627
- this.nextLine();
628
- i = newline + 1;
629
- }
630
- }
631
- }
632
- advance(str) {
633
- if (!str) return;
634
- const lastNewline = str.lastIndexOf("\n");
635
- for (let i = str.indexOf("\n"); i !== -1; i = str.indexOf("\n", i + 1)) this.nextLine();
636
- this.generatedCodeColumn += str.length - lastNewline - 1;
637
- }
638
- };
639
- const n = "\n";
640
- const NEWLINE_CHAR = "\n".charCodeAt(0);
641
- const CR_CHAR = "\r".charCodeAt(0);
642
- const warned = {
643
- insertLeft: false,
644
- insertRight: false,
645
- storeName: false
646
- };
647
- /**
648
- * Expands the `$` substitution patterns that `String.prototype.replace` accepts
649
- * in a string replacement.
650
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
651
- *
652
- * `captures` holds the capture groups in order, so `captures[0]` is `$1`.
653
- * `namedCaptures` is `undefined` when the pattern has no named groups, and
654
- * `$<name>` is then literal text - which is also the case for a string search
655
- * value, where there are no groups of either kind.
656
- */
657
- function expandReplacement(replacement, matched, position, str, captures, namedCaptures) {
658
- if (!replacement.includes("$")) return replacement;
659
- let result = "";
660
- let index = 0;
661
- while (index < replacement.length) {
662
- const dollar = replacement.indexOf("$", index);
663
- if (dollar === -1) {
664
- result += replacement.slice(index);
665
- break;
666
- }
667
- result += replacement.slice(index, dollar);
668
- const char = replacement[dollar + 1];
669
- let expansion = "$";
670
- let consumed = 1;
671
- if (char === "$") consumed = 2;
672
- else if (char === "&") {
673
- expansion = matched;
674
- consumed = 2;
675
- } else if (char === "`") {
676
- expansion = str.slice(0, position);
677
- consumed = 2;
678
- } else if (char === "'") {
679
- expansion = str.slice(position + matched.length);
680
- consumed = 2;
681
- } else if (char === "<" && namedCaptures !== void 0) {
682
- const close = replacement.indexOf(">", dollar + 2);
683
- if (close !== -1) {
684
- expansion = namedCaptures[replacement.slice(dollar + 2, close)] ?? "";
685
- consumed = close + 1 - dollar;
686
- }
687
- } else if (char >= "0" && char <= "9") {
688
- const second = replacement[dollar + 2];
689
- const double = second >= "0" && second <= "9" ? Number(char + second) : NaN;
690
- const single = Number(char);
691
- if (double >= 1 && double <= captures.length) {
692
- expansion = captures[double - 1] ?? "";
693
- consumed = 3;
694
- } else if (single >= 1 && single <= captures.length) {
695
- expansion = captures[single - 1] ?? "";
696
- consumed = 2;
697
- }
698
- }
699
- result += expansion;
700
- index = dollar + consumed;
701
- }
702
- return result;
703
- }
704
- var MagicString = class MagicString {
705
- /**
706
- * Wraps `string` for editing; every index passed to the editing methods refers to this original string.
707
- */
708
- constructor(string, options = {}) {
709
- const chunk = new Chunk(0, string.length, string);
710
- Object.defineProperties(this, {
711
- original: {
712
- writable: true,
713
- value: string
714
- },
715
- outro: {
716
- writable: true,
717
- value: ""
718
- },
719
- intro: {
720
- writable: true,
721
- value: ""
722
- },
723
- firstChunk: {
724
- writable: true,
725
- value: chunk
726
- },
727
- lastChunk: {
728
- writable: true,
729
- value: chunk
730
- },
731
- lastSearchedChunk: {
732
- writable: true,
733
- value: chunk
734
- },
735
- byStart: {
736
- writable: true,
737
- value: /* @__PURE__ */ new Map([[0, chunk]])
738
- },
739
- byEnd: {
740
- writable: true,
741
- value: /* @__PURE__ */ new Map([[string.length, chunk]])
742
- },
743
- filename: {
744
- writable: true,
745
- value: options.filename
746
- },
747
- indentExclusionRanges: {
748
- writable: true,
749
- value: options.indentExclusionRanges
750
- },
751
- sourcemapLocations: {
752
- writable: true,
753
- value: new BitSet()
754
- },
755
- storedNames: {
756
- writable: true,
757
- value: /* @__PURE__ */ new Set()
758
- },
759
- indentStr: {
760
- writable: true,
761
- value: void 0
762
- },
763
- ignoreList: {
764
- writable: true,
765
- value: options.ignoreList
766
- },
767
- offset: {
768
- writable: true,
769
- value: options.offset || 0
770
- },
771
- hasMovedChunks: {
772
- writable: true,
773
- value: false
774
- }
775
- });
776
- }
777
- /**
778
- * Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
779
- */
780
- addSourcemapLocation(char) {
781
- this.sourcemapLocations.add(char);
782
- }
783
- /**
784
- * Appends the specified content to the end of the string.
785
- */
786
- append(content) {
787
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
788
- this.outro += content;
789
- return this;
790
- }
791
- /**
792
- * Appends the specified content at the index in the original string.
793
- * If a range *ending* with index is subsequently moved, the insert will be moved with it.
794
- * See also `s.prependLeft(...)`.
795
- */
796
- appendLeft(index, content) {
797
- index = this._resolveIndex(index);
798
- if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
799
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
800
- this._split(index);
801
- const chunk = this.byEnd.get(index);
802
- if (chunk) chunk.appendLeft(content);
803
- else this.intro += content;
804
- return this;
805
- }
806
- /**
807
- * Appends the specified content at the index in the original string.
808
- * If a range *starting* with index is subsequently moved, the insert will be moved with it.
809
- * See also `s.prependRight(...)`.
810
- */
811
- appendRight(index, content) {
812
- index = this._resolveIndex(index);
813
- if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
814
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
815
- this._split(index);
816
- const chunk = this.byStart.get(index);
817
- if (chunk) chunk.appendRight(content);
818
- else this.outro += content;
819
- return this;
820
- }
821
- /**
822
- * Returns an independent copy: the chunk list, string-level intro/outro,
823
- * sourcemap locations, stored names, indent exclusion ranges, `offset`,
824
- * `filename` and `ignoreList` are all copied; later edits to either string
825
- * do not affect the other.
826
- */
827
- clone() {
828
- const cloned = new MagicString(this.original, {
829
- filename: this.filename,
830
- ignoreList: this.ignoreList,
831
- offset: this.offset
832
- });
833
- let originalChunk = this.firstChunk;
834
- let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
835
- while (originalChunk) {
836
- cloned.byStart.set(clonedChunk.start, clonedChunk);
837
- cloned.byEnd.set(clonedChunk.end, clonedChunk);
838
- const nextOriginalChunk = originalChunk.next;
839
- const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
840
- if (nextClonedChunk) {
841
- clonedChunk.next = nextClonedChunk;
842
- nextClonedChunk.previous = clonedChunk;
843
- clonedChunk = nextClonedChunk;
844
- }
845
- originalChunk = nextOriginalChunk;
846
- }
847
- cloned.lastChunk = clonedChunk;
848
- if (this.indentExclusionRanges) cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
849
- cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
850
- cloned.storedNames = new Set(this.storedNames);
851
- cloned.intro = this.intro;
852
- cloned.outro = this.outro;
853
- cloned.hasMovedChunks = this.hasMovedChunks;
854
- return cloned;
855
- }
856
- /**
857
- * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
858
- * Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
859
- */
860
- generateDecodedMap(options) {
861
- options = options || {};
862
- const mappings = new Mappings(options.hires);
863
- const names = this._generateMappings(mappings);
864
- return {
865
- ...this._mapProperties(options, names),
866
- mappings: mappings.raw,
867
- rangeMappings: mappings.rawRangeMappings
868
- };
869
- }
870
- /**
871
- * Generates a version 3 sourcemap.
872
- */
873
- generateMap(options) {
874
- options = options || {};
875
- const encoder = new MappingsEncoder();
876
- const mappings = new Mappings(options.hires, encoder);
877
- const names = this._generateMappings(mappings);
878
- return new SourceMap({
879
- ...this._mapProperties(options, names),
880
- mappings: encoder.finish(mappings.rawSegments),
881
- rangeMappings: mappings.rawRangeMappings
882
- });
883
- }
884
- /** @internal */
885
- _generateMappings(mappings) {
886
- const names = [...this.storedNames];
887
- mappings.addMagicString(this, 0, new Map(names.map((name, i) => [name, i])));
888
- return names;
889
- }
890
- /** @internal */
891
- _mapProperties(options, names) {
892
- return {
893
- file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
894
- sources: [options.source ? getRelativePath(options.file || "", options.source) : options.file || ""],
895
- sourcesContent: options.includeContent ? [this.original] : void 0,
896
- names,
897
- x_google_ignoreList: this.ignoreList ? [0] : void 0
898
- };
899
- }
900
- /** @internal */
901
- _ensureindentStr() {
902
- if (this.indentStr === void 0) this.indentStr = guessIndent(this.original);
903
- }
904
- /** @internal */
905
- _getRawIndentString() {
906
- this._ensureindentStr();
907
- return this.indentStr;
908
- }
909
- /**
910
- * Returns the indentation string guessed from the original content, falling back to a single tab character.
911
- * This is the value `s.indent()` uses when no prefix is supplied.
912
- */
913
- getIndentString() {
914
- this._ensureindentStr();
915
- return this.indentStr ?? " ";
916
- }
917
- indent(indentStr, options) {
918
- const pattern = /^[^\r\n]/gm;
919
- if (isObject(indentStr)) {
920
- options = indentStr;
921
- indentStr = void 0;
922
- }
923
- if (indentStr === void 0) {
924
- this._ensureindentStr();
925
- indentStr = this.indentStr || " ";
926
- }
927
- if (indentStr === "") return this;
928
- const resolvedIndentStr = indentStr;
929
- options = options || {};
930
- const isExcluded = new BitSet();
931
- if (options.exclude) (typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude).forEach((exclusion) => {
932
- for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded.add(i);
933
- });
934
- let shouldIndentNextCharacter = options.indentStart !== false;
935
- const indentPiece = (str) => {
936
- if (str === "") return str;
937
- const indented = str.replace(pattern, (match, offset) => offset > 0 || shouldIndentNextCharacter ? `${resolvedIndentStr}${match}` : match);
938
- shouldIndentNextCharacter = str[str.length - 1] === "\n";
939
- return indented;
940
- };
941
- this.intro = indentPiece(this.intro);
942
- let charIndex = 0;
943
- let chunk = this.firstChunk;
944
- const indentAt = (index) => {
945
- shouldIndentNextCharacter = false;
946
- if (index === chunk.start) chunk.appendRight(resolvedIndentStr);
947
- else {
948
- this._splitChunk(chunk, index);
949
- chunk = chunk.next;
950
- chunk.prependRight(resolvedIndentStr);
951
- }
952
- };
953
- while (chunk) {
954
- const end = chunk.end;
955
- if (!isExcluded.has(chunk.start)) chunk.intro = indentPiece(chunk.intro);
956
- if (chunk.edited) {
957
- if (!isExcluded.has(chunk.start)) chunk.content = indentPiece(chunk.content);
958
- else if (chunk.content) shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
959
- } else if (options.exclude) {
960
- charIndex = chunk.start;
961
- while (charIndex < end) {
962
- if (!isExcluded.has(charIndex)) {
963
- const char = this.original.charCodeAt(charIndex);
964
- if (char === NEWLINE_CHAR) shouldIndentNextCharacter = true;
965
- else if (char !== CR_CHAR && shouldIndentNextCharacter) indentAt(charIndex);
966
- }
967
- charIndex += 1;
968
- }
969
- } else {
970
- charIndex = chunk.start;
971
- while (charIndex < end) {
972
- if (!shouldIndentNextCharacter) {
973
- const nextLine = this.original.indexOf(n, charIndex);
974
- if (nextLine === -1 || nextLine >= end) break;
975
- shouldIndentNextCharacter = true;
976
- charIndex = nextLine + 1;
977
- continue;
978
- }
979
- const char = this.original.charCodeAt(charIndex);
980
- if (char === NEWLINE_CHAR || char === CR_CHAR) {
981
- charIndex += 1;
982
- continue;
983
- }
984
- indentAt(charIndex);
985
- charIndex += 1;
986
- }
987
- }
988
- if (!isExcluded.has(chunk.end - 1)) chunk.outro = indentPiece(chunk.outro);
989
- charIndex = chunk.end;
990
- chunk = chunk.next;
991
- }
992
- this.outro = indentPiece(this.outro);
993
- return this;
994
- }
995
- /** @internal */
996
- insert() {
997
- throw new MagicStringError("insert() is deprecated, use appendLeft() or prependRight()");
998
- }
999
- /** @internal */
1000
- insertLeft(index, content) {
1001
- if (!warned.insertLeft) {
1002
- console.warn("magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead");
1003
- warned.insertLeft = true;
1004
- }
1005
- return this.appendLeft(index, content);
1006
- }
1007
- /** @internal */
1008
- insertRight(index, content) {
1009
- if (!warned.insertRight) {
1010
- console.warn("magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead");
1011
- warned.insertRight = true;
1012
- }
1013
- return this.prependRight(index, content);
1014
- }
1015
- /**
1016
- * Moves the characters from `start` and `end` to `index`.
1017
- *
1018
- * `affinity` controls where the range is anchored at `index`. With the
1019
- * default `'right'`, it is inserted before the content that starts at `index`;
1020
- * with `'left'`, it is inserted after the content that ends at `index`. The
1021
- * two differ only when other content has already been moved to that boundary,
1022
- * mirroring the `appendLeft`/`appendRight` distinction.
1023
- */
1024
- move(start, end, index, affinity = "right") {
1025
- start = this._resolveIndex(start);
1026
- end = this._resolveIndex(end);
1027
- index = this._resolveIndex(index);
1028
- if (start === end) return this;
1029
- if (start < 0 || end > this.original.length || index < 0 || index > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1030
- if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
1031
- if (index >= start && index <= end) throw new MagicStringError("cannot move a selection inside itself");
1032
- this._split(start);
1033
- this._split(end);
1034
- this._split(index);
1035
- const first = this.byStart.get(start);
1036
- const last = this.byEnd.get(end);
1037
- if (!first || !last) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1038
- if (this.hasMovedChunks) {
1039
- let cursor = first;
1040
- while (cursor !== last) {
1041
- cursor = cursor.next;
1042
- if (!cursor || cursor.start < start || cursor.end > end) throw new MagicStringError(`cannot move ${start} to ${end} because an earlier move split that range`);
1043
- }
1044
- }
1045
- const oldLeft = first.previous;
1046
- const oldRight = last.next;
1047
- let newLeft;
1048
- let newRight;
1049
- if (affinity === "left") {
1050
- newLeft = this.byEnd.get(index) ?? null;
1051
- if (!newLeft) {
1052
- if (first === this.firstChunk) return this;
1053
- newRight = this.firstChunk;
1054
- } else {
1055
- if (newLeft.next === first) return this;
1056
- newRight = newLeft.next;
1057
- }
1058
- } else {
1059
- newRight = this.byStart.get(index) ?? null;
1060
- if (!newRight) {
1061
- if (last === this.lastChunk) return this;
1062
- newLeft = this.lastChunk;
1063
- } else {
1064
- if (newRight.previous === last) return this;
1065
- newLeft = newRight.previous;
1066
- }
1067
- }
1068
- if (oldLeft) oldLeft.next = oldRight;
1069
- if (oldRight) oldRight.previous = oldLeft;
1070
- if (newLeft) newLeft.next = first;
1071
- if (newRight) newRight.previous = last;
1072
- if (!oldLeft) this.firstChunk = oldRight;
1073
- if (!oldRight) {
1074
- this.lastChunk = oldLeft;
1075
- this.lastChunk.next = null;
1076
- }
1077
- first.previous = newLeft;
1078
- last.next = newRight || null;
1079
- if (!newLeft) this.firstChunk = first;
1080
- if (!newRight) this.lastChunk = last;
1081
- this.hasMovedChunks = true;
1082
- return this;
1083
- }
1084
- /**
1085
- * Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in
1086
- * that range. The same restrictions as `s.remove()` apply.
1087
- *
1088
- * The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
1089
- * for later inclusion in a sourcemap's names array - and a contentOnly property which determines whether only
1090
- * the content is overwritten, or anything that was appended/prepended to the range as well.
1091
- *
1092
- * It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
1093
- */
1094
- overwrite(start, end, content, options) {
1095
- if (options === true) {
1096
- if (!warned.storeName) {
1097
- console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string");
1098
- warned.storeName = true;
1099
- }
1100
- return this.update(start, end, content, {
1101
- storeName: true,
1102
- overwrite: true
1103
- });
1104
- }
1105
- const optionObject = typeof options === "object" && options ? options : {};
1106
- return this.update(start, end, content, {
1107
- ...optionObject,
1108
- overwrite: !optionObject.contentOnly
1109
- });
1110
- }
1111
- /**
1112
- * Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
1113
- *
1114
- * The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
1115
- * for later inclusion in a sourcemap's names array - and an overwrite property which determines whether only
1116
- * the content is overwritten, or anything that was appended/prepended to the range as well.
1117
- */
1118
- update(start, end, content, options) {
1119
- start = this._resolveIndex(start);
1120
- end = this._resolveIndex(end);
1121
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1122
- if (start < 0) throw new MagicStringError(`start ${start} is out of bounds`);
1123
- if (end > this.original.length) throw new MagicStringError(`end ${end} is out of bounds`);
1124
- if (start === end) throw new MagicStringError(`cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`);
1125
- if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
1126
- this._split(start);
1127
- this._split(end);
1128
- if (options === true) {
1129
- if (!warned.storeName) {
1130
- console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string");
1131
- warned.storeName = true;
1132
- }
1133
- options = { storeName: true };
1134
- }
1135
- const optionObject = typeof options === "object" && options ? options : {};
1136
- const storeName = optionObject.storeName || false;
1137
- const overwrite = optionObject.overwrite || false;
1138
- if (storeName) this.storedNames.add(this.original.slice(start, end));
1139
- const first = this.byStart.get(start);
1140
- for (let chunk = first; chunk.end < end;) {
1141
- const next = chunk.next;
1142
- if (next === null || next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
1143
- chunk = next;
1144
- }
1145
- for (let chunk = first.next; chunk !== null && chunk.start < end; chunk = chunk.next) {
1146
- chunk.edit("", false, !overwrite);
1147
- chunk.replacesUntil = -1;
1148
- chunk.replacedFrom = start;
1149
- }
1150
- first.edit(content, storeName, !overwrite);
1151
- first.replacesUntil = end;
1152
- first.replacedFrom = -1;
1153
- return this;
1154
- }
1155
- /**
1156
- * Prepends the string with the specified content.
1157
- */
1158
- prepend(content) {
1159
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1160
- this.intro = content + this.intro;
1161
- return this;
1162
- }
1163
- /**
1164
- * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
1165
- */
1166
- prependLeft(index, content) {
1167
- index = this._resolveIndex(index);
1168
- if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1169
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1170
- this._split(index);
1171
- const chunk = this.byEnd.get(index);
1172
- if (chunk) chunk.prependLeft(content);
1173
- else this.intro = content + this.intro;
1174
- return this;
1175
- }
1176
- /**
1177
- * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
1178
- */
1179
- prependRight(index, content) {
1180
- index = this._resolveIndex(index);
1181
- if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1182
- if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1183
- this._split(index);
1184
- const chunk = this.byStart.get(index);
1185
- if (chunk) chunk.prependRight(content);
1186
- else this.outro = content + this.outro;
1187
- return this;
1188
- }
1189
- /**
1190
- * Removes the characters from `start` to `end` (of the original string, **not** the generated string).
1191
- * Content appended or prepended at positions strictly inside the range is removed with it, while
1192
- * content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to remove
1193
- * the range including its edge inserts.
1194
- * Repeated removals and removals with partially overlapping ranges are allowed.
1195
- */
1196
- remove(start, end) {
1197
- start = this._resolveIndex(start);
1198
- end = this._resolveIndex(end);
1199
- if (start === end) return this;
1200
- if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1201
- if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
1202
- this._split(start);
1203
- this._split(end);
1204
- let chunk = this.byStart.get(start);
1205
- while (chunk) {
1206
- if (chunk.start > start) chunk.intro = "";
1207
- if (chunk.end < end) chunk.outro = "";
1208
- chunk.edit("", false, true);
1209
- chunk.replacesUntil = -1;
1210
- chunk.replacedFrom = -1;
1211
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1212
- }
1213
- return this;
1214
- }
1215
- /**
1216
- * Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
1217
- */
1218
- reset(start, end) {
1219
- start = this._resolveIndex(start);
1220
- end = this._resolveIndex(end);
1221
- if (start === end) return this;
1222
- if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1223
- if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
1224
- this._split(start);
1225
- this._split(end);
1226
- let chunk = this.byStart.get(start);
1227
- while (chunk) {
1228
- chunk.reset();
1229
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1230
- }
1231
- return this;
1232
- }
1233
- /**
1234
- * Returns the last character of the generated string, or `''` if it is empty.
1235
- */
1236
- lastChar() {
1237
- if (this.outro.length) return this.outro[this.outro.length - 1];
1238
- let chunk = this.lastChunk;
1239
- while (chunk) {
1240
- if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
1241
- if (chunk.content.length) return chunk.content[chunk.content.length - 1];
1242
- if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
1243
- chunk = chunk.previous;
1244
- }
1245
- if (this.intro.length) return this.intro[this.intro.length - 1];
1246
- return "";
1247
- }
1248
- /**
1249
- * Returns everything after the last newline in the generated string, or the whole string if there is no newline.
1250
- */
1251
- lastLine() {
1252
- let lineIndex = this.outro.lastIndexOf(n);
1253
- if (lineIndex !== -1) return this.outro.slice(lineIndex + 1);
1254
- let lineStr = this.outro;
1255
- let chunk = this.lastChunk;
1256
- while (chunk) {
1257
- if (chunk.outro.length > 0) {
1258
- lineIndex = chunk.outro.lastIndexOf(n);
1259
- if (lineIndex !== -1) return chunk.outro.slice(lineIndex + 1) + lineStr;
1260
- lineStr = chunk.outro + lineStr;
1261
- }
1262
- if (chunk.content.length > 0) {
1263
- lineIndex = chunk.content.lastIndexOf(n);
1264
- if (lineIndex !== -1) return chunk.content.slice(lineIndex + 1) + lineStr;
1265
- lineStr = chunk.content + lineStr;
1266
- }
1267
- if (chunk.intro.length > 0) {
1268
- lineIndex = chunk.intro.lastIndexOf(n);
1269
- if (lineIndex !== -1) return chunk.intro.slice(lineIndex + 1) + lineStr;
1270
- lineStr = chunk.intro + lineStr;
1271
- }
1272
- chunk = chunk.previous;
1273
- }
1274
- lineIndex = this.intro.lastIndexOf(n);
1275
- if (lineIndex !== -1) return this.intro.slice(lineIndex + 1) + lineStr;
1276
- return this.intro + lineStr;
1277
- }
1278
- /**
1279
- * Returns the content of the generated string that corresponds to the slice
1280
- * between `start` and `end` of the original string, walking the generated
1281
- * string from the chunk holding `start` to the chunk holding `end`. Throws
1282
- * if either index falls inside replaced content. After `move()`, a range
1283
- * whose end now comes before its start in the output yields `''`.
1284
- */
1285
- slice(start = 0, end = this.original.length - this.offset) {
1286
- start = this._resolveIndex(start);
1287
- end = this._resolveIndex(end);
1288
- let result = "";
1289
- let chunk;
1290
- if (this.hasMovedChunks) {
1291
- chunk = this.firstChunk;
1292
- while (chunk && (chunk.start > start || chunk.end <= start)) {
1293
- if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
1294
- chunk = chunk.next;
1295
- }
1296
- } else {
1297
- if (start >= end) return result;
1298
- chunk = this._chunkAt(start);
1299
- }
1300
- if (chunk && chunk.edited && chunk.start !== start) throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`);
1301
- const startChunk = chunk;
1302
- while (chunk) {
1303
- if (end === 0 && chunk.start === 0) break;
1304
- if (chunk.intro && (startChunk !== chunk || chunk.start === start)) result += chunk.intro;
1305
- const containsEnd = chunk.start < end && chunk.end >= end;
1306
- if (containsEnd && chunk.edited && chunk.end !== end) throw new MagicStringError(`cannot use edited character ${end} as slice end anchor`);
1307
- const sliceStart = startChunk === chunk ? start - chunk.start : 0;
1308
- const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
1309
- result += chunk.content.slice(sliceStart, sliceEnd);
1310
- if (chunk.outro && (!containsEnd || chunk.end === end)) result += chunk.outro;
1311
- if (containsEnd) break;
1312
- chunk = chunk.next;
1313
- }
1314
- return result;
1315
- }
1316
- /**
1317
- * Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
1318
- */
1319
- snip(start, end) {
1320
- const clone = this.clone();
1321
- clone.remove(-clone.offset, start);
1322
- clone.remove(end, clone.original.length - clone.offset);
1323
- return clone;
1324
- }
1325
- /**
1326
- * Applies `offset`, resolves a negative index from the end of `original` as
1327
- * `update`/`remove` always have, and rejects anything that is not an integer.
1328
- * Bounds are checked by the caller, because the methods report them differently.
1329
- *
1330
- * @internal
1331
- */
1332
- _resolveIndex(index) {
1333
- const resolved = index + this.offset;
1334
- if (!Number.isInteger(resolved)) throw new MagicStringError(`index must be an integer, got ${index}`);
1335
- if (resolved < 0 && this.original.length !== 0) return Math.max(0, resolved + this.original.length);
1336
- return resolved;
1337
- }
1338
- /**
1339
- * The chunk whose original range holds `index` (`chunk.start <= index < chunk.end`),
1340
- * or `undefined` when `index` is at or past the end. Searches from the last
1341
- * visited chunk so that scans over nearby indices stay cheap.
1342
- *
1343
- * @internal
1344
- */
1345
- _chunkAt(index) {
1346
- let chunk = this.byStart.get(index);
1347
- if (chunk) return chunk;
1348
- chunk = this.lastSearchedChunk;
1349
- const searchForward = index >= chunk.end;
1350
- while (chunk && !chunk.contains(index)) chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
1351
- if (chunk) this.lastSearchedChunk = chunk;
1352
- return chunk;
1353
- }
1354
- /** @internal */
1355
- _split(index) {
1356
- if (this.byStart.get(index) || this.byEnd.get(index)) return;
1357
- let chunk = this.lastSearchedChunk;
1358
- let previousChunk = chunk;
1359
- const searchForward = index > chunk.end;
1360
- while (chunk) {
1361
- if (chunk.contains(index)) return this._splitChunk(chunk, index);
1362
- chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
1363
- if (chunk === previousChunk) return;
1364
- previousChunk = chunk;
1365
- }
1366
- }
1367
- /** @internal */
1368
- _splitChunk(chunk, index) {
1369
- if (chunk.edited && chunk.content.length) {
1370
- const loc = getLocator(this.original)(index);
1371
- throw new MagicStringError(`cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`);
1372
- }
1373
- const newChunk = chunk.split(index);
1374
- this.byEnd.set(index, chunk);
1375
- this.byStart.set(index, newChunk);
1376
- this.byEnd.set(newChunk.end, newChunk);
1377
- if (chunk === this.lastChunk) this.lastChunk = newChunk;
1378
- this.lastSearchedChunk = chunk;
1379
- return true;
1380
- }
1381
- /**
1382
- * Returns the generated string.
1383
- */
1384
- toString() {
1385
- let str = this.intro;
1386
- let chunk = this.firstChunk;
1387
- while (chunk) {
1388
- str += chunk.toString();
1389
- chunk = chunk.next;
1390
- }
1391
- return str + this.outro;
1392
- }
1393
- /**
1394
- * Returns true if the resulting source is empty (disregarding white space).
1395
- */
1396
- isEmpty() {
1397
- if (this.intro.length && this.intro.trim()) return false;
1398
- let chunk = this.firstChunk;
1399
- while (chunk) {
1400
- if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim()) return false;
1401
- chunk = chunk.next;
1402
- }
1403
- if (this.outro.length && this.outro.trim()) return false;
1404
- return true;
1405
- }
1406
- /**
1407
- * Returns the length of the generated string, counting only content attached to the original characters.
1408
- * Content outside the string body (`s.prepend()`/`s.append()`, and inserts before the first or after the last
1409
- * character) is not counted, so this can differ from `s.toString().length`.
1410
- */
1411
- length() {
1412
- let chunk = this.firstChunk;
1413
- let length = 0;
1414
- while (chunk) {
1415
- length += chunk.intro.length + chunk.content.length + chunk.outro.length;
1416
- chunk = chunk.next;
1417
- }
1418
- return length;
1419
- }
1420
- /**
1421
- * Removes empty lines from the start and end.
1422
- */
1423
- trimLines() {
1424
- return this.trim("[\\r\\n]");
1425
- }
1426
- /**
1427
- * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
1428
- */
1429
- trim(charType) {
1430
- return this.trimStart(charType).trimEnd(charType);
1431
- }
1432
- /** @internal */
1433
- trimEndAborted(charType) {
1434
- const rx = new RegExp(`${charType || "\\s"}+$`);
1435
- this.outro = this.outro.replace(rx, "");
1436
- if (this.outro.length) return true;
1437
- let chunk = this.lastChunk;
1438
- do {
1439
- const end = chunk.end;
1440
- const aborted = chunk.trimEnd(rx);
1441
- if (chunk.end !== end) {
1442
- const next = chunk.next;
1443
- if (this.lastChunk === chunk) this.lastChunk = next;
1444
- this.byEnd.set(chunk.end, chunk);
1445
- this.byStart.set(next.start, next);
1446
- this.byEnd.set(next.end, next);
1447
- }
1448
- if (aborted) return true;
1449
- chunk = chunk.previous;
1450
- } while (chunk);
1451
- this.intro = this.intro.replace(rx, "");
1452
- return this.intro.length > 0;
1453
- }
1454
- /**
1455
- * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
1456
- */
1457
- trimEnd(charType) {
1458
- this.trimEndAborted(charType);
1459
- return this;
1460
- }
1461
- /** @internal */
1462
- trimStartAborted(charType) {
1463
- const rx = new RegExp(`^${charType || "\\s"}+`);
1464
- this.intro = this.intro.replace(rx, "");
1465
- if (this.intro.length) return true;
1466
- let chunk = this.firstChunk;
1467
- do {
1468
- const end = chunk.end;
1469
- const aborted = chunk.trimStart(rx);
1470
- if (chunk.end !== end) {
1471
- const next = chunk.next;
1472
- if (chunk === this.lastChunk) this.lastChunk = next;
1473
- this.byEnd.set(chunk.end, chunk);
1474
- this.byStart.set(next.start, next);
1475
- this.byEnd.set(next.end, next);
1476
- }
1477
- if (aborted) return true;
1478
- chunk = chunk.next;
1479
- } while (chunk);
1480
- this.outro = this.outro.replace(rx, "");
1481
- return this.outro.length > 0;
1482
- }
1483
- /**
1484
- * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
1485
- */
1486
- trimStart(charType) {
1487
- this.trimStartAborted(charType);
1488
- return this;
1489
- }
1490
- /**
1491
- * Indicates if the string has been changed.
1492
- */
1493
- hasChanged() {
1494
- if (this.intro || this.outro) return true;
1495
- let outputIndex = 0;
1496
- let chunk = this.firstChunk;
1497
- while (chunk) {
1498
- if (chunk.intro || chunk.outro) return true;
1499
- if (!chunk.edited && chunk.start === outputIndex) outputIndex = chunk.end;
1500
- else {
1501
- if (!this.original.startsWith(chunk.content, outputIndex)) return true;
1502
- outputIndex += chunk.content.length;
1503
- }
1504
- chunk = chunk.next;
1505
- }
1506
- return outputIndex !== this.original.length;
1507
- }
1508
- /**
1509
- * Whether the original range [start, end) has had any of its characters
1510
- * removed. `replace`/`replaceAll` search `original`, so a match can land on
1511
- * text that is no longer in the output - overwriting it would resurrect the
1512
- * removed characters, so such matches are skipped instead.
1513
- *
1514
- * @internal
1515
- */
1516
- _hasRemovedContent(start, end) {
1517
- const atBoundary = this.byStart.get(start);
1518
- if (atBoundary && start === end) {
1519
- const previous = this.byEnd.get(start);
1520
- return !!previous && previous.content === "" && atBoundary.content === "";
1521
- }
1522
- let chunk = this._chunkAt(start);
1523
- while (chunk && chunk.start < end) {
1524
- if (chunk.content === "" && (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom).replacesUntil < chunk.end)) return true;
1525
- chunk = this.byStart.get(chunk.end);
1526
- }
1527
- return false;
1528
- }
1529
- /** @internal */
1530
- _replaceRegexp(searchValue, replacement) {
1531
- function getReplacement(match, index, str) {
1532
- if (typeof replacement === "string") return expandReplacement(replacement, match[0], index, str, match.slice(1), match.groups);
1533
- else return match.groups === void 0 ? replacement(match[0], ...match.slice(1), index, str) : replacement(match[0], ...match.slice(1), index, str, match.groups);
1534
- }
1535
- const replaceMatch = (match) => {
1536
- /* v8 ignore next 2 -- `match.index` is always defined for matches from `matchAll` */
1537
- if (match.index == null) return false;
1538
- if (this._hasRemovedContent(match.index, match.index + match[0].length)) return false;
1539
- const replacement = getReplacement(match, match.index, this.original);
1540
- if (replacement === match[0]) return true;
1541
- if (match[0].length === 0) this.appendRight(match.index - this.offset, replacement);
1542
- else this.overwrite(match.index - this.offset, match.index + match[0].length - this.offset, replacement);
1543
- return true;
1544
- };
1545
- if (searchValue.global) {
1546
- searchValue.lastIndex = 0;
1547
- for (const match of this.original.matchAll(searchValue)) replaceMatch(match);
1548
- } else {
1549
- const match = this.original.match(searchValue);
1550
- if (match && !replaceMatch(match)) {
1551
- const global = new RegExp(searchValue.source, `${searchValue.flags}g`);
1552
- for (const next of this.original.matchAll(global)) if (replaceMatch(next)) break;
1553
- }
1554
- }
1555
- return this;
1556
- }
1557
- /** @internal */
1558
- _replaceString(string, replacement) {
1559
- const { original } = this;
1560
- let index = original.indexOf(string);
1561
- while (index !== -1) {
1562
- if (this._hasRemovedContent(index, index + string.length)) {
1563
- index = original.indexOf(string, index + string.length);
1564
- continue;
1565
- }
1566
- if (typeof replacement === "function") replacement = replacement(string, index, original);
1567
- else replacement = expandReplacement(replacement, string, index, original, [], void 0);
1568
- if (string !== replacement) {
1569
- if (string.length === 0) this.appendRight(index - this.offset, replacement);
1570
- else this.overwrite(index - this.offset, index + string.length - this.offset, replacement);
1571
- }
1572
- break;
1573
- }
1574
- return this;
1575
- }
1576
- /**
1577
- * String replacement with RegExp or string.
1578
- */
1579
- replace(searchValue, replacement) {
1580
- if (typeof searchValue === "string") return this._replaceString(searchValue, replacement);
1581
- return this._replaceRegexp(searchValue, replacement);
1582
- }
1583
- /** @internal */
1584
- _replaceAllString(string, replacement) {
1585
- const { original } = this;
1586
- const stringLength = string.length;
1587
- if (stringLength === 0) {
1588
- for (let index = 0; index <= original.length; index += 1) {
1589
- if (this._hasRemovedContent(index, index)) continue;
1590
- const _replacement = typeof replacement === "function" ? replacement("", index, original) : expandReplacement(replacement, "", index, original, [], void 0);
1591
- if (_replacement !== "") this.appendRight(index - this.offset, _replacement);
1592
- }
1593
- return this;
1594
- }
1595
- for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
1596
- if (this._hasRemovedContent(index, index + stringLength)) continue;
1597
- const previous = original.slice(index, index + stringLength);
1598
- const _replacement = typeof replacement === "function" ? replacement(previous, index, original) : expandReplacement(replacement, previous, index, original, [], void 0);
1599
- if (previous !== _replacement) this.overwrite(index - this.offset, index + stringLength - this.offset, _replacement);
1600
- }
1601
- return this;
1602
- }
1603
- /**
1604
- * Same as `s.replace`, but replace all matched strings instead of just one.
1605
- */
1606
- replaceAll(searchValue, replacement) {
1607
- if (typeof searchValue === "string") return this._replaceAllString(searchValue, replacement);
1608
- if (!searchValue.global) throw new MagicStringError("replaceAll() requires a global RegExp");
1609
- return this._replaceRegexp(searchValue, replacement);
1610
- }
1611
- };
1612
- export { MagicString, MagicString as default };