esperanto-source 0.6.8

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.
@@ -0,0 +1,765 @@
1
+ (function (global, factory) {
2
+ typeof define === 'function' && define.amd ? define(['vlq'], factory) :
3
+ typeof exports === 'object' ? module.exports = factory(require('vlq')) :
4
+ global.MagicString = factory(global.vlq)
5
+ }(this, function (vlq__default) { 'use strict';
6
+
7
+ var _btoa;
8
+
9
+ if ( typeof window !== 'undefined' && typeof window.btoa === 'function' ) {
10
+ _btoa = window.btoa;
11
+ } else if ( typeof Buffer === 'function' ) {
12
+ _btoa = function ( str ) {
13
+ return new Buffer( str ).toString( 'base64' );
14
+ };
15
+ } else {
16
+ throw new Error( 'Unsupported environment' );
17
+ }
18
+
19
+ var btoa = _btoa;
20
+
21
+ var SourceMap = function ( properties ) {
22
+ this.version = 3;
23
+
24
+ this.file = properties.file;
25
+ this.sources = properties.sources;
26
+ this.sourcesContent = properties.sourcesContent;
27
+ this.names = properties.names;
28
+ this.mappings = properties.mappings;
29
+ };
30
+
31
+ SourceMap.prototype = {
32
+ toString: function () {
33
+ return JSON.stringify( this );
34
+ },
35
+
36
+ toUrl: function () {
37
+ return 'data:application/json;charset=utf-8;base64,' + btoa( this.toString() );
38
+ }
39
+ };
40
+
41
+ function getRelativePath ( from, to ) {
42
+ var fromParts, toParts, i;
43
+
44
+ fromParts = from.split( '/' );
45
+ toParts = to.split( '/' );
46
+
47
+ fromParts.pop(); // get dirname
48
+
49
+ while ( fromParts[0] === toParts[0] ) {
50
+ fromParts.shift();
51
+ toParts.shift();
52
+ }
53
+
54
+ if ( fromParts.length ) {
55
+ i = fromParts.length;
56
+ while ( i-- ) fromParts[i] = '..';
57
+ }
58
+
59
+ return fromParts.concat( toParts ).join( '/' );
60
+ }
61
+
62
+ var Bundle = function ( options ) {
63
+ options = options || {};
64
+
65
+ this.intro = options.intro || '';
66
+ this.outro = options.outro || '';
67
+ this.separator = 'separator' in options ? options.separator : '\n';
68
+
69
+ this.sources = [];
70
+ };
71
+
72
+ Bundle.prototype = {
73
+ addSource: function ( source ) {
74
+ if ( typeof source !== 'object' || !source.content ) {
75
+ throw new Error( 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`' );
76
+ }
77
+
78
+ this.sources.push( source );
79
+ return this;
80
+ },
81
+
82
+ append: function ( str ) {
83
+ this.outro += str;
84
+ return this;
85
+ },
86
+
87
+ clone: function () {
88
+ var bundle = new Bundle({
89
+ intro: this.intro,
90
+ outro: this.outro,
91
+ separator: this.separator
92
+ });
93
+
94
+ this.sources.forEach( function ( source ) {
95
+ bundle.addSource({
96
+ filename: source.filename,
97
+ content: source.content.clone()
98
+ });
99
+ });
100
+
101
+ return bundle;
102
+ },
103
+
104
+ generateMap: function ( options ) {
105
+ var offsets = {}, encoded, encodingSeparator;
106
+
107
+ encodingSeparator = getSemis( this.separator );
108
+
109
+ encoded = (
110
+ getSemis( this.intro ) +
111
+ this.sources.map( function ( source, sourceIndex) {
112
+ return source.content.getMappings( options.hires, sourceIndex, offsets );
113
+ }).join( encodingSeparator ) +
114
+ getSemis( this.outro )
115
+ );
116
+
117
+ return new SourceMap({
118
+ file: options.file.split( '/' ).pop(),
119
+ sources: this.sources.map( function ( source ) {
120
+ return getRelativePath( options.file, source.filename );
121
+ }),
122
+ sourcesContent: this.sources.map( function ( source ) {
123
+ return options.includeContent ? source.content.original : null;
124
+ }),
125
+ names: [],
126
+ mappings: encoded
127
+ });
128
+ },
129
+
130
+ getIndentString: function () {
131
+ var indentStringCounts = {};
132
+
133
+ this.sources.forEach( function ( source ) {
134
+ var indentStr = source.content.indentStr;
135
+
136
+ if ( indentStr === null ) return;
137
+
138
+ if ( !indentStringCounts[ indentStr ] ) indentStringCounts[ indentStr ] = 0;
139
+ indentStringCounts[ indentStr ] += 1;
140
+ });
141
+
142
+ return ( Object.keys( indentStringCounts ).sort( function ( a, b ) {
143
+ return indentStringCounts[a] - indentStringCounts[b];
144
+ })[0] ) || '\t';
145
+ },
146
+
147
+ indent: function ( indentStr ) {
148
+ if ( !indentStr ) {
149
+ indentStr = this.getIndentString();
150
+ }
151
+
152
+ this.sources.forEach( function ( source ) {
153
+ source.content.indent( indentStr, { exclude: source.indentExclusionRanges });
154
+ });
155
+
156
+ this.intro = this.intro.replace( /^[^\n]/gm, indentStr + '$&' );
157
+ this.outro = this.outro.replace( /^[^\n]/gm, indentStr + '$&' );
158
+
159
+ return this;
160
+ },
161
+
162
+ prepend: function ( str ) {
163
+ this.intro = str + this.intro;
164
+ return this;
165
+ },
166
+
167
+ toString: function () {
168
+ return this.intro + this.sources.map( stringify ).join( this.separator ) + this.outro;
169
+ },
170
+
171
+ trimLines: function () {
172
+ return this.trim('[\\r\\n]');
173
+ },
174
+
175
+ trim: function (charType) {
176
+ return this.trimStart(charType).trimEnd(charType);
177
+ },
178
+
179
+ trimStart: function (charType) {
180
+ var rx = new RegExp('^' + (charType || '\\s') + '+');
181
+ this.intro = this.intro.replace( rx, '' );
182
+
183
+ if ( !this.intro ) {
184
+ var source;
185
+ var i = 0;
186
+ do {
187
+ source = this.sources[i];
188
+
189
+ if ( !source ) {
190
+ this.outro = this.outro.replace( rx, '' );
191
+ break;
192
+ }
193
+
194
+ source.content.trimStart();
195
+ i += 1;
196
+ } while ( source.content.str === '' );
197
+ }
198
+
199
+ return this;
200
+ },
201
+
202
+ trimEnd: function(charType) {
203
+ var rx = new RegExp((charType || '\\s') + '+$');
204
+ this.outro = this.outro.replace( rx, '' );
205
+
206
+ if ( !this.outro ) {
207
+ var source;
208
+ var i = this.sources.length - 1;
209
+ do {
210
+ source = this.sources[i];
211
+
212
+ if ( !source ) {
213
+ this.intro = this.intro.replace( rx, '' );
214
+ break;
215
+ }
216
+
217
+ source.content.trimEnd(charType);
218
+ i -= 1;
219
+ } while ( source.content.str === '' );
220
+ }
221
+
222
+ return this;
223
+ }
224
+ };
225
+
226
+
227
+
228
+ function stringify ( source ) {
229
+ return source.content.toString();
230
+ }
231
+
232
+ function getSemis ( str ) {
233
+ return new Array( str.split( '\n' ).length ).join( ';' );
234
+ }
235
+
236
+ function guessIndent ( code ) {
237
+ var lines, tabbed, spaced, min;
238
+
239
+ lines = code.split( '\n' );
240
+
241
+ tabbed = lines.filter( function ( line ) {
242
+ return /^\t+/.test( line );
243
+ });
244
+
245
+ spaced = lines.filter( function ( line ) {
246
+ return /^ {2,}/.test( line );
247
+ });
248
+
249
+ if ( tabbed.length === 0 && spaced.length === 0 ) {
250
+ return null;
251
+ }
252
+
253
+ // More lines tabbed than spaced? Assume tabs, and
254
+ // default to tabs in the case of a tie (or nothing
255
+ // to go on)
256
+ if ( tabbed.length >= spaced.length ) {
257
+ return '\t';
258
+ }
259
+
260
+ // Otherwise, we need to guess the multiple
261
+ min = spaced.reduce( function ( previous, current ) {
262
+ var numSpaces = /^ +/.exec( current )[0].length;
263
+ return Math.min( numSpaces, previous );
264
+ }, Infinity );
265
+
266
+ return new Array( min + 1 ).join( ' ' );
267
+ }
268
+
269
+ function encodeMappings ( original, str, mappings, hires, sourceIndex, offsets ) {
270
+ var lineStart,
271
+ locations,
272
+ lines,
273
+ encoded,
274
+ inverseMappings,
275
+ charOffset = 0,
276
+ firstSegment = true;
277
+
278
+ // store locations, for fast lookup
279
+ lineStart = 0;
280
+ locations = original.split( '\n' ).map( function ( line ) {
281
+ var start = lineStart;
282
+ lineStart += line.length + 1; // +1 for the newline
283
+
284
+ return start;
285
+ });
286
+
287
+ inverseMappings = invert( str, mappings );
288
+
289
+ lines = str.split( '\n' ).map( function ( line ) {
290
+ var segments, len, char, origin, lastOrigin, i, location;
291
+
292
+ segments = [];
293
+
294
+ len = line.length;
295
+ for ( i = 0; i < len; i += 1 ) {
296
+ char = i + charOffset;
297
+ origin = inverseMappings[ char ];
298
+
299
+ if ( !~origin ) {
300
+ if ( !~lastOrigin ) {
301
+ // do nothing
302
+ } else {
303
+ segments.push({
304
+ generatedCodeColumn: i,
305
+ sourceIndex: sourceIndex,
306
+ sourceCodeLine: 0,
307
+ sourceCodeColumn: 0
308
+ });
309
+ }
310
+ }
311
+
312
+ else {
313
+ if ( !hires && ( origin === lastOrigin + 1 ) ) {
314
+ // do nothing
315
+ } else {
316
+ location = getLocation( locations, origin );
317
+
318
+ segments.push({
319
+ generatedCodeColumn: i,
320
+ sourceIndex: sourceIndex,
321
+ sourceCodeLine: location.line,
322
+ sourceCodeColumn: location.column
323
+ });
324
+ }
325
+ }
326
+
327
+ lastOrigin = origin;
328
+ }
329
+
330
+ charOffset += line.length + 1;
331
+ return segments;
332
+ });
333
+
334
+ offsets = offsets || {};
335
+
336
+ offsets.sourceIndex = offsets.sourceIndex || 0;
337
+ offsets.sourceCodeLine = offsets.sourceCodeLine || 0;
338
+ offsets.sourceCodeColumn = offsets.sourceCodeColumn || 0;
339
+
340
+ encoded = lines.map( function ( segments ) {
341
+ var generatedCodeColumn = 0;
342
+
343
+ return segments.map( function ( segment ) {
344
+ var arr = [
345
+ segment.generatedCodeColumn - generatedCodeColumn,
346
+ segment.sourceIndex - offsets.sourceIndex,
347
+ segment.sourceCodeLine - offsets.sourceCodeLine,
348
+ segment.sourceCodeColumn - offsets.sourceCodeColumn
349
+ ];
350
+
351
+ generatedCodeColumn = segment.generatedCodeColumn;
352
+ offsets.sourceIndex = segment.sourceIndex;
353
+ offsets.sourceCodeLine = segment.sourceCodeLine;
354
+ offsets.sourceCodeColumn = segment.sourceCodeColumn;
355
+
356
+ firstSegment = false;
357
+
358
+ return vlq__default.encode( arr );
359
+ }).join( ',' );
360
+ }).join( ';' );
361
+
362
+ return encoded;
363
+ }
364
+
365
+
366
+ function invert ( str, mappings ) {
367
+ var inverted = new Uint32Array( str.length ), i;
368
+
369
+ // initialise everything to -1
370
+ i = str.length;
371
+ while ( i-- ) {
372
+ inverted[i] = -1;
373
+ }
374
+
375
+ // then apply the actual mappings
376
+ i = mappings.length;
377
+ while ( i-- ) {
378
+ if ( ~mappings[i] ) {
379
+ inverted[ mappings[i] ] = i;
380
+ }
381
+ }
382
+
383
+ return inverted;
384
+ }
385
+
386
+ function getLocation ( locations, char ) {
387
+ var i;
388
+
389
+ i = locations.length;
390
+ while ( i-- ) {
391
+ if ( locations[i] <= char ) {
392
+ return {
393
+ line: i,
394
+ column: char - locations[i]
395
+ };
396
+ }
397
+ }
398
+
399
+ throw new Error( 'Character out of bounds' );
400
+ }
401
+
402
+ var MagicString = function ( string ) {
403
+ this.original = this.str = string;
404
+ this.mappings = initMappings( string.length );
405
+
406
+ this.indentStr = guessIndent( string );
407
+ };
408
+
409
+ MagicString.prototype = {
410
+ append: function ( content ) {
411
+ this.str += content;
412
+ return this;
413
+ },
414
+
415
+ clone: function () {
416
+ var clone, i;
417
+
418
+ clone = new MagicString( this.original );
419
+ clone.str = this.str;
420
+
421
+ i = clone.mappings.length;
422
+ while ( i-- ) {
423
+ clone.mappings[i] = this.mappings[i];
424
+ }
425
+
426
+ return clone;
427
+ },
428
+
429
+ generateMap: function ( options ) {
430
+ options = options || {};
431
+
432
+ return new SourceMap({
433
+ file: ( options.file ? options.file.split( '/' ).pop() : null ),
434
+ sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ],
435
+ sourcesContent: options.includeContent ? [ this.original ] : [ null ],
436
+ names: [],
437
+ mappings: this.getMappings( options.hires, 0 )
438
+ });
439
+ },
440
+
441
+ getIndentString: function () {
442
+ return this.indentStr === null ? '\t' : this.indentStr;
443
+ },
444
+
445
+ getMappings: function ( hires, sourceIndex, offsets ) {
446
+ return encodeMappings( this.original, this.str, this.mappings, hires, sourceIndex, offsets );
447
+ },
448
+
449
+ indent: function ( indentStr, options ) {
450
+ var self = this,
451
+ mappings = this.mappings,
452
+ reverseMappings = reverse( mappings, this.str.length ),
453
+ pattern = /^[^\n]/gm,
454
+ match,
455
+ inserts = [],
456
+ adjustments,
457
+ exclusions,
458
+ lastEnd,
459
+ i;
460
+
461
+ if ( typeof indentStr === 'object' ) {
462
+ options = indentStr;
463
+ indentStr = undefined;
464
+ }
465
+
466
+ indentStr = indentStr !== undefined ? indentStr : ( this.indentStr || '\t' );
467
+
468
+ options = options || {};
469
+
470
+ // Process exclusion ranges
471
+ if ( options.exclude ) {
472
+ exclusions = typeof options.exclude[0] === 'number' ? [ options.exclude ] : options.exclude;
473
+
474
+ exclusions = exclusions.map( function ( range ) {
475
+ var rangeStart, rangeEnd;
476
+
477
+ rangeStart = self.locate( range[0] );
478
+ rangeEnd = self.locate( range[1] );
479
+
480
+ if ( rangeStart === null || rangeEnd === null ) {
481
+ throw new Error( 'Cannot use indices of replaced characters as exclusion ranges' );
482
+ }
483
+
484
+ return [ rangeStart, rangeEnd ];
485
+ });
486
+
487
+ exclusions.sort( function ( a, b ) {
488
+ return a[0] - b[0];
489
+ });
490
+
491
+ // check for overlaps
492
+ lastEnd = -1;
493
+ exclusions.forEach( function ( range ) {
494
+ if ( range[0] < lastEnd ) {
495
+ throw new Error( 'Exclusion ranges cannot overlap' );
496
+ }
497
+
498
+ lastEnd = range[1];
499
+ });
500
+ }
501
+
502
+ if ( !exclusions ) {
503
+ while ( match = pattern.exec( this.str ) ) {
504
+ inserts.push( match.index );
505
+ }
506
+
507
+ this.str = this.str.replace( pattern, indentStr + '$&' );
508
+ } else {
509
+ while ( match = pattern.exec( this.str ) ) {
510
+ if ( !isExcluded( match.index - 1 ) ) {
511
+ inserts.push( match.index );
512
+ }
513
+ }
514
+
515
+ this.str = this.str.replace( pattern, function ( match, index ) {
516
+ return isExcluded( index - 1 ) ? match : indentStr + match;
517
+ });
518
+ }
519
+
520
+ adjustments = inserts.map( function ( index ) {
521
+ var origin;
522
+
523
+ do {
524
+ origin = reverseMappings[ index++ ];
525
+ } while ( !~origin && index < self.str.length );
526
+
527
+ return origin;
528
+ });
529
+
530
+ i = adjustments.length;
531
+ lastEnd = this.mappings.length;
532
+ while ( i-- ) {
533
+ adjust( self.mappings, adjustments[i], lastEnd, ( ( i + 1 ) * indentStr.length ) );
534
+ lastEnd = adjustments[i];
535
+ }
536
+
537
+ return this;
538
+
539
+ function isExcluded ( index ) {
540
+ var i = exclusions.length, range;
541
+
542
+ while ( i-- ) {
543
+ range = exclusions[i];
544
+
545
+ if ( range[1] < index ) {
546
+ return false;
547
+ }
548
+
549
+ if ( range[0] <= index ) {
550
+ return true;
551
+ }
552
+ }
553
+ }
554
+ },
555
+
556
+ insert: function ( index, content ) {
557
+ if ( index === 0 ) {
558
+ this.prepend( content );
559
+ } else if ( index === this.original.length ) {
560
+ this.append( content );
561
+ } else {
562
+ this.replace( index, index, content );
563
+ }
564
+
565
+ return this;
566
+ },
567
+
568
+ // get current location of character in original string
569
+ locate: function ( character ) {
570
+ var loc;
571
+
572
+ if ( character < 0 || character > this.mappings.length ) {
573
+ throw new Error( 'Character is out of bounds' );
574
+ }
575
+
576
+ loc = this.mappings[ character ];
577
+ return ~loc ? loc : null;
578
+ },
579
+
580
+ locateOrigin: function ( character ) {
581
+ var i;
582
+
583
+ if ( character < 0 || character >= this.str.length ) {
584
+ throw new Error( 'Character is out of bounds' );
585
+ }
586
+
587
+ i = this.mappings.length;
588
+ while ( i-- ) {
589
+ if ( this.mappings[i] === character ) {
590
+ return i;
591
+ }
592
+ }
593
+
594
+ return null;
595
+ },
596
+
597
+ prepend: function ( content ) {
598
+ this.str = content + this.str;
599
+ adjust( this.mappings, 0, this.mappings.length, content.length );
600
+ return this;
601
+ },
602
+
603
+ remove: function ( start, end ) {
604
+ this.replace( start, end, '' );
605
+ return this;
606
+ },
607
+
608
+ replace: function ( start, end, content ) {
609
+ var firstChar, lastChar, d;
610
+
611
+ firstChar = this.locate( start );
612
+ lastChar = this.locate( end - 1 );
613
+
614
+ if ( firstChar === null || lastChar === null ) {
615
+ throw new Error( 'Cannot replace the same content twice' );
616
+ }
617
+
618
+ this.str = this.str.substr( 0, firstChar ) + content + this.str.substring( lastChar + 1 );
619
+
620
+ d = content.length - ( lastChar + 1 - firstChar );
621
+
622
+ blank( this.mappings, start, end );
623
+ adjust( this.mappings, end, this.mappings.length, d );
624
+ return this;
625
+ },
626
+
627
+ slice: function ( start, end ) {
628
+ var firstChar, lastChar;
629
+
630
+ firstChar = this.locate( start );
631
+ lastChar = this.locate( end - 1 ) + 1;
632
+
633
+ if ( firstChar === null || lastChar === null ) {
634
+ throw new Error( 'Cannot use replaced characters as slice anchors' );
635
+ }
636
+
637
+ return this.str.slice( firstChar, lastChar );
638
+ },
639
+
640
+ toString: function () {
641
+ return this.str;
642
+ },
643
+
644
+ trimLines: function() {
645
+ return this.trim('[\\r\\n]');
646
+ },
647
+
648
+ trim: function (charType) {
649
+ return this.trimStart(charType).trimEnd(charType);
650
+ },
651
+
652
+ trimEnd: function (charType) {
653
+ var self = this;
654
+ var rx = new RegExp((charType || '\\s') + '+$');
655
+
656
+ this.str = this.str.replace( rx, function ( trailing, index, str ) {
657
+ var strLength = str.length,
658
+ length = trailing.length,
659
+ i,
660
+ chars = [];
661
+
662
+ i = strLength;
663
+ while ( i-- > strLength - length ) {
664
+ chars.push( self.locateOrigin( i ) );
665
+ }
666
+
667
+ i = chars.length;
668
+ while ( i-- ) {
669
+ if ( chars[i] !== null ) {
670
+ self.mappings[ chars[i] ] = -1;
671
+ }
672
+ }
673
+
674
+ return '';
675
+ });
676
+
677
+ return this;
678
+ },
679
+
680
+ trimStart: function (charType) {
681
+ var self = this;
682
+ var rx = new RegExp('^' + (charType || '\\s') + '+');
683
+
684
+ this.str = this.str.replace( rx, function ( leading ) {
685
+ var length = leading.length, i, chars = [], adjustmentStart = 0;
686
+
687
+ i = length;
688
+ while ( i-- ) {
689
+ chars.push( self.locateOrigin( i ) );
690
+ }
691
+
692
+ i = chars.length;
693
+ while ( i-- ) {
694
+ if ( chars[i] !== null ) {
695
+ self.mappings[ chars[i] ] = -1;
696
+ adjustmentStart += 1;
697
+ }
698
+ }
699
+
700
+ adjust( self.mappings, adjustmentStart, self.mappings.length, -length );
701
+
702
+ return '';
703
+ });
704
+
705
+ return this;
706
+ }
707
+ };
708
+
709
+ MagicString.Bundle = Bundle;
710
+
711
+ function adjust ( mappings, start, end, d ) {
712
+ var i = end;
713
+
714
+ if ( !d ) return; // replacement is same length as replaced string
715
+
716
+ while ( i-- > start ) {
717
+ if ( ~mappings[i] ) {
718
+ mappings[i] += d;
719
+ }
720
+ }
721
+ }
722
+
723
+ function initMappings ( i ) {
724
+ var mappings = new Uint32Array( i );
725
+
726
+ while ( i-- ) {
727
+ mappings[i] = i;
728
+ }
729
+
730
+ return mappings;
731
+ }
732
+
733
+ function blank ( mappings, start, i ) {
734
+ while ( i-- > start ) {
735
+ mappings[i] = -1;
736
+ }
737
+ }
738
+
739
+ function reverse ( mappings, i ) {
740
+ var result, location;
741
+
742
+ result = new Uint32Array( i );
743
+
744
+ while ( i-- ) {
745
+ result[i] = -1;
746
+ }
747
+
748
+ i = mappings.length;
749
+ while ( i-- ) {
750
+ location = mappings[i];
751
+
752
+ if ( ~location ) {
753
+ result[ location ] = i;
754
+ }
755
+ }
756
+
757
+ return result;
758
+ }
759
+
760
+ var index = MagicString;
761
+
762
+ return index;
763
+
764
+ }));
765
+ //# sourceMappingURL=./magic-string.js.map