@@ -3,7 +3,7 @@ import { assert } from 'vitest';
3
3
import { getLocator , locate } from 'locate-character' ;
4
4
import { suite , type BaseTest } from '../suite.js' ;
5
5
import { compile_directory } from '../helpers.js' ;
6
- import { decode } from '@jridgewell/sourcemap-codec ' ;
6
+ import { TraceMap , originalPositionFor } from '@jridgewell/trace-mapping ' ;
7
7
8
8
type SourceMapEntry =
9
9
| string
@@ -63,7 +63,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
63
63
const input = fs . readFileSync ( `${ cwd } /input.svelte` , 'utf-8' ) ;
64
64
65
65
function compare ( info : string , output : string , map : any , entries : SourceMapEntry [ ] ) {
66
- const output_locator = getLocator ( output ) ;
66
+ const output_locator = getLocator ( output , { offsetLine : 1 } ) ;
67
67
68
68
/** Find line/column of string in original code */
69
69
function find_original ( entry : SourceMapEntry , idx = 0 ) {
@@ -80,7 +80,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
80
80
source = input ;
81
81
}
82
82
83
- const original = locate ( source , source . indexOf ( str , idx ) ) ;
83
+ const original = locate ( source , source . indexOf ( str , idx ) , { offsetLine : 1 } ) ;
84
84
if ( ! original )
85
85
throw new Error ( `Could not find '${ str } '${ idx > 0 ? ` after index ${ idx } ` : '' } in input` ) ;
86
86
return original ;
@@ -94,7 +94,7 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
94
94
return generated ;
95
95
}
96
96
97
- const decoded = decode ( map . mappings ) ;
97
+ map = new TraceMap ( map ) ;
98
98
99
99
try {
100
100
for ( let entry of entries ) {
@@ -116,17 +116,16 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
116
116
}
117
117
118
118
// Find segment in source map pointing from generated to original
119
- const segments = decoded [ generated . line ] ;
120
- const segment = segments . find ( ( segment ) => segment [ 0 ] === generated . column ) ;
121
- if ( ! segment && entry . strGenerated !== null ) {
119
+ const result = originalPositionFor ( map , generated ) ;
120
+ if ( result . line === null && entry . strGenerated !== null ) {
122
121
throw new Error (
123
122
`Could not find segment for '${ str } ' in sourcemap (${ generated . line } :${ generated . column } )`
124
123
) ;
125
- } else if ( segment && entry . strGenerated === null ) {
124
+ } else if ( result . line !== null && entry . strGenerated === null ) {
126
125
throw new Error (
127
126
`Found segment for '${ str } ' in sourcemap (${ generated . line } :${ generated . column } ) but should not`
128
127
) ;
129
- } else if ( ! segment ) {
128
+ } else if ( result . line === null ) {
130
129
continue ;
131
130
}
132
131
@@ -140,27 +139,30 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
140
139
}
141
140
142
141
// Check that segment points to expected original
143
- assert . equal ( segment [ 2 ] , original . line , `mapped line did not match for '${ str } '` ) ;
144
- assert . equal ( segment [ 3 ] , original . column , `mapped column did not match for '${ str } '` ) ;
142
+ assert . equal ( result . line , original . line , `mapped line did not match for '${ str } '` ) ;
143
+ assert . equal ( result . column , original . column , `mapped column did not match for '${ str } '` ) ;
145
144
146
145
// Same for end of string
147
146
const generated_end = generated . column + generated_str . length ;
148
- const end_segment = segments . find ( ( segment ) => segment [ 0 ] === generated_end ) ;
149
- if ( ! end_segment )
147
+ const result_end = originalPositionFor ( map , {
148
+ line : generated . line ,
149
+ column : generated_end
150
+ } ) ;
151
+ if ( result_end . line === null )
150
152
throw new Error (
151
153
`Could not find end segment for '${ str } ' in sourcemap (${ generated . line } :${ generated_end } )`
152
154
) ;
153
155
154
- assert . equal ( end_segment [ 2 ] , original . line , `mapped line end did not match for '${ str } '` ) ;
156
+ assert . equal ( result_end . line , original . line , `mapped line end did not match for '${ str } '` ) ;
155
157
assert . equal (
156
- end_segment [ 3 ] ,
158
+ result_end . column ,
157
159
original . column + str . length ,
158
160
`mapped column end did not match for '${ str } '`
159
161
) ;
160
162
}
161
163
} catch ( e ) {
162
164
console . log ( `Source map ${ info } :\n` ) ;
163
- console . log ( decoded ) ;
165
+ console . log ( map . _decoded ) ;
164
166
throw e ;
165
167
}
166
168
}
0 commit comments