source: trip-planner-front/node_modules/magic-string/dist/magic-string.cjs.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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