@@ -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 { LEAST_UPPER_BOUND , TraceMap , originalPositionFor } from '@jridgewell/trace-mapping ' ;
6
+ import { decode } from '@jridgewell/sourcemap-codec ' ;
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 , { offsetLine : 1 } ) ;
66
+ const output_locator = getLocator ( output ) ;
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 ) , { offsetLine : 1 } ) ;
83
+ const original = locate ( source , source . indexOf ( str , idx ) ) ;
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
- map = new TraceMap ( map ) ;
97
+ const decoded = decode ( map . mappings ) ;
98
98
99
99
try {
100
100
for ( let entry of entries ) {
@@ -116,16 +116,17 @@ 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 result = originalPositionFor ( map , generated ) ;
120
- if ( result . line === null && entry . strGenerated !== null ) {
119
+ const segments = decoded [ generated . line ] ;
120
+ const segment = segments . find ( ( segment ) => segment [ 0 ] === generated . column ) ;
121
+ if ( ! segment && entry . strGenerated !== null ) {
121
122
throw new Error (
122
123
`Could not find segment for '${ str } ' in sourcemap (${ generated . line } :${ generated . column } )`
123
124
) ;
124
- } else if ( result . line !== null && entry . strGenerated === null ) {
125
+ } else if ( segment && entry . strGenerated === null ) {
125
126
throw new Error (
126
127
`Found segment for '${ str } ' in sourcemap (${ generated . line } :${ generated . column } ) but should not`
127
128
) ;
128
- } else if ( result . line === null ) {
129
+ } else if ( ! segment ) {
129
130
continue ;
130
131
}
131
132
@@ -139,31 +140,37 @@ const { test, run } = suite<SourcemapTest>(async (config, cwd) => {
139
140
}
140
141
141
142
// Check that segment points to expected original
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 } '` ) ;
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 } '` ) ;
144
145
145
146
// Same for end of string
146
147
const generated_end = generated . column + generated_str . length ;
147
- const result_end = originalPositionFor ( map , {
148
- bias : LEAST_UPPER_BOUND ,
149
- line : generated . line ,
150
- column : generated_end
151
- } ) ;
152
- if ( result_end . line === null )
153
- throw new Error (
154
- `Could not find end segment for '${ str } ' in sourcemap (${ generated . line } :${ generated_end } )`
155
- ) ;
148
+ const end_segment = segments . find ( ( segment ) => segment [ 0 ] === generated_end ) ;
149
+ if ( ! end_segment ) {
150
+ // If the string is the last segment and it's the end of the line,
151
+ // it's okay if there's no end segment (source maps save space by omitting it in that case)
152
+ if (
153
+ segments . indexOf ( segment ) !== segments . length - 1 ||
154
+ / [ \r \n ] / . test ( output [ generated . character ] )
155
+ ) {
156
+ throw new Error (
157
+ `Could not find end segment for '${ str } ' in sourcemap (${ generated . line } :${ generated_end } )`
158
+ ) ;
159
+ } else {
160
+ continue ;
161
+ }
162
+ }
156
163
157
- assert . equal ( result_end . line , original . line , `mapped line end did not match for '${ str } '` ) ;
164
+ assert . equal ( end_segment [ 2 ] , original . line , `mapped line end did not match for '${ str } '` ) ;
158
165
assert . equal (
159
- result_end . column ,
166
+ end_segment [ 3 ] ,
160
167
original . column + str . length ,
161
168
`mapped column end did not match for '${ str } '`
162
169
) ;
163
170
}
164
171
} catch ( e ) {
165
172
console . log ( `Source map ${ info } :\n` ) ;
166
- console . log ( map . _decoded ) ;
173
+ console . log ( decoded ) ;
167
174
throw e ;
168
175
}
169
176
}
0 commit comments