@@ -15,7 +15,106 @@ type MappingSource = SourceMapSegmentObject | typeof INVALID_MAPPING | typeof SO
15
15
* traceMappings is only called on the root level SourceMapTree, and begins the process of
16
16
* resolving each mapping in terms of the original source files.
17
17
*/
18
- export let traceMappings : ( tree : SourceMapTree ) => TraceMap ;
18
+ export function traceMappings ( tree : SourceMapTree ) : TraceMap {
19
+ const mappings : SourceMapSegment [ ] [ ] = [ ] ;
20
+ const names = new FastStringArray ( ) ;
21
+ const sources = new FastStringArray ( ) ;
22
+ const sourcesContent : ( string | null ) [ ] = [ ] ;
23
+ const { sources : rootSources , map } = tree ;
24
+ const rootNames = map . names ;
25
+ const rootMappings = decodedMappings ( map ) ;
26
+
27
+ let lastLineWithSegment = - 1 ;
28
+ for ( let i = 0 ; i < rootMappings . length ; i ++ ) {
29
+ const segments = rootMappings [ i ] ;
30
+ const tracedSegments : SourceMapSegment [ ] = [ ] ;
31
+
32
+ let lastSourcesIndex = - 1 ;
33
+ let lastSourceLine = - 1 ;
34
+ let lastSourceColumn = - 1 ;
35
+
36
+ for ( let j = 0 ; j < segments . length ; j ++ ) {
37
+ const segment = segments [ j ] ;
38
+
39
+ let traced : MappingSource = SOURCELESS_MAPPING ;
40
+ // 1-length segments only move the current generated column, there's no source information
41
+ // to gather from it.
42
+ if ( segment . length !== 1 ) {
43
+ const source = rootSources [ segment [ 1 ] ] ;
44
+ traced = source . originalPositionFor (
45
+ segment [ 2 ] ,
46
+ segment [ 3 ] ,
47
+ segment . length === 5 ? rootNames [ segment [ 4 ] ] : ''
48
+ ) ;
49
+
50
+ // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
51
+ // respective segment into an original source.
52
+ if ( traced === INVALID_MAPPING ) continue ;
53
+ }
54
+
55
+ const genCol = segment [ 0 ] ;
56
+ if ( traced === SOURCELESS_MAPPING ) {
57
+ if ( lastSourcesIndex === - 1 ) {
58
+ // This is a consecutive source-less segment, which doesn't carry any new information.
59
+ continue ;
60
+ }
61
+ lastSourcesIndex = lastSourceLine = lastSourceColumn = - 1 ;
62
+ tracedSegments . push ( [ genCol ] ) ;
63
+ continue ;
64
+ }
65
+
66
+ // So we traced a segment down into its original source file. Now push a
67
+ // new segment pointing to this location.
68
+ const { column, line, name, content, source } = traced ;
69
+
70
+ // Store the source location, and ensure we keep sourcesContent up to
71
+ // date with the sources array.
72
+ const sourcesIndex = put ( sources , source ) ;
73
+ sourcesContent [ sourcesIndex ] = content ;
74
+
75
+ if (
76
+ lastSourcesIndex === sourcesIndex &&
77
+ lastSourceLine === line &&
78
+ lastSourceColumn === column
79
+ ) {
80
+ // This is a duplicate mapping pointing at the exact same starting point in the source
81
+ // file. It doesn't carry any new information, and only bloats the sourcemap.
82
+ continue ;
83
+ }
84
+ lastLineWithSegment = i ;
85
+ lastSourcesIndex = sourcesIndex ;
86
+ lastSourceLine = line ;
87
+ lastSourceColumn = column ;
88
+
89
+ // This looks like unnecessary duplication, but it noticeably increases performance. If we
90
+ // were to push the nameIndex onto length-4 array, v8 would internally allocate 22 slots!
91
+ // That's 68 wasted bytes! Array literals have the same capacity as their length, saving
92
+ // memory.
93
+ tracedSegments . push (
94
+ name
95
+ ? [ genCol , sourcesIndex , line , column , put ( names , name ) ]
96
+ : [ genCol , sourcesIndex , line , column ]
97
+ ) ;
98
+ }
99
+
100
+ mappings . push ( tracedSegments ) ;
101
+ }
102
+
103
+ if ( mappings . length > lastLineWithSegment + 1 ) {
104
+ mappings . length = lastLineWithSegment + 1 ;
105
+ }
106
+
107
+ return presortedDecodedMap (
108
+ Object . assign ( { } , tree . map , {
109
+ mappings,
110
+ // TODO: Make all sources relative to the sourceRoot.
111
+ sourceRoot : undefined ,
112
+ names : names . array ,
113
+ sources : sources . array ,
114
+ sourcesContent,
115
+ } )
116
+ ) ;
117
+ }
19
118
20
119
/**
21
120
* SourceMapTree represents a single sourcemap, with the ability to trace
@@ -30,109 +129,6 @@ export class SourceMapTree {
30
129
this . sources = sources ;
31
130
}
32
131
33
- static {
34
- traceMappings = ( tree ) => {
35
- const mappings : SourceMapSegment [ ] [ ] = [ ] ;
36
- const names = new FastStringArray ( ) ;
37
- const sources = new FastStringArray ( ) ;
38
- const sourcesContent : ( string | null ) [ ] = [ ] ;
39
- const { sources : rootSources , map } = tree ;
40
- const rootNames = map . names ;
41
- const rootMappings = decodedMappings ( map ) ;
42
-
43
- let lastLineWithSegment = - 1 ;
44
- for ( let i = 0 ; i < rootMappings . length ; i ++ ) {
45
- const segments = rootMappings [ i ] ;
46
- const tracedSegments : SourceMapSegment [ ] = [ ] ;
47
-
48
- let lastSourcesIndex = - 1 ;
49
- let lastSourceLine = - 1 ;
50
- let lastSourceColumn = - 1 ;
51
-
52
- for ( let j = 0 ; j < segments . length ; j ++ ) {
53
- const segment = segments [ j ] ;
54
-
55
- let traced : MappingSource = SOURCELESS_MAPPING ;
56
- // 1-length segments only move the current generated column, there's no source information
57
- // to gather from it.
58
- if ( segment . length !== 1 ) {
59
- const source = rootSources [ segment [ 1 ] ] ;
60
- traced = source . originalPositionFor (
61
- segment [ 2 ] ,
62
- segment [ 3 ] ,
63
- segment . length === 5 ? rootNames [ segment [ 4 ] ] : ''
64
- ) ;
65
-
66
- // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
67
- // respective segment into an original source.
68
- if ( traced === INVALID_MAPPING ) continue ;
69
- }
70
-
71
- const genCol = segment [ 0 ] ;
72
- if ( traced === SOURCELESS_MAPPING ) {
73
- if ( lastSourcesIndex === - 1 ) {
74
- // This is a consecutive source-less segment, which doesn't carry any new information.
75
- continue ;
76
- }
77
- lastSourcesIndex = lastSourceLine = lastSourceColumn = - 1 ;
78
- tracedSegments . push ( [ genCol ] ) ;
79
- continue ;
80
- }
81
-
82
- // So we traced a segment down into its original source file. Now push a
83
- // new segment pointing to this location.
84
- const { column, line, name, content, source } = traced ;
85
-
86
- // Store the source location, and ensure we keep sourcesContent up to
87
- // date with the sources array.
88
- const sourcesIndex = put ( sources , source ) ;
89
- sourcesContent [ sourcesIndex ] = content ;
90
-
91
- if (
92
- lastSourcesIndex === sourcesIndex &&
93
- lastSourceLine === line &&
94
- lastSourceColumn === column
95
- ) {
96
- // This is a duplicate mapping pointing at the exact same starting point in the source
97
- // file. It doesn't carry any new information, and only bloats the sourcemap.
98
- continue ;
99
- }
100
- lastLineWithSegment = i ;
101
- lastSourcesIndex = sourcesIndex ;
102
- lastSourceLine = line ;
103
- lastSourceColumn = column ;
104
-
105
- // This looks like unnecessary duplication, but it noticeably increases performance. If we
106
- // were to push the nameIndex onto length-4 array, v8 would internally allocate 22 slots!
107
- // That's 68 wasted bytes! Array literals have the same capacity as their length, saving
108
- // memory.
109
- tracedSegments . push (
110
- name
111
- ? [ genCol , sourcesIndex , line , column , put ( names , name ) ]
112
- : [ genCol , sourcesIndex , line , column ]
113
- ) ;
114
- }
115
-
116
- mappings . push ( tracedSegments ) ;
117
- }
118
-
119
- if ( mappings . length > lastLineWithSegment + 1 ) {
120
- mappings . length = lastLineWithSegment + 1 ;
121
- }
122
-
123
- return presortedDecodedMap (
124
- Object . assign ( { } , tree . map , {
125
- mappings,
126
- // TODO: Make all sources relative to the sourceRoot.
127
- sourceRoot : undefined ,
128
- names : names . array ,
129
- sources : sources . array ,
130
- sourcesContent,
131
- } )
132
- ) ;
133
- } ;
134
- }
135
-
136
132
/**
137
133
* originalPositionFor is only called on children SourceMapTrees. It recurses down
138
134
* into its own child SourceMapTrees, until we find the original source map.
0 commit comments