1
1
import { SetArray , put , remove } from '@jridgewell/set-array' ;
2
- import { encode } from '@jridgewell/sourcemap-codec' ;
2
+ import { encode , encodeGeneratedRanges , encodeOriginalScopes } from '@jridgewell/sourcemap-codec' ;
3
3
import { TraceMap , decodedMappings } from '@jridgewell/trace-mapping' ;
4
4
5
5
import {
@@ -11,8 +11,18 @@ import {
11
11
} from './sourcemap-segment' ;
12
12
13
13
import type { SourceMapInput } from '@jridgewell/trace-mapping' ;
14
+ import type { OriginalScope , GeneratedRange } from '@jridgewell/sourcemap-codec' ;
14
15
import type { SourceMapSegment } from './sourcemap-segment' ;
15
- import type { DecodedSourceMap , EncodedSourceMap , Pos , Mapping } from './types' ;
16
+ import type {
17
+ DecodedSourceMap ,
18
+ EncodedSourceMap ,
19
+ Pos ,
20
+ Mapping ,
21
+ BindingExpressionRange ,
22
+ OriginalPos ,
23
+ OriginalScopeInfo ,
24
+ GeneratedRangeInfo ,
25
+ } from './types' ;
16
26
17
27
export type { DecodedSourceMap , EncodedSourceMap , Mapping } ;
18
28
@@ -31,6 +41,8 @@ export class GenMapping {
31
41
private declare _sources : SetArray < string > ;
32
42
private declare _sourcesContent : ( string | null ) [ ] ;
33
43
private declare _mappings : SourceMapSegment [ ] [ ] ;
44
+ private declare _originalScopes : OriginalScope [ ] [ ] ;
45
+ private declare _generatedRanges : GeneratedRange [ ] ;
34
46
private declare _ignoreList : SetArray < number > ;
35
47
declare file : string | null | undefined ;
36
48
declare sourceRoot : string | null | undefined ;
@@ -40,6 +52,8 @@ export class GenMapping {
40
52
this . _sources = new SetArray ( ) ;
41
53
this . _sourcesContent = [ ] ;
42
54
this . _mappings = [ ] ;
55
+ this . _originalScopes = [ ] ;
56
+ this . _generatedRanges = [ ] ;
43
57
this . file = file ;
44
58
this . sourceRoot = sourceRoot ;
45
59
this . _ignoreList = new SetArray ( ) ;
@@ -51,6 +65,8 @@ interface PublicMap {
51
65
_sources : GenMapping [ '_sources' ] ;
52
66
_sourcesContent : GenMapping [ '_sourcesContent' ] ;
53
67
_mappings : GenMapping [ '_mappings' ] ;
68
+ _originalScopes : GenMapping [ '_originalScopes' ] ;
69
+ _generatedRanges : GenMapping [ '_generatedRanges' ] ;
54
70
_ignoreList : GenMapping [ '_ignoreList' ] ;
55
71
}
56
72
@@ -207,15 +223,26 @@ export const maybeAddMapping: typeof addMapping = (map, mapping) => {
207
223
* Adds/removes the content of the source file to the source map.
208
224
*/
209
225
export function setSourceContent ( map : GenMapping , source : string , content : string | null ) : void {
210
- const { _sources : sources , _sourcesContent : sourcesContent } = cast ( map ) ;
226
+ const {
227
+ _sources : sources ,
228
+ _sourcesContent : sourcesContent ,
229
+ _originalScopes : originalScopes ,
230
+ } = cast ( map ) ;
211
231
const index = put ( sources , source ) ;
212
232
sourcesContent [ index ] = content ;
233
+ if ( index === originalScopes . length ) originalScopes [ index ] = [ ] ;
213
234
}
214
235
215
236
export function setIgnore ( map : GenMapping , source : string , ignore = true ) {
216
- const { _sources : sources , _sourcesContent : sourcesContent , _ignoreList : ignoreList } = cast ( map ) ;
237
+ const {
238
+ _sources : sources ,
239
+ _sourcesContent : sourcesContent ,
240
+ _ignoreList : ignoreList ,
241
+ _originalScopes : originalScopes ,
242
+ } = cast ( map ) ;
217
243
const index = put ( sources , source ) ;
218
244
if ( index === sourcesContent . length ) sourcesContent [ index ] = null ;
245
+ if ( index === originalScopes . length ) originalScopes [ index ] = [ ] ;
219
246
if ( ignore ) put ( ignoreList , index ) ;
220
247
else remove ( ignoreList , index ) ;
221
248
}
@@ -231,6 +258,8 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
231
258
_sourcesContent : sourcesContent ,
232
259
_names : names ,
233
260
_ignoreList : ignoreList ,
261
+ _originalScopes : originalScopes ,
262
+ _generatedRanges : generatedRanges ,
234
263
} = cast ( map ) ;
235
264
removeEmptyFinalLines ( mappings ) ;
236
265
@@ -242,6 +271,8 @@ export function toDecodedMap(map: GenMapping): DecodedSourceMap {
242
271
sources : sources . array ,
243
272
sourcesContent,
244
273
mappings,
274
+ originalScopes,
275
+ generatedRanges,
245
276
ignoreList : ignoreList . array ,
246
277
} ;
247
278
}
@@ -254,6 +285,8 @@ export function toEncodedMap(map: GenMapping): EncodedSourceMap {
254
285
const decoded = toDecodedMap ( map ) ;
255
286
return {
256
287
...decoded ,
288
+ originalScopes : decoded . originalScopes . map ( ( os ) => encodeOriginalScopes ( os ) ) ,
289
+ generatedRanges : encodeGeneratedRanges ( decoded . generatedRanges as GeneratedRange [ ] ) ,
257
290
mappings : encode ( decoded . mappings as SourceMapSegment [ ] [ ] ) ,
258
291
} ;
259
292
}
@@ -269,6 +302,7 @@ export function fromMap(input: SourceMapInput): GenMapping {
269
302
putAll ( cast ( gen ) . _sources , map . sources as string [ ] ) ;
270
303
cast ( gen ) . _sourcesContent = map . sourcesContent || map . sources . map ( ( ) => null ) ;
271
304
cast ( gen ) . _mappings = decodedMappings ( map ) as GenMapping [ '_mappings' ] ;
305
+ // TODO: implement originalScopes/generatedRanges
272
306
if ( map . ignoreList ) putAll ( cast ( gen ) . _ignoreList , map . ignoreList ) ;
273
307
274
308
return gen ;
@@ -323,8 +357,9 @@ function addSegmentInternal<S extends string | null | undefined>(
323
357
_sources : sources ,
324
358
_sourcesContent : sourcesContent ,
325
359
_names : names ,
360
+ _originalScopes : originalScopes ,
326
361
} = cast ( map ) ;
327
- const line = getLine ( mappings , genLine ) ;
362
+ const line = getIndex ( mappings , genLine ) ;
328
363
const index = getColumnIndex ( line , genColumn ) ;
329
364
330
365
if ( ! source ) {
@@ -340,6 +375,7 @@ function addSegmentInternal<S extends string | null | undefined>(
340
375
const sourcesIndex = put ( sources , source ) ;
341
376
const namesIndex = name ? put ( names , name ) : NO_NAME ;
342
377
if ( sourcesIndex === sourcesContent . length ) sourcesContent [ sourcesIndex ] = content ?? null ;
378
+ if ( sourcesIndex === originalScopes . length ) originalScopes [ sourcesIndex ] = [ ] ;
343
379
344
380
if ( skipable && skipSource ( line , index , sourcesIndex , sourceLine , sourceColumn , namesIndex ) ) {
345
381
return ;
@@ -358,11 +394,11 @@ function assert<T>(_val: unknown): asserts _val is T {
358
394
// noop.
359
395
}
360
396
361
- function getLine ( mappings : SourceMapSegment [ ] [ ] , index : number ) : SourceMapSegment [ ] {
362
- for ( let i = mappings . length ; i <= index ; i ++ ) {
363
- mappings [ i ] = [ ] ;
397
+ function getIndex < T > ( arr : T [ ] [ ] , index : number ) : T [ ] {
398
+ for ( let i = arr . length ; i <= index ; i ++ ) {
399
+ arr [ i ] = [ ] ;
364
400
}
365
- return mappings [ index ] ;
401
+ return arr [ index ] ;
366
402
}
367
403
368
404
function getColumnIndex ( line : SourceMapSegment [ ] , genColumn : number ) : number {
@@ -470,3 +506,102 @@ function addMappingInternal<S extends string | null | undefined>(
470
506
content ,
471
507
) ;
472
508
}
509
+
510
+ export function addOriginalScope (
511
+ map : GenMapping ,
512
+ data : {
513
+ start : Pos ;
514
+ end : Pos ;
515
+ source : string ;
516
+ kind : string ;
517
+ name ?: string ;
518
+ variables ?: string [ ] ;
519
+ } ,
520
+ ) : OriginalScopeInfo {
521
+ const { start, end, source, kind, name, variables } = data ;
522
+ const {
523
+ _sources : sources ,
524
+ _sourcesContent : sourcesContent ,
525
+ _originalScopes : originalScopes ,
526
+ _names : names ,
527
+ } = cast ( map ) ;
528
+ const index = put ( sources , source ) ;
529
+ if ( index === sourcesContent . length ) sourcesContent [ index ] = null ;
530
+ if ( index === originalScopes . length ) originalScopes [ index ] = [ ] ;
531
+
532
+ const kindIndex = put ( names , kind ) ;
533
+ const scope : OriginalScope = name
534
+ ? [ start . line - 1 , start . column , end . line - 1 , end . column , kindIndex , put ( names , name ) ]
535
+ : [ start . line - 1 , start . column , end . line - 1 , end . column , kindIndex ] ;
536
+ if ( variables ) {
537
+ scope . vars = variables . map ( ( v ) => put ( names , v ) ) ;
538
+ }
539
+ const len = originalScopes [ index ] . push ( scope ) ;
540
+ return [ index , len - 1 , variables ] ;
541
+ }
542
+
543
+ // Generated Ranges
544
+ export function addGeneratedRange (
545
+ map : GenMapping ,
546
+ data : {
547
+ start : Pos ;
548
+ isScope : boolean ;
549
+ originalScope ?: OriginalScopeInfo ;
550
+ callsite ?: OriginalPos ;
551
+ } ,
552
+ ) : GeneratedRangeInfo {
553
+ const { start, isScope, originalScope, callsite } = data ;
554
+ const {
555
+ _originalScopes : originalScopes ,
556
+ _sources : sources ,
557
+ _sourcesContent : sourcesContent ,
558
+ _generatedRanges : generatedRanges ,
559
+ } = cast ( map ) ;
560
+
561
+ const range : GeneratedRange = [
562
+ start . line - 1 ,
563
+ start . column ,
564
+ 0 ,
565
+ 0 ,
566
+ originalScope ? originalScope [ 0 ] : - 1 ,
567
+ originalScope ? originalScope [ 1 ] : - 1 ,
568
+ ] ;
569
+ if ( originalScope ?. [ 2 ] ) {
570
+ range . bindings = originalScope [ 2 ] . map ( ( ) => [ [ - 1 ] ] ) ;
571
+ }
572
+ if ( callsite ) {
573
+ const index = put ( sources , callsite . source ) ;
574
+ if ( index === sourcesContent . length ) sourcesContent [ index ] = null ;
575
+ if ( index === originalScopes . length ) originalScopes [ index ] = [ ] ;
576
+ range . callsite = [ index , callsite . line - 1 , callsite . column ] ;
577
+ }
578
+ if ( isScope ) range . isScope = true ;
579
+ generatedRanges . push ( range ) ;
580
+
581
+ return [ range , originalScope ?. [ 2 ] ] ;
582
+ }
583
+
584
+ export function setEndPosition ( range : GeneratedRangeInfo , pos : Pos ) {
585
+ range [ 0 ] [ 2 ] = pos . line - 1 ;
586
+ range [ 0 ] [ 3 ] = pos . column ;
587
+ }
588
+
589
+ export function addBinding (
590
+ map : GenMapping ,
591
+ range : GeneratedRangeInfo ,
592
+ variable : string ,
593
+ expression : string | BindingExpressionRange ,
594
+ ) {
595
+ const { _names : names } = cast ( map ) ;
596
+ const bindings = ( range [ 0 ] . bindings ||= [ ] ) ;
597
+ const vars = range [ 1 ] ;
598
+
599
+ const index = vars ! . indexOf ( variable ) ;
600
+ const binding = getIndex ( bindings , index ) ;
601
+
602
+ if ( typeof expression === 'string' ) binding [ 0 ] = [ put ( names , expression ) ] ;
603
+ else {
604
+ const { start } = expression ;
605
+ binding . push ( [ put ( names , expression . expression ) , start . line - 1 , start . column ] ) ;
606
+ }
607
+ }
0 commit comments