@@ -2,20 +2,65 @@ import { FastStringArray, put } from './fast-string-array';
22import { presortedDecodedMap , traceSegment , decodedMappings } from '@jridgewell/trace-mapping' ;
33
44import type { TraceMap } from '@jridgewell/trace-mapping' ;
5- import type OriginalSource from './original-source' ;
65import type { SourceMapSegment , SourceMapSegmentObject } from './types' ;
76
8- type Sources = OriginalSource | SourceMapTree ;
9-
107const INVALID_MAPPING = undefined ;
118const SOURCELESS_MAPPING = null ;
9+ const EMPTY_SOURCES : Sources [ ] = [ ] ;
10+
1211type MappingSource = SourceMapSegmentObject | typeof INVALID_MAPPING | typeof SOURCELESS_MAPPING ;
1312
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+
1459/**
1560 * traceMappings is only called on the root level SourceMapTree, and begins the process of
1661 * resolving each mapping in terms of the original source files.
1762 */
18- export function traceMappings ( tree : SourceMapTree ) : TraceMap {
63+ export function traceMappings ( tree : Sources ) : TraceMap {
1964 const mappings : SourceMapSegment [ ] [ ] = [ ] ;
2065 const names = FastStringArray ( ) ;
2166 const sources = FastStringArray ( ) ;
@@ -41,7 +86,8 @@ export function traceMappings(tree: SourceMapTree): TraceMap {
4186 // to gather from it.
4287 if ( segment . length !== 1 ) {
4388 const source = rootSources [ segment [ 1 ] ] ;
44- traced = source . originalPositionFor (
89+ traced = originalPositionFor (
90+ source ,
4591 segment [ 2 ] ,
4692 segment [ 3 ] ,
4793 segment . length === 5 ? rootNames [ segment [ 4 ] ] : ''
@@ -117,36 +163,31 @@ export function traceMappings(tree: SourceMapTree): TraceMap {
117163}
118164
119165/**
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 .
122168 */
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 } ;
130177 }
131178
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+ ) ;
152193}
0 commit comments