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