11import { COLUMN , SOURCES_INDEX , SOURCE_LINE , SOURCE_COLUMN } from './sourcemap-segment' ;
2- import { memoizedBinarySearch , upperBound } from './binary-search' ;
32
43import type { ReverseSegment , SourceMapSegment } from './sourcemap-segment' ;
5- import type { MemoState } from './binary-search' ;
64
75export type Source = {
86 __proto__ : null ;
7+ length : number ;
98 [ line : number ] : Exclude < ReverseSegment , [ number ] > [ ] ;
109} ;
1110
1211// Rebuilds the original source files, with mappings that are ordered by source line/column instead
1312// of generated line/column.
1413export default function buildBySources (
1514 decoded : readonly SourceMapSegment [ ] [ ] ,
16- memos : MemoState [ ] ,
15+ sourcesCount : number ,
1716) : Source [ ] {
18- const sources : Source [ ] = memos . map ( buildNullArray ) ;
19- const todo : ReverseSegment [ ] [ ] [ ] = [ ] ;
17+ const sources : Source [ ] = Array . from ( { length : sourcesCount } , buildNullArray < Source > ) ;
2018
2119 for ( let i = 0 ; i < decoded . length ; i ++ ) {
2220 const line = decoded [ i ] ;
@@ -28,58 +26,16 @@ export default function buildBySources(
2826 const sourceLine = seg [ SOURCE_LINE ] ;
2927 const sourceColumn = seg [ SOURCE_COLUMN ] ;
3028
31- todo [ sourceIndex ] ||= [ ] ;
32- ( todo [ sourceIndex ] [ sourceLine ] ||= [ ] ) . push ( [ sourceColumn , i , seg [ COLUMN ] ] ) ;
29+ const source = sources [ sourceIndex ] ;
30+ source . length = Math . max ( source . length || 0 , sourceLine + 1 ) ;
31+ ( source [ sourceLine ] ||= [ ] ) . push ( [ sourceColumn , i , seg [ COLUMN ] ] ) ;
3332 }
3433 }
3534
36- for ( let sourceIndex = 0 ; sourceIndex < todo . length ; sourceIndex ++ ) {
37- const lines = todo [ sourceIndex ] ;
38- if ( ! lines ) continue ;
39- const originalSource = sources [ sourceIndex ] ;
40- const memo = memos [ sourceIndex ] ;
41-
42- for ( let sourceLine = 0 ; sourceLine < lines . length ; sourceLine ++ ) {
43- const segs = lines [ sourceLine ] ;
44- if ( ! segs ) continue ;
45-
46- segs . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
47- const originalLine = ( originalSource [ sourceLine ] ||= [ ] ) ;
48-
49- let lastIndex = 0 ;
50- const newOriginalLine : ReverseSegment [ ] = [ ] ;
51- for ( let i = 0 ; i < segs . length ; i ++ ) {
52- const seg = segs [ i ] ;
53-
54- // The binary search either found a match, or it found the left-index just before where the
55- // segment should go. Either way, we want to insert after that. And there may be multiple
56- // generated segments associated with an original location, so there may need to move several
57- // indexes before we find where we need to insert.
58- const originalLineJoint = jointArray ( newOriginalLine , originalLine , lastIndex ) ;
59- let index = upperBound (
60- originalLineJoint ,
61- seg [ 0 ] ,
62- memoizedBinarySearch ( originalLineJoint , seg [ 0 ] , memo , sourceLine ) ,
63- ) ;
64-
65- memo . lastIndex = ++ index ;
66- if ( index <= newOriginalLine . length ) {
67- insert ( newOriginalLine , index , seg ) ;
68- continue ;
69- }
70-
71- index = index - newOriginalLine . length + lastIndex ;
72- while ( lastIndex < index ) {
73- newOriginalLine . push ( originalLine [ lastIndex ++ ] ) ;
74- }
75-
76- newOriginalLine . push ( seg ) ;
77- }
78-
79- while ( lastIndex < originalLine . length ) {
80- newOriginalLine . push ( originalLine [ lastIndex ++ ] ) ;
81- }
82- originalSource [ sourceLine ] = newOriginalLine ;
35+ for ( let sourceIndex = 0 ; sourceIndex < sources . length ; sourceIndex ++ ) {
36+ const source = sources [ sourceIndex ] ;
37+ for ( let line = 0 ; line < source . length ; line ++ ) {
38+ source [ line ] ?. sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
8339 }
8440 }
8541
@@ -94,21 +50,3 @@ export default function buildBySources(
9450function buildNullArray < T extends { __proto__ : null } > ( ) : T {
9551 return { __proto__ : null } as T ;
9652}
97-
98- function jointArray < T > ( a : T [ ] , b : T [ ] , bOffset : number ) : T [ ] {
99- return new Proxy ( [ ] , {
100- get ( _ , p ) {
101- if ( p === 'length' ) return a . length + ( b . length - bOffset ) ;
102- const index = typeof p === 'symbol' ? NaN : parseInt ( p , 10 ) ;
103- if ( isNaN ( index ) ) return ( a as any ) [ p ] ;
104- return index < a . length ? a [ index ] : b [ index - bOffset ] ;
105- } ,
106- } ) ;
107- }
108-
109- function insert < T > ( array : T [ ] , index : number , value : T ) {
110- for ( let i = array . length ; i > index ; i -- ) {
111- array [ i ] = array [ i - 1 ] ;
112- }
113- array [ index ] = value ;
114- }
0 commit comments