source: trip-planner-front/node_modules/magic-string/dist/magic-string.es.js@ eed0bf8

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

initial commit

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