trilean 1.13.4 → 1.13.6

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.
@@ -93,7 +93,7 @@ function encode(decoded) {
93
93
  return writer.flush();
94
94
  }
95
95
  //#endregion
96
- //#region ../../node_modules/.pnpm/magic-string@1.4.1/node_modules/magic-string/dist/index.mjs
96
+ //#region ../../node_modules/.pnpm/magic-string@1.4.5/node_modules/magic-string/dist/index.mjs
97
97
  var BitSet = class BitSet {
98
98
  constructor(arg) {
99
99
  this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
@@ -104,6 +104,17 @@ var BitSet = class BitSet {
104
104
  has(n) {
105
105
  return !!(this.bits[n >> 5] & 1 << (n & 31));
106
106
  }
107
+ forEach(fn) {
108
+ const bits = this.bits;
109
+ for (let w = 0; w < bits.length; w++) {
110
+ let word = bits[w];
111
+ while (word) {
112
+ const lowest = word & -word;
113
+ fn((w << 5) + 31 - Math.clz32(lowest));
114
+ word ^= lowest;
115
+ }
116
+ }
117
+ }
107
118
  };
108
119
  var Chunk = class Chunk {
109
120
  constructor(start, end, content) {
@@ -115,6 +126,8 @@ var Chunk = class Chunk {
115
126
  this.content = content;
116
127
  this.storeName = false;
117
128
  this.edited = false;
129
+ this.replacesUntil = -1;
130
+ this.replacedFrom = -1;
118
131
  this.previous = null;
119
132
  this.next = null;
120
133
  }
@@ -131,6 +144,8 @@ var Chunk = class Chunk {
131
144
  chunk.content = this.content;
132
145
  chunk.storeName = this.storeName;
133
146
  chunk.edited = this.edited;
147
+ chunk.replacesUntil = this.replacesUntil;
148
+ chunk.replacedFrom = this.replacedFrom;
134
149
  return chunk;
135
150
  }
136
151
  contains(index) {
@@ -171,6 +186,8 @@ var Chunk = class Chunk {
171
186
  reset() {
172
187
  this.intro = "";
173
188
  this.outro = "";
189
+ this.replacesUntil = -1;
190
+ this.replacedFrom = -1;
174
191
  if (this.edited) {
175
192
  this.content = this.original;
176
193
  this.storeName = false;
@@ -187,8 +204,8 @@ var Chunk = class Chunk {
187
204
  this.outro = "";
188
205
  this.end = index;
189
206
  if (this.edited) {
190
- newChunk.edit("", false);
191
- this.content = "";
207
+ newChunk.edit("", false, true);
208
+ newChunk.replacedFrom = this.replacedFrom;
192
209
  } else this.content = originalBefore;
193
210
  newChunk.next = this.next;
194
211
  if (newChunk.next) newChunk.next.previous = newChunk;
@@ -438,8 +455,8 @@ var Mappings = class {
438
455
  this.generatedCodeColumn = 0;
439
456
  this.raw = [];
440
457
  this.rawSegments = this.raw[this.generatedCodeLine] = [];
441
- this.rawRangeMappings = [];
442
- this.rawRangeMappingsIndices = this.rawRangeMappings[this.generatedCodeLine] = [];
458
+ this.rawRangeMappingsIndices = [];
459
+ this.rawRangeMappings = hires === "experimental-range" ? [this.rawRangeMappingsIndices] : void 0;
443
460
  this.encoder = encoder;
444
461
  }
445
462
  nextLine() {
@@ -452,7 +469,28 @@ var Mappings = class {
452
469
  this.generatedCodeLine += 1;
453
470
  }
454
471
  this.generatedCodeColumn = 0;
455
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
472
+ if (this.rawRangeMappings) this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
473
+ }
474
+ /**
475
+ * Emits the mappings for one MagicString: its intro, every chunk (with the
476
+ * chunk's own intro/outro) and its outro. `sourceIndex === -1` means the
477
+ * string has no source of its own, so its content only advances the
478
+ * generated position without mapping anything.
479
+ */
480
+ addMagicString(magicString, sourceIndex, nameIndex) {
481
+ const { original } = magicString;
482
+ const locate = getLocator(original);
483
+ if (magicString.intro) this.advance(magicString.intro);
484
+ magicString.firstChunk.eachNext((chunk) => {
485
+ if (chunk.intro.length) this.advance(chunk.intro);
486
+ if (sourceIndex === -1) this.advance(chunk.content);
487
+ else if (chunk.edited) {
488
+ const name = chunk.storeName ? nameIndex.get(original.slice(chunk.start, chunk.replacesUntil)) : void 0;
489
+ this.addEdit(sourceIndex, chunk.content, locate(chunk.start), name ?? -1);
490
+ } else this.addUneditedChunk(sourceIndex, chunk, original, locate(chunk.start), magicString.sourcemapLocations);
491
+ if (chunk.outro.length) this.advance(chunk.outro);
492
+ });
493
+ if (magicString.outro) this.advance(magicString.outro);
456
494
  }
457
495
  addEdit(sourceIndex, content, loc, nameIndex) {
458
496
  if (content.length) {
@@ -665,6 +703,9 @@ function expandReplacement(replacement, matched, position, str, captures, namedC
665
703
  return result;
666
704
  }
667
705
  var MagicString = class MagicString {
706
+ /**
707
+ * Wraps `string` for editing; every index passed to the editing methods refers to this original string.
708
+ */
668
709
  constructor(string, options = {}) {
669
710
  const chunk = new Chunk(0, string.length, string);
670
711
  Object.defineProperties(this, {
@@ -714,7 +755,7 @@ var MagicString = class MagicString {
714
755
  },
715
756
  storedNames: {
716
757
  writable: true,
717
- value: {}
758
+ value: /* @__PURE__ */ new Set()
718
759
  },
719
760
  indentStr: {
720
761
  writable: true,
@@ -754,7 +795,8 @@ var MagicString = class MagicString {
754
795
  * See also `s.prependLeft(...)`.
755
796
  */
756
797
  appendLeft(index, content) {
757
- index = index + this.offset;
798
+ index = this._resolveIndex(index);
799
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
758
800
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
759
801
  this._split(index);
760
802
  const chunk = this.byEnd.get(index);
@@ -768,7 +810,8 @@ var MagicString = class MagicString {
768
810
  * See also `s.prependRight(...)`.
769
811
  */
770
812
  appendRight(index, content) {
771
- index = index + this.offset;
813
+ index = this._resolveIndex(index);
814
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
772
815
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
773
816
  this._split(index);
774
817
  const chunk = this.byStart.get(index);
@@ -777,7 +820,10 @@ var MagicString = class MagicString {
777
820
  return this;
778
821
  }
779
822
  /**
780
- * Does what you'd expect.
823
+ * Returns an independent copy: the chunk list, string-level intro/outro,
824
+ * sourcemap locations, stored names, indent exclusion ranges, `offset`,
825
+ * `filename` and `ignoreList` are all copied; later edits to either string
826
+ * do not affect the other.
781
827
  */
782
828
  clone() {
783
829
  const cloned = new MagicString(this.original, {
@@ -802,7 +848,7 @@ var MagicString = class MagicString {
802
848
  cloned.lastChunk = clonedChunk;
803
849
  if (this.indentExclusionRanges) cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
804
850
  cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
805
- cloned.storedNames = { ...this.storedNames };
851
+ cloned.storedNames = new Set(this.storedNames);
806
852
  cloned.intro = this.intro;
807
853
  cloned.outro = this.outro;
808
854
  cloned.hasMovedChunks = this.hasMovedChunks;
@@ -838,18 +884,8 @@ var MagicString = class MagicString {
838
884
  }
839
885
  /** @internal */
840
886
  _generateMappings(mappings) {
841
- const sourceIndex = 0;
842
- const names = Object.keys(this.storedNames);
843
- const locate = getLocator(this.original);
844
- if (this.intro) mappings.advance(this.intro);
845
- this.firstChunk.eachNext((chunk) => {
846
- const loc = locate(chunk.start);
847
- if (chunk.intro.length) mappings.advance(chunk.intro);
848
- if (chunk.edited) mappings.addEdit(sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf(chunk.original) : -1);
849
- else mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
850
- if (chunk.outro.length) mappings.advance(chunk.outro);
851
- });
852
- if (this.outro) mappings.advance(this.outro);
887
+ const names = [...this.storedNames];
888
+ mappings.addMagicString(this, 0, new Map(names.map((name, i) => [name, i])));
853
889
  return names;
854
890
  }
855
891
  /** @internal */
@@ -871,9 +907,13 @@ var MagicString = class MagicString {
871
907
  this._ensureindentStr();
872
908
  return this.indentStr;
873
909
  }
910
+ /**
911
+ * Returns the indentation string guessed from the original content, falling back to a single tab character.
912
+ * This is the value `s.indent()` uses when no prefix is supplied.
913
+ */
874
914
  getIndentString() {
875
915
  this._ensureindentStr();
876
- return this.indentStr === null ? " " : this.indentStr;
916
+ return this.indentStr ?? " ";
877
917
  }
878
918
  indent(indentStr, options) {
879
919
  const pattern = /^[^\r\n]/gm;
@@ -888,9 +928,9 @@ var MagicString = class MagicString {
888
928
  if (indentStr === "") return this;
889
929
  const resolvedIndentStr = indentStr;
890
930
  options = options || {};
891
- const isExcluded = {};
931
+ const isExcluded = new BitSet();
892
932
  if (options.exclude) (typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude).forEach((exclusion) => {
893
- for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded[i] = true;
933
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded.add(i);
894
934
  });
895
935
  let shouldIndentNextCharacter = options.indentStart !== false;
896
936
  const indentPiece = (str) => {
@@ -913,13 +953,14 @@ var MagicString = class MagicString {
913
953
  };
914
954
  while (chunk) {
915
955
  const end = chunk.end;
916
- if (!isExcluded[chunk.start]) chunk.intro = indentPiece(chunk.intro);
956
+ if (!isExcluded.has(chunk.start)) chunk.intro = indentPiece(chunk.intro);
917
957
  if (chunk.edited) {
918
- if (!isExcluded[charIndex]) chunk.content = indentPiece(chunk.content);
958
+ if (!isExcluded.has(chunk.start)) chunk.content = indentPiece(chunk.content);
959
+ else if (chunk.content) shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
919
960
  } else if (options.exclude) {
920
961
  charIndex = chunk.start;
921
962
  while (charIndex < end) {
922
- if (!isExcluded[charIndex]) {
963
+ if (!isExcluded.has(charIndex)) {
923
964
  const char = this.original.charCodeAt(charIndex);
924
965
  if (char === NEWLINE_CHAR) shouldIndentNextCharacter = true;
925
966
  else if (char !== CR_CHAR && shouldIndentNextCharacter) indentAt(charIndex);
@@ -945,7 +986,7 @@ var MagicString = class MagicString {
945
986
  charIndex += 1;
946
987
  }
947
988
  }
948
- if (!isExcluded[chunk.end - 1]) chunk.outro = indentPiece(chunk.outro);
989
+ if (!isExcluded.has(chunk.end - 1)) chunk.outro = indentPiece(chunk.outro);
949
990
  charIndex = chunk.end;
950
991
  chunk = chunk.next;
951
992
  }
@@ -982,16 +1023,19 @@ var MagicString = class MagicString {
982
1023
  * mirroring the `appendLeft`/`appendRight` distinction.
983
1024
  */
984
1025
  move(start, end, index, affinity = "right") {
985
- start = start + this.offset;
986
- end = end + this.offset;
987
- index = index + this.offset;
1026
+ start = this._resolveIndex(start);
1027
+ end = this._resolveIndex(end);
1028
+ index = this._resolveIndex(index);
988
1029
  if (start === end) return this;
1030
+ if (start < 0 || end > this.original.length || index < 0 || index > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1031
+ if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
989
1032
  if (index >= start && index <= end) throw new MagicStringError("cannot move a selection inside itself");
990
1033
  this._split(start);
991
1034
  this._split(end);
992
1035
  this._split(index);
993
1036
  const first = this.byStart.get(start);
994
1037
  const last = this.byEnd.get(end);
1038
+ if (!first || !last) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
995
1039
  if (this.hasMovedChunks) {
996
1040
  let cursor = first;
997
1041
  while (cursor !== last) {
@@ -1026,9 +1070,9 @@ var MagicString = class MagicString {
1026
1070
  if (oldRight) oldRight.previous = oldLeft;
1027
1071
  if (newLeft) newLeft.next = first;
1028
1072
  if (newRight) newRight.previous = last;
1029
- if (!first.previous) this.firstChunk = last.next;
1030
- if (!last.next) {
1031
- this.lastChunk = first.previous;
1073
+ if (!oldLeft) this.firstChunk = oldRight;
1074
+ if (!oldRight) {
1075
+ this.lastChunk = oldLeft;
1032
1076
  this.lastChunk.next = null;
1033
1077
  }
1034
1078
  first.previous = newLeft;
@@ -1049,6 +1093,16 @@ var MagicString = class MagicString {
1049
1093
  * It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
1050
1094
  */
1051
1095
  overwrite(start, end, content, options) {
1096
+ if (options === true) {
1097
+ if (!warned.storeName) {
1098
+ console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string");
1099
+ warned.storeName = true;
1100
+ }
1101
+ return this.update(start, end, content, {
1102
+ storeName: true,
1103
+ overwrite: true
1104
+ });
1105
+ }
1052
1106
  const optionObject = typeof options === "object" && options ? options : {};
1053
1107
  return this.update(start, end, content, {
1054
1108
  ...optionObject,
@@ -1063,13 +1117,9 @@ var MagicString = class MagicString {
1063
1117
  * the content is overwritten, or anything that was appended/prepended to the range as well.
1064
1118
  */
1065
1119
  update(start, end, content, options) {
1066
- start = start + this.offset;
1067
- end = end + this.offset;
1120
+ start = this._resolveIndex(start);
1121
+ end = this._resolveIndex(end);
1068
1122
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1069
- if (this.original.length !== 0) {
1070
- if (start < 0) start = Math.max(0, start + this.original.length);
1071
- if (end < 0) end = Math.max(0, end + this.original.length);
1072
- }
1073
1123
  if (start < 0) throw new MagicStringError(`start ${start} is out of bounds`);
1074
1124
  if (end > this.original.length) throw new MagicStringError(`end ${end} is out of bounds`);
1075
1125
  if (start === end) throw new MagicStringError(`cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`);
@@ -1086,30 +1136,21 @@ var MagicString = class MagicString {
1086
1136
  const optionObject = typeof options === "object" && options ? options : {};
1087
1137
  const storeName = optionObject.storeName || false;
1088
1138
  const overwrite = optionObject.overwrite || false;
1089
- if (storeName) {
1090
- const original = this.original.slice(start, end);
1091
- Object.defineProperty(this.storedNames, original, {
1092
- writable: true,
1093
- value: true,
1094
- enumerable: true
1095
- });
1096
- }
1139
+ if (storeName) this.storedNames.add(this.original.slice(start, end));
1097
1140
  const first = this.byStart.get(start);
1098
- const last = this.byEnd.get(end);
1099
- /* v8 ignore else -- unreachable: a valid start/end always yields a `first` chunk */
1100
- if (first) {
1101
- let chunk = first;
1102
- while (chunk !== last) {
1103
- if (chunk.next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
1104
- chunk = chunk.next;
1105
- chunk.edit("", false);
1106
- }
1107
- first.edit(content, storeName, !overwrite);
1108
- } else {
1109
- const newChunk = new Chunk(start, end, "").edit(content, storeName);
1110
- last.next = newChunk;
1111
- newChunk.previous = last;
1141
+ for (let chunk = first; chunk.end < end;) {
1142
+ const next = chunk.next;
1143
+ if (next === null || next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
1144
+ chunk = next;
1145
+ }
1146
+ for (let chunk = first.next; chunk !== null && chunk.start < end; chunk = chunk.next) {
1147
+ chunk.edit("", false, !overwrite);
1148
+ chunk.replacesUntil = -1;
1149
+ chunk.replacedFrom = start;
1112
1150
  }
1151
+ first.edit(content, storeName, !overwrite);
1152
+ first.replacesUntil = end;
1153
+ first.replacedFrom = -1;
1113
1154
  return this;
1114
1155
  }
1115
1156
  /**
@@ -1124,7 +1165,8 @@ var MagicString = class MagicString {
1124
1165
  * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
1125
1166
  */
1126
1167
  prependLeft(index, content) {
1127
- index = index + this.offset;
1168
+ index = this._resolveIndex(index);
1169
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1128
1170
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1129
1171
  this._split(index);
1130
1172
  const chunk = this.byEnd.get(index);
@@ -1136,7 +1178,8 @@ var MagicString = class MagicString {
1136
1178
  * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
1137
1179
  */
1138
1180
  prependRight(index, content) {
1139
- index = index + this.offset;
1181
+ index = this._resolveIndex(index);
1182
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1140
1183
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1141
1184
  this._split(index);
1142
1185
  const chunk = this.byStart.get(index);
@@ -1149,15 +1192,11 @@ var MagicString = class MagicString {
1149
1192
  * Content appended or prepended at positions strictly inside the range is removed with it, while
1150
1193
  * content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to remove
1151
1194
  * the range including its edge inserts.
1152
- * Removing the same content twice, or making removals that partially overlap, will cause an error.
1195
+ * Repeated removals and removals with partially overlapping ranges are allowed.
1153
1196
  */
1154
1197
  remove(start, end) {
1155
- start = start + this.offset;
1156
- end = end + this.offset;
1157
- if (this.original.length !== 0) {
1158
- if (start < 0) start = Math.max(0, start + this.original.length);
1159
- if (end < 0) end = Math.max(0, end + this.original.length);
1160
- }
1198
+ start = this._resolveIndex(start);
1199
+ end = this._resolveIndex(end);
1161
1200
  if (start === end) return this;
1162
1201
  if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1163
1202
  if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
@@ -1168,7 +1207,9 @@ var MagicString = class MagicString {
1168
1207
  if (chunk.start > start) chunk.intro = "";
1169
1208
  if (chunk.end < end) chunk.outro = "";
1170
1209
  chunk.edit("", false, true);
1171
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
1210
+ chunk.replacesUntil = -1;
1211
+ chunk.replacedFrom = -1;
1212
+ chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1172
1213
  }
1173
1214
  return this;
1174
1215
  }
@@ -1176,12 +1217,8 @@ var MagicString = class MagicString {
1176
1217
  * Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
1177
1218
  */
1178
1219
  reset(start, end) {
1179
- start = start + this.offset;
1180
- end = end + this.offset;
1181
- if (this.original.length !== 0) {
1182
- if (start < 0) start = Math.max(0, start + this.original.length);
1183
- if (end < 0) end = Math.max(0, end + this.original.length);
1184
- }
1220
+ start = this._resolveIndex(start);
1221
+ end = this._resolveIndex(end);
1185
1222
  if (start === end) return this;
1186
1223
  if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1187
1224
  if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
@@ -1190,10 +1227,13 @@ var MagicString = class MagicString {
1190
1227
  let chunk = this.byStart.get(start);
1191
1228
  while (chunk) {
1192
1229
  chunk.reset();
1193
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
1230
+ chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1194
1231
  }
1195
1232
  return this;
1196
1233
  }
1234
+ /**
1235
+ * Returns the last character of the generated string, or `''` if it is empty.
1236
+ */
1197
1237
  lastChar() {
1198
1238
  if (this.outro.length) return this.outro[this.outro.length - 1];
1199
1239
  let chunk = this.lastChunk;
@@ -1206,49 +1246,57 @@ var MagicString = class MagicString {
1206
1246
  if (this.intro.length) return this.intro[this.intro.length - 1];
1207
1247
  return "";
1208
1248
  }
1249
+ /**
1250
+ * Returns everything after the last newline in the generated string, or the whole string if there is no newline.
1251
+ */
1209
1252
  lastLine() {
1210
1253
  let lineIndex = this.outro.lastIndexOf(n);
1211
- if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
1254
+ if (lineIndex !== -1) return this.outro.slice(lineIndex + 1);
1212
1255
  let lineStr = this.outro;
1213
1256
  let chunk = this.lastChunk;
1214
1257
  while (chunk) {
1215
1258
  if (chunk.outro.length > 0) {
1216
1259
  lineIndex = chunk.outro.lastIndexOf(n);
1217
- if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
1260
+ if (lineIndex !== -1) return chunk.outro.slice(lineIndex + 1) + lineStr;
1218
1261
  lineStr = chunk.outro + lineStr;
1219
1262
  }
1220
1263
  if (chunk.content.length > 0) {
1221
1264
  lineIndex = chunk.content.lastIndexOf(n);
1222
- if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
1265
+ if (lineIndex !== -1) return chunk.content.slice(lineIndex + 1) + lineStr;
1223
1266
  lineStr = chunk.content + lineStr;
1224
1267
  }
1225
1268
  if (chunk.intro.length > 0) {
1226
1269
  lineIndex = chunk.intro.lastIndexOf(n);
1227
- if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
1270
+ if (lineIndex !== -1) return chunk.intro.slice(lineIndex + 1) + lineStr;
1228
1271
  lineStr = chunk.intro + lineStr;
1229
1272
  }
1230
1273
  chunk = chunk.previous;
1231
1274
  }
1232
1275
  lineIndex = this.intro.lastIndexOf(n);
1233
- if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
1276
+ if (lineIndex !== -1) return this.intro.slice(lineIndex + 1) + lineStr;
1234
1277
  return this.intro + lineStr;
1235
1278
  }
1236
1279
  /**
1237
- * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
1238
- * Throws error if the indices are for characters that were already removed.
1280
+ * Returns the content of the generated string that corresponds to the slice
1281
+ * between `start` and `end` of the original string, walking the generated
1282
+ * string from the chunk holding `start` to the chunk holding `end`. Throws
1283
+ * if either index falls inside replaced content. After `move()`, a range
1284
+ * whose end now comes before its start in the output yields `''`.
1239
1285
  */
1240
1286
  slice(start = 0, end = this.original.length - this.offset) {
1241
- start = start + this.offset;
1242
- end = end + this.offset;
1243
- if (this.original.length !== 0) {
1244
- if (start < 0) start = Math.max(0, start + this.original.length);
1245
- if (end < 0) end = Math.max(0, end + this.original.length);
1246
- }
1287
+ start = this._resolveIndex(start);
1288
+ end = this._resolveIndex(end);
1247
1289
  let result = "";
1248
- let chunk = this.firstChunk;
1249
- while (chunk && (chunk.start > start || chunk.end <= start)) {
1250
- if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
1251
- chunk = chunk.next;
1290
+ let chunk;
1291
+ if (this.hasMovedChunks) {
1292
+ chunk = this.firstChunk;
1293
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
1294
+ if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
1295
+ chunk = chunk.next;
1296
+ }
1297
+ } else {
1298
+ if (start >= end) return result;
1299
+ chunk = this._chunkAt(start);
1252
1300
  }
1253
1301
  if (chunk && chunk.edited && chunk.start !== start) throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`);
1254
1302
  const startChunk = chunk;
@@ -1271,10 +1319,39 @@ var MagicString = class MagicString {
1271
1319
  */
1272
1320
  snip(start, end) {
1273
1321
  const clone = this.clone();
1274
- clone.remove(0, start);
1275
- clone.remove(end, clone.original.length);
1322
+ clone.remove(-clone.offset, start);
1323
+ clone.remove(end, clone.original.length - clone.offset);
1276
1324
  return clone;
1277
1325
  }
1326
+ /**
1327
+ * Applies `offset`, resolves a negative index from the end of `original` as
1328
+ * `update`/`remove` always have, and rejects anything that is not an integer.
1329
+ * Bounds are checked by the caller, because the methods report them differently.
1330
+ *
1331
+ * @internal
1332
+ */
1333
+ _resolveIndex(index) {
1334
+ const resolved = index + this.offset;
1335
+ if (!Number.isInteger(resolved)) throw new MagicStringError(`index must be an integer, got ${index}`);
1336
+ if (resolved < 0 && this.original.length !== 0) return Math.max(0, resolved + this.original.length);
1337
+ return resolved;
1338
+ }
1339
+ /**
1340
+ * The chunk whose original range holds `index` (`chunk.start <= index < chunk.end`),
1341
+ * or `undefined` when `index` is at or past the end. Searches from the last
1342
+ * visited chunk so that scans over nearby indices stay cheap.
1343
+ *
1344
+ * @internal
1345
+ */
1346
+ _chunkAt(index) {
1347
+ let chunk = this.byStart.get(index);
1348
+ if (chunk) return chunk;
1349
+ chunk = this.lastSearchedChunk;
1350
+ const searchForward = index >= chunk.end;
1351
+ while (chunk && !chunk.contains(index)) chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
1352
+ if (chunk) this.lastSearchedChunk = chunk;
1353
+ return chunk;
1354
+ }
1278
1355
  /** @internal */
1279
1356
  _split(index) {
1280
1357
  if (this.byStart.get(index) || this.byEnd.get(index)) return;
@@ -1327,6 +1404,11 @@ var MagicString = class MagicString {
1327
1404
  if (this.outro.length && this.outro.trim()) return false;
1328
1405
  return true;
1329
1406
  }
1407
+ /**
1408
+ * Returns the length of the generated string, counting only content attached to the original characters.
1409
+ * Content outside the string body (`s.prepend()`/`s.append()`, and inserts before the first or after the last
1410
+ * character) is not counted, so this can differ from `s.toString().length`.
1411
+ */
1330
1412
  length() {
1331
1413
  let chunk = this.firstChunk;
1332
1414
  let length = 0;
@@ -1358,10 +1440,11 @@ var MagicString = class MagicString {
1358
1440
  const end = chunk.end;
1359
1441
  const aborted = chunk.trimEnd(rx);
1360
1442
  if (chunk.end !== end) {
1361
- if (this.lastChunk === chunk) this.lastChunk = chunk.next;
1443
+ const next = chunk.next;
1444
+ if (this.lastChunk === chunk) this.lastChunk = next;
1362
1445
  this.byEnd.set(chunk.end, chunk);
1363
- this.byStart.set(chunk.next.start, chunk.next);
1364
- this.byEnd.set(chunk.next.end, chunk.next);
1446
+ this.byStart.set(next.start, next);
1447
+ this.byEnd.set(next.end, next);
1365
1448
  }
1366
1449
  if (aborted) return true;
1367
1450
  chunk = chunk.previous;
@@ -1386,10 +1469,11 @@ var MagicString = class MagicString {
1386
1469
  const end = chunk.end;
1387
1470
  const aborted = chunk.trimStart(rx);
1388
1471
  if (chunk.end !== end) {
1389
- if (chunk === this.lastChunk) this.lastChunk = chunk.next;
1472
+ const next = chunk.next;
1473
+ if (chunk === this.lastChunk) this.lastChunk = next;
1390
1474
  this.byEnd.set(chunk.end, chunk);
1391
- this.byStart.set(chunk.next.start, chunk.next);
1392
- this.byEnd.set(chunk.next.end, chunk.next);
1475
+ this.byStart.set(next.start, next);
1476
+ this.byEnd.set(next.end, next);
1393
1477
  }
1394
1478
  if (aborted) return true;
1395
1479
  chunk = chunk.next;
@@ -1431,27 +1515,32 @@ var MagicString = class MagicString {
1431
1515
  * @internal
1432
1516
  */
1433
1517
  _hasRemovedContent(start, end) {
1434
- let chunk = this.firstChunk;
1435
- while (chunk) {
1436
- if (chunk.content === "" && chunk.start < end && chunk.end > start) return true;
1437
- chunk = chunk.next;
1518
+ const atBoundary = this.byStart.get(start);
1519
+ if (atBoundary && start === end) {
1520
+ const previous = this.byEnd.get(start);
1521
+ return !!previous && previous.content === "" && atBoundary.content === "";
1522
+ }
1523
+ let chunk = this._chunkAt(start);
1524
+ while (chunk && chunk.start < end) {
1525
+ if (chunk.content === "" && (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom).replacesUntil < chunk.end)) return true;
1526
+ chunk = this.byStart.get(chunk.end);
1438
1527
  }
1439
1528
  return false;
1440
1529
  }
1441
1530
  /** @internal */
1442
1531
  _replaceRegexp(searchValue, replacement) {
1443
- function getReplacement(match, str) {
1444
- if (typeof replacement === "string") return expandReplacement(replacement, match[0], match.index, str, match.slice(1), match.groups);
1445
- else return match.groups === void 0 ? replacement(match[0], ...match.slice(1), match.index, str) : replacement(match[0], ...match.slice(1), match.index, str, match.groups);
1532
+ function getReplacement(match, index, str) {
1533
+ if (typeof replacement === "string") return expandReplacement(replacement, match[0], index, str, match.slice(1), match.groups);
1534
+ else return match.groups === void 0 ? replacement(match[0], ...match.slice(1), index, str) : replacement(match[0], ...match.slice(1), index, str, match.groups);
1446
1535
  }
1447
1536
  const replaceMatch = (match) => {
1448
1537
  /* v8 ignore next 2 -- `match.index` is always defined for matches from `matchAll` */
1449
1538
  if (match.index == null) return false;
1450
1539
  if (this._hasRemovedContent(match.index, match.index + match[0].length)) return false;
1451
- const replacement = getReplacement(match, this.original);
1540
+ const replacement = getReplacement(match, match.index, this.original);
1452
1541
  if (replacement === match[0]) return true;
1453
- if (match[0].length === 0) this.appendRight(match.index, replacement);
1454
- else this.overwrite(match.index, match.index + match[0].length, replacement);
1542
+ if (match[0].length === 0) this.appendRight(match.index - this.offset, replacement);
1543
+ else this.overwrite(match.index - this.offset, match.index + match[0].length - this.offset, replacement);
1455
1544
  return true;
1456
1545
  };
1457
1546
  if (searchValue.global) {
@@ -1478,8 +1567,8 @@ var MagicString = class MagicString {
1478
1567
  if (typeof replacement === "function") replacement = replacement(string, index, original);
1479
1568
  else replacement = expandReplacement(replacement, string, index, original, [], void 0);
1480
1569
  if (string !== replacement) {
1481
- if (string.length === 0) this.appendRight(index, replacement);
1482
- else this.overwrite(index, index + string.length, replacement);
1570
+ if (string.length === 0) this.appendRight(index - this.offset, replacement);
1571
+ else this.overwrite(index - this.offset, index + string.length - this.offset, replacement);
1483
1572
  }
1484
1573
  break;
1485
1574
  }
@@ -1498,8 +1587,9 @@ var MagicString = class MagicString {
1498
1587
  const stringLength = string.length;
1499
1588
  if (stringLength === 0) {
1500
1589
  for (let index = 0; index <= original.length; index += 1) {
1590
+ if (this._hasRemovedContent(index, index)) continue;
1501
1591
  const _replacement = typeof replacement === "function" ? replacement("", index, original) : expandReplacement(replacement, "", index, original, [], void 0);
1502
- if (_replacement !== "") this.appendRight(index, _replacement);
1592
+ if (_replacement !== "") this.appendRight(index - this.offset, _replacement);
1503
1593
  }
1504
1594
  return this;
1505
1595
  }
@@ -1507,7 +1597,7 @@ var MagicString = class MagicString {
1507
1597
  if (this._hasRemovedContent(index, index + stringLength)) continue;
1508
1598
  const previous = original.slice(index, index + stringLength);
1509
1599
  const _replacement = typeof replacement === "function" ? replacement(previous, index, original) : expandReplacement(replacement, previous, index, original, [], void 0);
1510
- if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
1600
+ if (previous !== _replacement) this.overwrite(index - this.offset, index + stringLength - this.offset, _replacement);
1511
1601
  }
1512
1602
  return this;
1513
1603
  }
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_index_C4tKZ0yW = require("./index.C4tKZ0yW-BXgPC7yz.cjs");
2
+ const require_index_CZzC1gGg = require("./index.CZzC1gGg-C3tq-f5f.cjs");
3
3
  const require_evaluator_factory = require("./evaluator-factory.cjs");
4
4
  //#region src/evaluator-indeterminacy-helpers.ts
5
5
  /**
@@ -152,8 +152,8 @@ function expr(description, node, expected, overrides = {}) {
152
152
  }
153
153
  async function runFixture({ run, expected }) {
154
154
  const result = await run();
155
- require_index_C4tKZ0yW.globalExpect(result.status).toBe(expected.status);
156
- if (expected.status === "indeterminate" && result.status === "indeterminate") require_index_C4tKZ0yW.globalExpect(result.reason.code).toBe(expected.code);
155
+ require_index_CZzC1gGg.globalExpect(result.status).toBe(expected.status);
156
+ if (expected.status === "indeterminate" && result.status === "indeterminate") require_index_CZzC1gGg.globalExpect(result.reason.code).toBe(expected.code);
157
157
  }
158
158
  //#endregion
159
159
  exports.baseResolvers = baseResolvers;