@@ -2,20 +2,65 @@ import { FastStringArray, put } from './fast-string-array';
2
2
import { presortedDecodedMap , traceSegment , decodedMappings } from '@jridgewell/trace-mapping' ;
3
3
4
4
import type { TraceMap } from '@jridgewell/trace-mapping' ;
5
- import type OriginalSource from './original-source' ;
6
5
import type { SourceMapSegment , SourceMapSegmentObject } from './types' ;
7
6
8
- type Sources = OriginalSource | SourceMapTree ;
9
-
10
7
const INVALID_MAPPING = undefined ;
11
8
const SOURCELESS_MAPPING = null ;
9
+ const EMPTY_SOURCES : Sources [ ] = [ ] ;
10
+
12
11
type MappingSource = SourceMapSegmentObject | typeof INVALID_MAPPING | typeof SOURCELESS_MAPPING ;
13
12
13
+ type OriginalSource = {
14
+ map : TraceMap ;
15
+ sources : Sources [ ] ;
16
+ source : string ;
17
+ content : string | null ;
18
+ } ;
19
+
20
+ type MapSource = {
21
+ map : TraceMap ;
22
+ sources : Sources [ ] ;
23
+ source : string ;
24
+ content : string | null ;
25
+ } ;
26
+
27
+ export type Sources = OriginalSource | MapSource ;
28
+
29
+ function Source < M extends TraceMap | null > (
30
+ map : TraceMap | null ,
31
+ sources : Sources [ ] ,
32
+ source : string ,
33
+ content : string | null
34
+ ) : M extends null ? OriginalSource : MapSource {
35
+ return {
36
+ map,
37
+ sources,
38
+ source,
39
+ content,
40
+ } as any ;
41
+ }
42
+
43
+ /**
44
+ * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
45
+ * (which may themselves be SourceMapTrees).
46
+ */
47
+ export function MapSource ( map : TraceMap , sources : Sources [ ] ) : MapSource {
48
+ return Source ( map , sources , '' , null ) ;
49
+ }
50
+
51
+ /**
52
+ * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
53
+ * segment tracing ends at the `OriginalSource`.
54
+ */
55
+ export function OriginalSource ( source : string , content : string | null ) : OriginalSource {
56
+ return Source ( null , EMPTY_SOURCES , source , content ) ;
57
+ }
58
+
14
59
/**
15
60
* traceMappings is only called on the root level SourceMapTree, and begins the process of
16
61
* resolving each mapping in terms of the original source files.
17
62
*/
18
- export function traceMappings ( tree : SourceMapTree ) : TraceMap {
63
+ export function traceMappings ( tree : Sources ) : TraceMap {
19
64
const mappings : SourceMapSegment [ ] [ ] = [ ] ;
20
65
const names = FastStringArray ( ) ;
21
66
const sources = FastStringArray ( ) ;
@@ -41,7 +86,8 @@ export function traceMappings(tree: SourceMapTree): TraceMap {
41
86
// to gather from it.
42
87
if ( segment . length !== 1 ) {
43
88
const source = rootSources [ segment [ 1 ] ] ;
44
- traced = source . originalPositionFor (
89
+ traced = originalPositionFor (
90
+ source ,
45
91
segment [ 2 ] ,
46
92
segment [ 3 ] ,
47
93
segment . length === 5 ? rootNames [ segment [ 4 ] ] : ''
@@ -117,36 +163,31 @@ export function traceMappings(tree: SourceMapTree): TraceMap {
117
163
}
118
164
119
165
/**
120
- * SourceMapTree represents a single sourcemap, with the ability to trace
121
- * mappings into its child nodes (which may themselves be SourceMapTrees) .
166
+ * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
167
+ * child SourceMapTrees, until we find the original source map .
122
168
*/
123
- export class SourceMapTree {
124
- declare map : TraceMap ;
125
- declare sources : Sources [ ] ;
126
-
127
- constructor ( map : TraceMap , sources : Sources [ ] ) {
128
- this . map = map ;
129
- this . sources = sources ;
169
+ export function originalPositionFor (
170
+ source : Sources ,
171
+ line : number ,
172
+ column : number ,
173
+ name : string
174
+ ) : MappingSource {
175
+ if ( ! source . map ) {
176
+ return { column, line, name, source : source . source , content : source . content } ;
130
177
}
131
178
132
- /**
133
- * originalPositionFor is only called on children SourceMapTrees. It recurses down
134
- * into its own child SourceMapTrees, until we find the original source map.
135
- */
136
- originalPositionFor ( line : number , column : number , name : string ) : MappingSource {
137
- const segment = traceSegment ( this . map , line , column ) ;
138
-
139
- // If we couldn't find a segment, then this doesn't exist in the sourcemap.
140
- if ( segment == null ) return INVALID_MAPPING ;
141
- // 1-length segments only move the current generated column, there's no source information
142
- // to gather from it.
143
- if ( segment . length === 1 ) return SOURCELESS_MAPPING ;
144
-
145
- const source = this . sources [ segment [ 1 ] ] ;
146
- return source . originalPositionFor (
147
- segment [ 2 ] ,
148
- segment [ 3 ] ,
149
- segment . length === 5 ? this . map . names [ segment [ 4 ] ] : name
150
- ) ;
151
- }
179
+ const segment = traceSegment ( source . map , line , column ) ;
180
+
181
+ // If we couldn't find a segment, then this doesn't exist in the sourcemap.
182
+ if ( segment == null ) return INVALID_MAPPING ;
183
+ // 1-length segments only move the current generated column, there's no source information
184
+ // to gather from it.
185
+ if ( segment . length === 1 ) return SOURCELESS_MAPPING ;
186
+
187
+ return originalPositionFor (
188
+ source . sources [ segment [ 1 ] ] ,
189
+ segment [ 2 ] ,
190
+ segment [ 3 ] ,
191
+ segment . length === 5 ? source . map . names [ segment [ 4 ] ] : name
192
+ ) ;
152
193
}
0 commit comments