@vitest/browser 0.33.0 → 0.34.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.
package/dist/index.js CHANGED
@@ -3,1470 +3,8 @@ import { resolve } from 'path';
3
3
  import { builtinModules } from 'module';
4
4
  import { polyfillPath } from 'modern-node-polyfills';
5
5
  import sirv from 'sirv';
6
-
7
- const comma = ','.charCodeAt(0);
8
- const semicolon = ';'.charCodeAt(0);
9
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
10
- const intToChar = new Uint8Array(64); // 64 possible chars.
11
- const charToInt = new Uint8Array(128); // z is 122 in ASCII
12
- for (let i = 0; i < chars.length; i++) {
13
- const c = chars.charCodeAt(i);
14
- intToChar[i] = c;
15
- charToInt[c] = i;
16
- }
17
- // Provide a fallback for older environments.
18
- const td = typeof TextDecoder !== 'undefined'
19
- ? /* #__PURE__ */ new TextDecoder()
20
- : typeof Buffer !== 'undefined'
21
- ? {
22
- decode(buf) {
23
- const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
24
- return out.toString();
25
- },
26
- }
27
- : {
28
- decode(buf) {
29
- let out = '';
30
- for (let i = 0; i < buf.length; i++) {
31
- out += String.fromCharCode(buf[i]);
32
- }
33
- return out;
34
- },
35
- };
36
- function encode(decoded) {
37
- const state = new Int32Array(5);
38
- const bufLength = 1024 * 16;
39
- const subLength = bufLength - 36;
40
- const buf = new Uint8Array(bufLength);
41
- const sub = buf.subarray(0, subLength);
42
- let pos = 0;
43
- let out = '';
44
- for (let i = 0; i < decoded.length; i++) {
45
- const line = decoded[i];
46
- if (i > 0) {
47
- if (pos === bufLength) {
48
- out += td.decode(buf);
49
- pos = 0;
50
- }
51
- buf[pos++] = semicolon;
52
- }
53
- if (line.length === 0)
54
- continue;
55
- state[0] = 0;
56
- for (let j = 0; j < line.length; j++) {
57
- const segment = line[j];
58
- // We can push up to 5 ints, each int can take at most 7 chars, and we
59
- // may push a comma.
60
- if (pos > subLength) {
61
- out += td.decode(sub);
62
- buf.copyWithin(0, subLength, pos);
63
- pos -= subLength;
64
- }
65
- if (j > 0)
66
- buf[pos++] = comma;
67
- pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
68
- if (segment.length === 1)
69
- continue;
70
- pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
71
- pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
72
- pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
73
- if (segment.length === 4)
74
- continue;
75
- pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
76
- }
77
- }
78
- return out + td.decode(buf.subarray(0, pos));
79
- }
80
- function encodeInteger(buf, pos, state, segment, j) {
81
- const next = segment[j];
82
- let num = next - state[j];
83
- state[j] = next;
84
- num = num < 0 ? (-num << 1) | 1 : num << 1;
85
- do {
86
- let clamped = num & 0b011111;
87
- num >>>= 5;
88
- if (num > 0)
89
- clamped |= 0b100000;
90
- buf[pos++] = intToChar[clamped];
91
- } while (num > 0);
92
- return pos;
93
- }
94
-
95
- class BitSet {
96
- constructor(arg) {
97
- this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
98
- }
99
-
100
- add(n) {
101
- this.bits[n >> 5] |= 1 << (n & 31);
102
- }
103
-
104
- has(n) {
105
- return !!(this.bits[n >> 5] & (1 << (n & 31)));
106
- }
107
- }
108
-
109
- class Chunk {
110
- constructor(start, end, content) {
111
- this.start = start;
112
- this.end = end;
113
- this.original = content;
114
-
115
- this.intro = '';
116
- this.outro = '';
117
-
118
- this.content = content;
119
- this.storeName = false;
120
- this.edited = false;
121
-
122
- {
123
- this.previous = null;
124
- this.next = null;
125
- }
126
- }
127
-
128
- appendLeft(content) {
129
- this.outro += content;
130
- }
131
-
132
- appendRight(content) {
133
- this.intro = this.intro + content;
134
- }
135
-
136
- clone() {
137
- const chunk = new Chunk(this.start, this.end, this.original);
138
-
139
- chunk.intro = this.intro;
140
- chunk.outro = this.outro;
141
- chunk.content = this.content;
142
- chunk.storeName = this.storeName;
143
- chunk.edited = this.edited;
144
-
145
- return chunk;
146
- }
147
-
148
- contains(index) {
149
- return this.start < index && index < this.end;
150
- }
151
-
152
- eachNext(fn) {
153
- let chunk = this;
154
- while (chunk) {
155
- fn(chunk);
156
- chunk = chunk.next;
157
- }
158
- }
159
-
160
- eachPrevious(fn) {
161
- let chunk = this;
162
- while (chunk) {
163
- fn(chunk);
164
- chunk = chunk.previous;
165
- }
166
- }
167
-
168
- edit(content, storeName, contentOnly) {
169
- this.content = content;
170
- if (!contentOnly) {
171
- this.intro = '';
172
- this.outro = '';
173
- }
174
- this.storeName = storeName;
175
-
176
- this.edited = true;
177
-
178
- return this;
179
- }
180
-
181
- prependLeft(content) {
182
- this.outro = content + this.outro;
183
- }
184
-
185
- prependRight(content) {
186
- this.intro = content + this.intro;
187
- }
188
-
189
- split(index) {
190
- const sliceIndex = index - this.start;
191
-
192
- const originalBefore = this.original.slice(0, sliceIndex);
193
- const originalAfter = this.original.slice(sliceIndex);
194
-
195
- this.original = originalBefore;
196
-
197
- const newChunk = new Chunk(index, this.end, originalAfter);
198
- newChunk.outro = this.outro;
199
- this.outro = '';
200
-
201
- this.end = index;
202
-
203
- if (this.edited) {
204
- // TODO is this block necessary?...
205
- newChunk.edit('', false);
206
- this.content = '';
207
- } else {
208
- this.content = originalBefore;
209
- }
210
-
211
- newChunk.next = this.next;
212
- if (newChunk.next) newChunk.next.previous = newChunk;
213
- newChunk.previous = this;
214
- this.next = newChunk;
215
-
216
- return newChunk;
217
- }
218
-
219
- toString() {
220
- return this.intro + this.content + this.outro;
221
- }
222
-
223
- trimEnd(rx) {
224
- this.outro = this.outro.replace(rx, '');
225
- if (this.outro.length) return true;
226
-
227
- const trimmed = this.content.replace(rx, '');
228
-
229
- if (trimmed.length) {
230
- if (trimmed !== this.content) {
231
- this.split(this.start + trimmed.length).edit('', undefined, true);
232
- }
233
- return true;
234
- } else {
235
- this.edit('', undefined, true);
236
-
237
- this.intro = this.intro.replace(rx, '');
238
- if (this.intro.length) return true;
239
- }
240
- }
241
-
242
- trimStart(rx) {
243
- this.intro = this.intro.replace(rx, '');
244
- if (this.intro.length) return true;
245
-
246
- const trimmed = this.content.replace(rx, '');
247
-
248
- if (trimmed.length) {
249
- if (trimmed !== this.content) {
250
- this.split(this.end - trimmed.length);
251
- this.edit('', undefined, true);
252
- }
253
- return true;
254
- } else {
255
- this.edit('', undefined, true);
256
-
257
- this.outro = this.outro.replace(rx, '');
258
- if (this.outro.length) return true;
259
- }
260
- }
261
- }
262
-
263
- function getBtoa () {
264
- if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
265
- return (str) => window.btoa(unescape(encodeURIComponent(str)));
266
- } else if (typeof Buffer === 'function') {
267
- return (str) => Buffer.from(str, 'utf-8').toString('base64');
268
- } else {
269
- return () => {
270
- throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
271
- };
272
- }
273
- }
274
-
275
- const btoa = /*#__PURE__*/ getBtoa();
276
-
277
- class SourceMap {
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 = encode(properties.mappings);
285
- if (typeof properties.x_google_ignoreList !== 'undefined') {
286
- this.x_google_ignoreList = properties.x_google_ignoreList;
287
- }
288
- }
289
-
290
- toString() {
291
- return JSON.stringify(this);
292
- }
293
-
294
- toUrl() {
295
- return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
296
- }
297
- }
298
-
299
- function guessIndent(code) {
300
- const lines = code.split('\n');
301
-
302
- const tabbed = lines.filter((line) => /^\t+/.test(line));
303
- const spaced = lines.filter((line) => /^ {2,}/.test(line));
304
-
305
- if (tabbed.length === 0 && spaced.length === 0) {
306
- return null;
307
- }
308
-
309
- // More lines tabbed than spaced? Assume tabs, and
310
- // default to tabs in the case of a tie (or nothing
311
- // to go on)
312
- if (tabbed.length >= spaced.length) {
313
- return '\t';
314
- }
315
-
316
- // Otherwise, we need to guess the multiple
317
- const min = spaced.reduce((previous, current) => {
318
- const numSpaces = /^ +/.exec(current)[0].length;
319
- return Math.min(numSpaces, previous);
320
- }, Infinity);
321
-
322
- return new Array(min + 1).join(' ');
323
- }
324
-
325
- function getRelativePath(from, to) {
326
- const fromParts = from.split(/[/\\]/);
327
- const toParts = to.split(/[/\\]/);
328
-
329
- fromParts.pop(); // get dirname
330
-
331
- while (fromParts[0] === toParts[0]) {
332
- fromParts.shift();
333
- toParts.shift();
334
- }
335
-
336
- if (fromParts.length) {
337
- let i = fromParts.length;
338
- while (i--) fromParts[i] = '..';
339
- }
340
-
341
- return fromParts.concat(toParts).join('/');
342
- }
343
-
344
- const toString = Object.prototype.toString;
345
-
346
- function isObject(thing) {
347
- return toString.call(thing) === '[object Object]';
348
- }
349
-
350
- function getLocator(source) {
351
- const originalLines = source.split('\n');
352
- const lineOffsets = [];
353
-
354
- for (let i = 0, pos = 0; i < originalLines.length; i++) {
355
- lineOffsets.push(pos);
356
- pos += originalLines[i].length + 1;
357
- }
358
-
359
- return function locate(index) {
360
- let i = 0;
361
- let j = lineOffsets.length;
362
- while (i < j) {
363
- const m = (i + j) >> 1;
364
- if (index < lineOffsets[m]) {
365
- j = m;
366
- } else {
367
- i = m + 1;
368
- }
369
- }
370
- const line = i - 1;
371
- const column = index - lineOffsets[line];
372
- return { line, column };
373
- };
374
- }
375
-
376
- class Mappings {
377
- constructor(hires) {
378
- this.hires = hires;
379
- this.generatedCodeLine = 0;
380
- this.generatedCodeColumn = 0;
381
- this.raw = [];
382
- this.rawSegments = this.raw[this.generatedCodeLine] = [];
383
- this.pending = null;
384
- }
385
-
386
- addEdit(sourceIndex, content, loc, nameIndex) {
387
- if (content.length) {
388
- const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
389
- if (nameIndex >= 0) {
390
- segment.push(nameIndex);
391
- }
392
- this.rawSegments.push(segment);
393
- } else if (this.pending) {
394
- this.rawSegments.push(this.pending);
395
- }
396
-
397
- this.advance(content);
398
- this.pending = null;
399
- }
400
-
401
- addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
402
- let originalCharIndex = chunk.start;
403
- let first = true;
404
-
405
- while (originalCharIndex < chunk.end) {
406
- if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
407
- this.rawSegments.push([this.generatedCodeColumn, sourceIndex, loc.line, loc.column]);
408
- }
409
-
410
- if (original[originalCharIndex] === '\n') {
411
- loc.line += 1;
412
- loc.column = 0;
413
- this.generatedCodeLine += 1;
414
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
415
- this.generatedCodeColumn = 0;
416
- first = true;
417
- } else {
418
- loc.column += 1;
419
- this.generatedCodeColumn += 1;
420
- first = false;
421
- }
422
-
423
- originalCharIndex += 1;
424
- }
425
-
426
- this.pending = null;
427
- }
428
-
429
- advance(str) {
430
- if (!str) return;
431
-
432
- const lines = str.split('\n');
433
-
434
- if (lines.length > 1) {
435
- for (let i = 0; i < lines.length - 1; i++) {
436
- this.generatedCodeLine++;
437
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
438
- }
439
- this.generatedCodeColumn = 0;
440
- }
441
-
442
- this.generatedCodeColumn += lines[lines.length - 1].length;
443
- }
444
- }
445
-
446
- const n = '\n';
447
-
448
- const warned = {
449
- insertLeft: false,
450
- insertRight: false,
451
- storeName: false,
452
- };
453
-
454
- class MagicString {
455
- constructor(string, options = {}) {
456
- const chunk = new Chunk(0, string.length, string);
457
-
458
- Object.defineProperties(this, {
459
- original: { writable: true, value: string },
460
- outro: { writable: true, value: '' },
461
- intro: { writable: true, value: '' },
462
- firstChunk: { writable: true, value: chunk },
463
- lastChunk: { writable: true, value: chunk },
464
- lastSearchedChunk: { writable: true, value: chunk },
465
- byStart: { writable: true, value: {} },
466
- byEnd: { writable: true, value: {} },
467
- filename: { writable: true, value: options.filename },
468
- indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
469
- sourcemapLocations: { writable: true, value: new BitSet() },
470
- storedNames: { writable: true, value: {} },
471
- indentStr: { writable: true, value: undefined },
472
- ignoreList: { writable: true, value: options.ignoreList },
473
- });
474
-
475
- this.byStart[0] = chunk;
476
- this.byEnd[string.length] = chunk;
477
- }
478
-
479
- addSourcemapLocation(char) {
480
- this.sourcemapLocations.add(char);
481
- }
482
-
483
- append(content) {
484
- if (typeof content !== 'string') throw new TypeError('outro content must be a string');
485
-
486
- this.outro += content;
487
- return this;
488
- }
489
-
490
- appendLeft(index, content) {
491
- if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
492
-
493
- this._split(index);
494
-
495
- const chunk = this.byEnd[index];
496
-
497
- if (chunk) {
498
- chunk.appendLeft(content);
499
- } else {
500
- this.intro += content;
501
- }
502
- return this;
503
- }
504
-
505
- appendRight(index, content) {
506
- if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
507
-
508
- this._split(index);
509
-
510
- const chunk = this.byStart[index];
511
-
512
- if (chunk) {
513
- chunk.appendRight(content);
514
- } else {
515
- this.outro += content;
516
- }
517
- return this;
518
- }
519
-
520
- clone() {
521
- const cloned = new MagicString(this.original, { filename: this.filename });
522
-
523
- let originalChunk = this.firstChunk;
524
- let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
525
-
526
- while (originalChunk) {
527
- cloned.byStart[clonedChunk.start] = clonedChunk;
528
- cloned.byEnd[clonedChunk.end] = clonedChunk;
529
-
530
- const nextOriginalChunk = originalChunk.next;
531
- const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
532
-
533
- if (nextClonedChunk) {
534
- clonedChunk.next = nextClonedChunk;
535
- nextClonedChunk.previous = clonedChunk;
536
-
537
- clonedChunk = nextClonedChunk;
538
- }
539
-
540
- originalChunk = nextOriginalChunk;
541
- }
542
-
543
- cloned.lastChunk = clonedChunk;
544
-
545
- if (this.indentExclusionRanges) {
546
- cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
547
- }
548
-
549
- cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
550
-
551
- cloned.intro = this.intro;
552
- cloned.outro = this.outro;
553
-
554
- return cloned;
555
- }
556
-
557
- generateDecodedMap(options) {
558
- options = options || {};
559
-
560
- const sourceIndex = 0;
561
- const names = Object.keys(this.storedNames);
562
- const mappings = new Mappings(options.hires);
563
-
564
- const locate = getLocator(this.original);
565
-
566
- if (this.intro) {
567
- mappings.advance(this.intro);
568
- }
569
-
570
- this.firstChunk.eachNext((chunk) => {
571
- const loc = locate(chunk.start);
572
-
573
- if (chunk.intro.length) mappings.advance(chunk.intro);
574
-
575
- if (chunk.edited) {
576
- mappings.addEdit(
577
- sourceIndex,
578
- chunk.content,
579
- loc,
580
- chunk.storeName ? names.indexOf(chunk.original) : -1
581
- );
582
- } else {
583
- mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
584
- }
585
-
586
- if (chunk.outro.length) mappings.advance(chunk.outro);
587
- });
588
-
589
- return {
590
- file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
591
- sources: [options.source ? getRelativePath(options.file || '', options.source) : (options.file || '')],
592
- sourcesContent: options.includeContent ? [this.original] : undefined,
593
- names,
594
- mappings: mappings.raw,
595
- x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined
596
- };
597
- }
598
-
599
- generateMap(options) {
600
- return new SourceMap(this.generateDecodedMap(options));
601
- }
602
-
603
- _ensureindentStr() {
604
- if (this.indentStr === undefined) {
605
- this.indentStr = guessIndent(this.original);
606
- }
607
- }
608
-
609
- _getRawIndentString() {
610
- this._ensureindentStr();
611
- return this.indentStr;
612
- }
613
-
614
- getIndentString() {
615
- this._ensureindentStr();
616
- return this.indentStr === null ? '\t' : this.indentStr;
617
- }
618
-
619
- indent(indentStr, options) {
620
- const pattern = /^[^\r\n]/gm;
621
-
622
- if (isObject(indentStr)) {
623
- options = indentStr;
624
- indentStr = undefined;
625
- }
626
-
627
- if (indentStr === undefined) {
628
- this._ensureindentStr();
629
- indentStr = this.indentStr || '\t';
630
- }
631
-
632
- if (indentStr === '') return this; // noop
633
-
634
- options = options || {};
635
-
636
- // Process exclusion ranges
637
- const isExcluded = {};
638
-
639
- if (options.exclude) {
640
- const exclusions =
641
- typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
642
- exclusions.forEach((exclusion) => {
643
- for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
644
- isExcluded[i] = true;
645
- }
646
- });
647
- }
648
-
649
- let shouldIndentNextCharacter = options.indentStart !== false;
650
- const replacer = (match) => {
651
- if (shouldIndentNextCharacter) return `${indentStr}${match}`;
652
- shouldIndentNextCharacter = true;
653
- return match;
654
- };
655
-
656
- this.intro = this.intro.replace(pattern, replacer);
657
-
658
- let charIndex = 0;
659
- let chunk = this.firstChunk;
660
-
661
- while (chunk) {
662
- const end = chunk.end;
663
-
664
- if (chunk.edited) {
665
- if (!isExcluded[charIndex]) {
666
- chunk.content = chunk.content.replace(pattern, replacer);
667
-
668
- if (chunk.content.length) {
669
- shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
670
- }
671
- }
672
- } else {
673
- charIndex = chunk.start;
674
-
675
- while (charIndex < end) {
676
- if (!isExcluded[charIndex]) {
677
- const char = this.original[charIndex];
678
-
679
- if (char === '\n') {
680
- shouldIndentNextCharacter = true;
681
- } else if (char !== '\r' && shouldIndentNextCharacter) {
682
- shouldIndentNextCharacter = false;
683
-
684
- if (charIndex === chunk.start) {
685
- chunk.prependRight(indentStr);
686
- } else {
687
- this._splitChunk(chunk, charIndex);
688
- chunk = chunk.next;
689
- chunk.prependRight(indentStr);
690
- }
691
- }
692
- }
693
-
694
- charIndex += 1;
695
- }
696
- }
697
-
698
- charIndex = chunk.end;
699
- chunk = chunk.next;
700
- }
701
-
702
- this.outro = this.outro.replace(pattern, replacer);
703
-
704
- return this;
705
- }
706
-
707
- insert() {
708
- throw new Error(
709
- 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)'
710
- );
711
- }
712
-
713
- insertLeft(index, content) {
714
- if (!warned.insertLeft) {
715
- console.warn(
716
- 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead'
717
- ); // eslint-disable-line no-console
718
- warned.insertLeft = true;
719
- }
720
-
721
- return this.appendLeft(index, content);
722
- }
723
-
724
- insertRight(index, content) {
725
- if (!warned.insertRight) {
726
- console.warn(
727
- 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead'
728
- ); // eslint-disable-line no-console
729
- warned.insertRight = true;
730
- }
731
-
732
- return this.prependRight(index, content);
733
- }
734
-
735
- move(start, end, index) {
736
- if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
737
-
738
- this._split(start);
739
- this._split(end);
740
- this._split(index);
741
-
742
- const first = this.byStart[start];
743
- const last = this.byEnd[end];
744
-
745
- const oldLeft = first.previous;
746
- const oldRight = last.next;
747
-
748
- const newRight = this.byStart[index];
749
- if (!newRight && last === this.lastChunk) return this;
750
- const newLeft = newRight ? newRight.previous : this.lastChunk;
751
-
752
- if (oldLeft) oldLeft.next = oldRight;
753
- if (oldRight) oldRight.previous = oldLeft;
754
-
755
- if (newLeft) newLeft.next = first;
756
- if (newRight) newRight.previous = last;
757
-
758
- if (!first.previous) this.firstChunk = last.next;
759
- if (!last.next) {
760
- this.lastChunk = first.previous;
761
- this.lastChunk.next = null;
762
- }
763
-
764
- first.previous = newLeft;
765
- last.next = newRight || null;
766
-
767
- if (!newLeft) this.firstChunk = first;
768
- if (!newRight) this.lastChunk = last;
769
- return this;
770
- }
771
-
772
- overwrite(start, end, content, options) {
773
- options = options || {};
774
- return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
775
- }
776
-
777
- update(start, end, content, options) {
778
- if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
779
-
780
- while (start < 0) start += this.original.length;
781
- while (end < 0) end += this.original.length;
782
-
783
- if (end > this.original.length) throw new Error('end is out of bounds');
784
- if (start === end)
785
- throw new Error(
786
- 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead'
787
- );
788
-
789
- this._split(start);
790
- this._split(end);
791
-
792
- if (options === true) {
793
- if (!warned.storeName) {
794
- console.warn(
795
- 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string'
796
- ); // eslint-disable-line no-console
797
- warned.storeName = true;
798
- }
799
-
800
- options = { storeName: true };
801
- }
802
- const storeName = options !== undefined ? options.storeName : false;
803
- const overwrite = options !== undefined ? options.overwrite : false;
804
-
805
- if (storeName) {
806
- const original = this.original.slice(start, end);
807
- Object.defineProperty(this.storedNames, original, {
808
- writable: true,
809
- value: true,
810
- enumerable: true,
811
- });
812
- }
813
-
814
- const first = this.byStart[start];
815
- const last = this.byEnd[end];
816
-
817
- if (first) {
818
- let chunk = first;
819
- while (chunk !== last) {
820
- if (chunk.next !== this.byStart[chunk.end]) {
821
- throw new Error('Cannot overwrite across a split point');
822
- }
823
- chunk = chunk.next;
824
- chunk.edit('', false);
825
- }
826
-
827
- first.edit(content, storeName, !overwrite);
828
- } else {
829
- // must be inserting at the end
830
- const newChunk = new Chunk(start, end, '').edit(content, storeName);
831
-
832
- // TODO last chunk in the array may not be the last chunk, if it's moved...
833
- last.next = newChunk;
834
- newChunk.previous = last;
835
- }
836
- return this;
837
- }
838
-
839
- prepend(content) {
840
- if (typeof content !== 'string') throw new TypeError('outro content must be a string');
841
-
842
- this.intro = content + this.intro;
843
- return this;
844
- }
845
-
846
- prependLeft(index, content) {
847
- if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
848
-
849
- this._split(index);
850
-
851
- const chunk = this.byEnd[index];
852
-
853
- if (chunk) {
854
- chunk.prependLeft(content);
855
- } else {
856
- this.intro = content + this.intro;
857
- }
858
- return this;
859
- }
860
-
861
- prependRight(index, content) {
862
- if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
863
-
864
- this._split(index);
865
-
866
- const chunk = this.byStart[index];
867
-
868
- if (chunk) {
869
- chunk.prependRight(content);
870
- } else {
871
- this.outro = content + this.outro;
872
- }
873
- return this;
874
- }
875
-
876
- remove(start, end) {
877
- while (start < 0) start += this.original.length;
878
- while (end < 0) end += this.original.length;
879
-
880
- if (start === end) return this;
881
-
882
- if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
883
- if (start > end) throw new Error('end must be greater than start');
884
-
885
- this._split(start);
886
- this._split(end);
887
-
888
- let chunk = this.byStart[start];
889
-
890
- while (chunk) {
891
- chunk.intro = '';
892
- chunk.outro = '';
893
- chunk.edit('');
894
-
895
- chunk = end > chunk.end ? this.byStart[chunk.end] : null;
896
- }
897
- return this;
898
- }
899
-
900
- lastChar() {
901
- if (this.outro.length) return this.outro[this.outro.length - 1];
902
- let chunk = this.lastChunk;
903
- do {
904
- if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
905
- if (chunk.content.length) return chunk.content[chunk.content.length - 1];
906
- if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
907
- } while ((chunk = chunk.previous));
908
- if (this.intro.length) return this.intro[this.intro.length - 1];
909
- return '';
910
- }
911
-
912
- lastLine() {
913
- let lineIndex = this.outro.lastIndexOf(n);
914
- if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
915
- let lineStr = this.outro;
916
- let chunk = this.lastChunk;
917
- do {
918
- if (chunk.outro.length > 0) {
919
- lineIndex = chunk.outro.lastIndexOf(n);
920
- if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
921
- lineStr = chunk.outro + lineStr;
922
- }
923
-
924
- if (chunk.content.length > 0) {
925
- lineIndex = chunk.content.lastIndexOf(n);
926
- if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
927
- lineStr = chunk.content + lineStr;
928
- }
929
-
930
- if (chunk.intro.length > 0) {
931
- lineIndex = chunk.intro.lastIndexOf(n);
932
- if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
933
- lineStr = chunk.intro + lineStr;
934
- }
935
- } while ((chunk = chunk.previous));
936
- lineIndex = this.intro.lastIndexOf(n);
937
- if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
938
- return this.intro + lineStr;
939
- }
940
-
941
- slice(start = 0, end = this.original.length) {
942
- while (start < 0) start += this.original.length;
943
- while (end < 0) end += this.original.length;
944
-
945
- let result = '';
946
-
947
- // find start chunk
948
- let chunk = this.firstChunk;
949
- while (chunk && (chunk.start > start || chunk.end <= start)) {
950
- // found end chunk before start
951
- if (chunk.start < end && chunk.end >= end) {
952
- return result;
953
- }
954
-
955
- chunk = chunk.next;
956
- }
957
-
958
- if (chunk && chunk.edited && chunk.start !== start)
959
- throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
960
-
961
- const startChunk = chunk;
962
- while (chunk) {
963
- if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
964
- result += chunk.intro;
965
- }
966
-
967
- const containsEnd = chunk.start < end && chunk.end >= end;
968
- if (containsEnd && chunk.edited && chunk.end !== end)
969
- throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
970
-
971
- const sliceStart = startChunk === chunk ? start - chunk.start : 0;
972
- const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
973
-
974
- result += chunk.content.slice(sliceStart, sliceEnd);
975
-
976
- if (chunk.outro && (!containsEnd || chunk.end === end)) {
977
- result += chunk.outro;
978
- }
979
-
980
- if (containsEnd) {
981
- break;
982
- }
983
-
984
- chunk = chunk.next;
985
- }
986
-
987
- return result;
988
- }
989
-
990
- // TODO deprecate this? not really very useful
991
- snip(start, end) {
992
- const clone = this.clone();
993
- clone.remove(0, start);
994
- clone.remove(end, clone.original.length);
995
-
996
- return clone;
997
- }
998
-
999
- _split(index) {
1000
- if (this.byStart[index] || this.byEnd[index]) return;
1001
-
1002
- let chunk = this.lastSearchedChunk;
1003
- const searchForward = index > chunk.end;
1004
-
1005
- while (chunk) {
1006
- if (chunk.contains(index)) return this._splitChunk(chunk, index);
1007
-
1008
- chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
1009
- }
1010
- }
1011
-
1012
- _splitChunk(chunk, index) {
1013
- if (chunk.edited && chunk.content.length) {
1014
- // zero-length edited chunks are a special case (overlapping replacements)
1015
- const loc = getLocator(this.original)(index);
1016
- throw new Error(
1017
- `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`
1018
- );
1019
- }
1020
-
1021
- const newChunk = chunk.split(index);
1022
-
1023
- this.byEnd[index] = chunk;
1024
- this.byStart[index] = newChunk;
1025
- this.byEnd[newChunk.end] = newChunk;
1026
-
1027
- if (chunk === this.lastChunk) this.lastChunk = newChunk;
1028
-
1029
- this.lastSearchedChunk = chunk;
1030
- return true;
1031
- }
1032
-
1033
- toString() {
1034
- let str = this.intro;
1035
-
1036
- let chunk = this.firstChunk;
1037
- while (chunk) {
1038
- str += chunk.toString();
1039
- chunk = chunk.next;
1040
- }
1041
-
1042
- return str + this.outro;
1043
- }
1044
-
1045
- isEmpty() {
1046
- let chunk = this.firstChunk;
1047
- do {
1048
- if (
1049
- (chunk.intro.length && chunk.intro.trim()) ||
1050
- (chunk.content.length && chunk.content.trim()) ||
1051
- (chunk.outro.length && chunk.outro.trim())
1052
- )
1053
- return false;
1054
- } while ((chunk = chunk.next));
1055
- return true;
1056
- }
1057
-
1058
- length() {
1059
- let chunk = this.firstChunk;
1060
- let length = 0;
1061
- do {
1062
- length += chunk.intro.length + chunk.content.length + chunk.outro.length;
1063
- } while ((chunk = chunk.next));
1064
- return length;
1065
- }
1066
-
1067
- trimLines() {
1068
- return this.trim('[\\r\\n]');
1069
- }
1070
-
1071
- trim(charType) {
1072
- return this.trimStart(charType).trimEnd(charType);
1073
- }
1074
-
1075
- trimEndAborted(charType) {
1076
- const rx = new RegExp((charType || '\\s') + '+$');
1077
-
1078
- this.outro = this.outro.replace(rx, '');
1079
- if (this.outro.length) return true;
1080
-
1081
- let chunk = this.lastChunk;
1082
-
1083
- do {
1084
- const end = chunk.end;
1085
- const aborted = chunk.trimEnd(rx);
1086
-
1087
- // if chunk was trimmed, we have a new lastChunk
1088
- if (chunk.end !== end) {
1089
- if (this.lastChunk === chunk) {
1090
- this.lastChunk = chunk.next;
1091
- }
1092
-
1093
- this.byEnd[chunk.end] = chunk;
1094
- this.byStart[chunk.next.start] = chunk.next;
1095
- this.byEnd[chunk.next.end] = chunk.next;
1096
- }
1097
-
1098
- if (aborted) return true;
1099
- chunk = chunk.previous;
1100
- } while (chunk);
1101
-
1102
- return false;
1103
- }
1104
-
1105
- trimEnd(charType) {
1106
- this.trimEndAborted(charType);
1107
- return this;
1108
- }
1109
- trimStartAborted(charType) {
1110
- const rx = new RegExp('^' + (charType || '\\s') + '+');
1111
-
1112
- this.intro = this.intro.replace(rx, '');
1113
- if (this.intro.length) return true;
1114
-
1115
- let chunk = this.firstChunk;
1116
-
1117
- do {
1118
- const end = chunk.end;
1119
- const aborted = chunk.trimStart(rx);
1120
-
1121
- if (chunk.end !== end) {
1122
- // special case...
1123
- if (chunk === this.lastChunk) this.lastChunk = chunk.next;
1124
-
1125
- this.byEnd[chunk.end] = chunk;
1126
- this.byStart[chunk.next.start] = chunk.next;
1127
- this.byEnd[chunk.next.end] = chunk.next;
1128
- }
1129
-
1130
- if (aborted) return true;
1131
- chunk = chunk.next;
1132
- } while (chunk);
1133
-
1134
- return false;
1135
- }
1136
-
1137
- trimStart(charType) {
1138
- this.trimStartAborted(charType);
1139
- return this;
1140
- }
1141
-
1142
- hasChanged() {
1143
- return this.original !== this.toString();
1144
- }
1145
-
1146
- _replaceRegexp(searchValue, replacement) {
1147
- function getReplacement(match, str) {
1148
- if (typeof replacement === 'string') {
1149
- return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
1150
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
1151
- if (i === '$') return '$';
1152
- if (i === '&') return match[0];
1153
- const num = +i;
1154
- if (num < match.length) return match[+i];
1155
- return `$${i}`;
1156
- });
1157
- } else {
1158
- return replacement(...match, match.index, str, match.groups);
1159
- }
1160
- }
1161
- function matchAll(re, str) {
1162
- let match;
1163
- const matches = [];
1164
- while ((match = re.exec(str))) {
1165
- matches.push(match);
1166
- }
1167
- return matches;
1168
- }
1169
- if (searchValue.global) {
1170
- const matches = matchAll(searchValue, this.original);
1171
- matches.forEach((match) => {
1172
- if (match.index != null)
1173
- this.overwrite(
1174
- match.index,
1175
- match.index + match[0].length,
1176
- getReplacement(match, this.original)
1177
- );
1178
- });
1179
- } else {
1180
- const match = this.original.match(searchValue);
1181
- if (match && match.index != null)
1182
- this.overwrite(
1183
- match.index,
1184
- match.index + match[0].length,
1185
- getReplacement(match, this.original)
1186
- );
1187
- }
1188
- return this;
1189
- }
1190
-
1191
- _replaceString(string, replacement) {
1192
- const { original } = this;
1193
- const index = original.indexOf(string);
1194
-
1195
- if (index !== -1) {
1196
- this.overwrite(index, index + string.length, replacement);
1197
- }
1198
-
1199
- return this;
1200
- }
1201
-
1202
- replace(searchValue, replacement) {
1203
- if (typeof searchValue === 'string') {
1204
- return this._replaceString(searchValue, replacement);
1205
- }
1206
-
1207
- return this._replaceRegexp(searchValue, replacement);
1208
- }
1209
-
1210
- _replaceAllString(string, replacement) {
1211
- const { original } = this;
1212
- const stringLength = string.length;
1213
- for (
1214
- let index = original.indexOf(string);
1215
- index !== -1;
1216
- index = original.indexOf(string, index + stringLength)
1217
- ) {
1218
- this.overwrite(index, index + stringLength, replacement);
1219
- }
1220
-
1221
- return this;
1222
- }
1223
-
1224
- replaceAll(searchValue, replacement) {
1225
- if (typeof searchValue === 'string') {
1226
- return this._replaceAllString(searchValue, replacement);
1227
- }
1228
-
1229
- if (!searchValue.global) {
1230
- throw new TypeError(
1231
- 'MagicString.prototype.replaceAll called with a non-global RegExp argument'
1232
- );
1233
- }
1234
-
1235
- return this._replaceRegexp(searchValue, replacement);
1236
- }
1237
- }
1238
-
1239
- /**
1240
- * @typedef { import('estree').Node} Node
1241
- * @typedef {{
1242
- * skip: () => void;
1243
- * remove: () => void;
1244
- * replace: (node: Node) => void;
1245
- * }} WalkerContext
1246
- */
1247
-
1248
- class WalkerBase {
1249
- constructor() {
1250
- /** @type {boolean} */
1251
- this.should_skip = false;
1252
-
1253
- /** @type {boolean} */
1254
- this.should_remove = false;
1255
-
1256
- /** @type {Node | null} */
1257
- this.replacement = null;
1258
-
1259
- /** @type {WalkerContext} */
1260
- this.context = {
1261
- skip: () => (this.should_skip = true),
1262
- remove: () => (this.should_remove = true),
1263
- replace: (node) => (this.replacement = node)
1264
- };
1265
- }
1266
-
1267
- /**
1268
- * @template {Node} Parent
1269
- * @param {Parent | null | undefined} parent
1270
- * @param {keyof Parent | null | undefined} prop
1271
- * @param {number | null | undefined} index
1272
- * @param {Node} node
1273
- */
1274
- replace(parent, prop, index, node) {
1275
- if (parent && prop) {
1276
- if (index != null) {
1277
- /** @type {Array<Node>} */ (parent[prop])[index] = node;
1278
- } else {
1279
- /** @type {Node} */ (parent[prop]) = node;
1280
- }
1281
- }
1282
- }
1283
-
1284
- /**
1285
- * @template {Node} Parent
1286
- * @param {Parent | null | undefined} parent
1287
- * @param {keyof Parent | null | undefined} prop
1288
- * @param {number | null | undefined} index
1289
- */
1290
- remove(parent, prop, index) {
1291
- if (parent && prop) {
1292
- if (index !== null && index !== undefined) {
1293
- /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
1294
- } else {
1295
- delete parent[prop];
1296
- }
1297
- }
1298
- }
1299
- }
1300
-
1301
- /**
1302
- * @typedef { import('estree').Node} Node
1303
- * @typedef { import('./walker.js').WalkerContext} WalkerContext
1304
- * @typedef {(
1305
- * this: WalkerContext,
1306
- * node: Node,
1307
- * parent: Node | null,
1308
- * key: string | number | symbol | null | undefined,
1309
- * index: number | null | undefined
1310
- * ) => void} SyncHandler
1311
- */
1312
-
1313
- class SyncWalker extends WalkerBase {
1314
- /**
1315
- *
1316
- * @param {SyncHandler} [enter]
1317
- * @param {SyncHandler} [leave]
1318
- */
1319
- constructor(enter, leave) {
1320
- super();
1321
-
1322
- /** @type {boolean} */
1323
- this.should_skip = false;
1324
-
1325
- /** @type {boolean} */
1326
- this.should_remove = false;
1327
-
1328
- /** @type {Node | null} */
1329
- this.replacement = null;
1330
-
1331
- /** @type {WalkerContext} */
1332
- this.context = {
1333
- skip: () => (this.should_skip = true),
1334
- remove: () => (this.should_remove = true),
1335
- replace: (node) => (this.replacement = node)
1336
- };
1337
-
1338
- /** @type {SyncHandler | undefined} */
1339
- this.enter = enter;
1340
-
1341
- /** @type {SyncHandler | undefined} */
1342
- this.leave = leave;
1343
- }
1344
-
1345
- /**
1346
- * @template {Node} Parent
1347
- * @param {Node} node
1348
- * @param {Parent | null} parent
1349
- * @param {keyof Parent} [prop]
1350
- * @param {number | null} [index]
1351
- * @returns {Node | null}
1352
- */
1353
- visit(node, parent, prop, index) {
1354
- if (node) {
1355
- if (this.enter) {
1356
- const _should_skip = this.should_skip;
1357
- const _should_remove = this.should_remove;
1358
- const _replacement = this.replacement;
1359
- this.should_skip = false;
1360
- this.should_remove = false;
1361
- this.replacement = null;
1362
-
1363
- this.enter.call(this.context, node, parent, prop, index);
1364
-
1365
- if (this.replacement) {
1366
- node = this.replacement;
1367
- this.replace(parent, prop, index, node);
1368
- }
1369
-
1370
- if (this.should_remove) {
1371
- this.remove(parent, prop, index);
1372
- }
1373
-
1374
- const skipped = this.should_skip;
1375
- const removed = this.should_remove;
1376
-
1377
- this.should_skip = _should_skip;
1378
- this.should_remove = _should_remove;
1379
- this.replacement = _replacement;
1380
-
1381
- if (skipped) return node;
1382
- if (removed) return null;
1383
- }
1384
-
1385
- /** @type {keyof Node} */
1386
- let key;
1387
-
1388
- for (key in node) {
1389
- /** @type {unknown} */
1390
- const value = node[key];
1391
-
1392
- if (value && typeof value === 'object') {
1393
- if (Array.isArray(value)) {
1394
- const nodes = /** @type {Array<unknown>} */ (value);
1395
- for (let i = 0; i < nodes.length; i += 1) {
1396
- const item = nodes[i];
1397
- if (isNode(item)) {
1398
- if (!this.visit(item, node, key, i)) {
1399
- // removed
1400
- i--;
1401
- }
1402
- }
1403
- }
1404
- } else if (isNode(value)) {
1405
- this.visit(value, node, key, null);
1406
- }
1407
- }
1408
- }
1409
-
1410
- if (this.leave) {
1411
- const _replacement = this.replacement;
1412
- const _should_remove = this.should_remove;
1413
- this.replacement = null;
1414
- this.should_remove = false;
1415
-
1416
- this.leave.call(this.context, node, parent, prop, index);
1417
-
1418
- if (this.replacement) {
1419
- node = this.replacement;
1420
- this.replace(parent, prop, index, node);
1421
- }
1422
-
1423
- if (this.should_remove) {
1424
- this.remove(parent, prop, index);
1425
- }
1426
-
1427
- const removed = this.should_remove;
1428
-
1429
- this.replacement = _replacement;
1430
- this.should_remove = _should_remove;
1431
-
1432
- if (removed) return null;
1433
- }
1434
- }
1435
-
1436
- return node;
1437
- }
1438
- }
1439
-
1440
- /**
1441
- * Ducktype a node.
1442
- *
1443
- * @param {unknown} value
1444
- * @returns {value is Node}
1445
- */
1446
- function isNode(value) {
1447
- return (
1448
- value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
1449
- );
1450
- }
1451
-
1452
- /**
1453
- * @typedef {import('estree').Node} Node
1454
- * @typedef {import('./sync.js').SyncHandler} SyncHandler
1455
- * @typedef {import('./async.js').AsyncHandler} AsyncHandler
1456
- */
1457
-
1458
- /**
1459
- * @param {Node} ast
1460
- * @param {{
1461
- * enter?: SyncHandler
1462
- * leave?: SyncHandler
1463
- * }} walker
1464
- * @returns {Node | null}
1465
- */
1466
- function walk(ast, { enter, leave }) {
1467
- const instance = new SyncWalker(enter, leave);
1468
- return instance.visit(ast, null);
1469
- }
6
+ import MagicString from 'magic-string';
7
+ import { walk } from 'estree-walker';
1470
8
 
1471
9
  /**
1472
10
  * @param {import('estree').Node} param