| 1 |
const { |
| 2 |
GenMapping, |
| 3 |
addSegment, |
| 4 |
addMapping, |
| 5 |
setSourceContent, |
| 6 |
toDecodedMap, |
| 7 |
toEncodedMap, |
| 8 |
allMappings, |
| 9 |
fromMap, |
| 10 |
maybeAddSegment, |
| 11 |
maybeAddMapping, |
| 12 |
} = require('..'); |
| 13 |
const assert = require('assert'); |
| 14 |
|
| 15 |
describe('GenMapping', () => { |
| 16 |
describe('toDecodedMap', () => { |
| 17 |
it('has version', () => { |
| 18 |
const map = new GenMapping({ file: 'output.js' }); |
| 19 |
|
| 20 |
assert.strictEqual(toDecodedMap(map).version, 3); |
| 21 |
}); |
| 22 |
|
| 23 |
it('has file name', () => { |
| 24 |
const map = new GenMapping({ file: 'output.js' }); |
| 25 |
|
| 26 |
assert.strictEqual(toDecodedMap(map).file, 'output.js'); |
| 27 |
}); |
| 28 |
|
| 29 |
it('has sourceRoot', () => { |
| 30 |
const map = new GenMapping({ sourceRoot: 'foo/' }); |
| 31 |
|
| 32 |
assert.strictEqual(toDecodedMap(map).sourceRoot, 'foo/'); |
| 33 |
}); |
| 34 |
|
| 35 |
it('has sources', () => { |
| 36 |
const map = new GenMapping(); |
| 37 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 38 |
|
| 39 |
assert.deepEqual(toDecodedMap(map).sources, ['input.js']); |
| 40 |
}); |
| 41 |
|
| 42 |
it('has sourcesContent', () => { |
| 43 |
const map = new GenMapping(); |
| 44 |
setSourceContent(map, 'input.js', 'input'); |
| 45 |
|
| 46 |
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']); |
| 47 |
}); |
| 48 |
|
| 49 |
it('has names', () => { |
| 50 |
const map = new GenMapping(); |
| 51 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 52 |
|
| 53 |
assert.deepEqual(toDecodedMap(map).names, ['foo']); |
| 54 |
}); |
| 55 |
|
| 56 |
it('has mappings', () => { |
| 57 |
const map = new GenMapping(); |
| 58 |
addSegment(map, 0, 1, 'input.js', 2, 3, 'foo'); |
| 59 |
|
| 60 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 2, 3, 0]]]); |
| 61 |
}); |
| 62 |
}); |
| 63 |
|
| 64 |
describe('toEncodedMap', () => { |
| 65 |
it('has version', () => { |
| 66 |
const map = new GenMapping({ file: 'output.js' }); |
| 67 |
|
| 68 |
assert.strictEqual(toEncodedMap(map).version, 3); |
| 69 |
}); |
| 70 |
|
| 71 |
it('has file name', () => { |
| 72 |
const map = new GenMapping({ file: 'output.js' }); |
| 73 |
|
| 74 |
assert.strictEqual(toEncodedMap(map).file, 'output.js'); |
| 75 |
}); |
| 76 |
|
| 77 |
it('has sourceRoot', () => { |
| 78 |
const map = new GenMapping({ sourceRoot: 'foo/' }); |
| 79 |
|
| 80 |
assert.strictEqual(toEncodedMap(map).sourceRoot, 'foo/'); |
| 81 |
}); |
| 82 |
|
| 83 |
it('has sources', () => { |
| 84 |
const map = new GenMapping(); |
| 85 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 86 |
|
| 87 |
assert.deepEqual(toEncodedMap(map).sources, ['input.js']); |
| 88 |
}); |
| 89 |
|
| 90 |
it('has sourcesContent', () => { |
| 91 |
const map = new GenMapping(); |
| 92 |
setSourceContent(map, 'input.js', 'input'); |
| 93 |
|
| 94 |
assert.deepEqual(toEncodedMap(map).sourcesContent, ['input']); |
| 95 |
}); |
| 96 |
|
| 97 |
it('has names', () => { |
| 98 |
const map = new GenMapping(); |
| 99 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 100 |
|
| 101 |
assert.deepEqual(toEncodedMap(map).names, ['foo']); |
| 102 |
}); |
| 103 |
|
| 104 |
it('has mappings', () => { |
| 105 |
const map = new GenMapping(); |
| 106 |
addSegment(map, 0, 1, 'input.js', 2, 3, 'foo'); |
| 107 |
|
| 108 |
assert.deepEqual(toEncodedMap(map).mappings, 'CAEGA'); |
| 109 |
}); |
| 110 |
}); |
| 111 |
|
| 112 |
describe('addSegment', () => { |
| 113 |
it('requires generated line/column', () => { |
| 114 |
const map = new GenMapping(); |
| 115 |
addSegment(map, 0, 0); |
| 116 |
|
| 117 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0]]]); |
| 118 |
}); |
| 119 |
|
| 120 |
it('records nameless source segment', () => { |
| 121 |
const map = new GenMapping(); |
| 122 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 123 |
|
| 124 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0]]]); |
| 125 |
}); |
| 126 |
|
| 127 |
it('records named source segment', () => { |
| 128 |
const map = new GenMapping(); |
| 129 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 130 |
|
| 131 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0, 0]]]); |
| 132 |
}); |
| 133 |
|
| 134 |
it('uses 0-based line', () => { |
| 135 |
const map = new GenMapping(); |
| 136 |
addSegment(map, 1, 0, 'input.js', 1, 0, 'foo'); |
| 137 |
|
| 138 |
assert.deepEqual(toDecodedMap(map).mappings, [[], [[0, 0, 1, 0, 0]]]); |
| 139 |
}); |
| 140 |
|
| 141 |
it('can skip back and forth in generated lines', () => { |
| 142 |
const map = new GenMapping(); |
| 143 |
addSegment(map, 1, 0, 'input.js', 1, 0); |
| 144 |
addSegment(map, 2, 0, 'input.js', 2, 0); |
| 145 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 146 |
|
| 147 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 148 |
[[0, 0, 0, 0]], |
| 149 |
[[0, 0, 1, 0]], |
| 150 |
[[0, 0, 2, 0]], |
| 151 |
]); |
| 152 |
}); |
| 153 |
|
| 154 |
it('sorts generated column', () => { |
| 155 |
const map = new GenMapping(); |
| 156 |
addSegment(map, 0, 1, 'input.js', 0, 0); |
| 157 |
addSegment(map, 0, 2, 'input.js', 0, 0); |
| 158 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 159 |
|
| 160 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 161 |
[ |
| 162 |
[0, 0, 0, 0], |
| 163 |
[1, 0, 0, 0], |
| 164 |
[2, 0, 0, 0], |
| 165 |
], |
| 166 |
]); |
| 167 |
}); |
| 168 |
|
| 169 |
it('postfix sorts source index', () => { |
| 170 |
const map = new GenMapping(); |
| 171 |
|
| 172 |
addSegment(map, 1, 0, 'foo.js', 0, 0); |
| 173 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 174 |
addSegment(map, 0, 0, 'foo.js', 0, 0); |
| 175 |
|
| 176 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 177 |
[ |
| 178 |
[0, 1, 0, 0], |
| 179 |
[0, 0, 0, 0], |
| 180 |
], |
| 181 |
[[0, 0, 0, 0]], |
| 182 |
]); |
| 183 |
}); |
| 184 |
|
| 185 |
it('postfix sorts source line', () => { |
| 186 |
const map = new GenMapping(); |
| 187 |
|
| 188 |
addSegment(map, 0, 0, 'input.js', 1, 0); |
| 189 |
addSegment(map, 0, 0, 'input.js', 2, 0); |
| 190 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 191 |
|
| 192 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 193 |
[ |
| 194 |
[0, 0, 1, 0], |
| 195 |
[0, 0, 2, 0], |
| 196 |
[0, 0, 0, 0], |
| 197 |
], |
| 198 |
]); |
| 199 |
}); |
| 200 |
|
| 201 |
it('postfix sorts source column', () => { |
| 202 |
const map = new GenMapping(); |
| 203 |
|
| 204 |
addSegment(map, 0, 0, 'input.js', 0, 1); |
| 205 |
addSegment(map, 0, 0, 'input.js', 0, 2); |
| 206 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 207 |
|
| 208 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 209 |
[ |
| 210 |
[0, 0, 0, 1], |
| 211 |
[0, 0, 0, 2], |
| 212 |
[0, 0, 0, 0], |
| 213 |
], |
| 214 |
]); |
| 215 |
}); |
| 216 |
|
| 217 |
it('postfix sorts name index', () => { |
| 218 |
const map = new GenMapping(); |
| 219 |
|
| 220 |
addSegment(map, 1, 0, 'input.js', 0, 0, 'foo'); |
| 221 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'bar'); |
| 222 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 223 |
|
| 224 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 225 |
[ |
| 226 |
[0, 0, 0, 0, 1], |
| 227 |
[0, 0, 0, 0, 0], |
| 228 |
], |
| 229 |
[[0, 0, 0, 0, 0]], |
| 230 |
]); |
| 231 |
}); |
| 232 |
|
| 233 |
it('postfix sorts sourceless segment after source segment', () => { |
| 234 |
const map = new GenMapping(); |
| 235 |
|
| 236 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 237 |
addSegment(map, 0, 0); |
| 238 |
|
| 239 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [0]]]); |
| 240 |
}); |
| 241 |
|
| 242 |
it('postfix sorts sourceless segment after named segment', () => { |
| 243 |
const map = new GenMapping(); |
| 244 |
|
| 245 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 246 |
addSegment(map, 0, 0); |
| 247 |
|
| 248 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0, 0], [0]]]); |
| 249 |
}); |
| 250 |
|
| 251 |
it('postfix sorts source segment after named segment', () => { |
| 252 |
const map = new GenMapping(); |
| 253 |
|
| 254 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 255 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 256 |
|
| 257 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 258 |
[ |
| 259 |
[0, 0, 0, 0, 0], |
| 260 |
[0, 0, 0, 0], |
| 261 |
], |
| 262 |
]); |
| 263 |
}); |
| 264 |
|
| 265 |
it('keeps equivalent sourceless segment', () => { |
| 266 |
const map = new GenMapping(); |
| 267 |
|
| 268 |
addSegment(map, 0, 0); |
| 269 |
addSegment(map, 1, 0); |
| 270 |
addSegment(map, 0, 0); |
| 271 |
|
| 272 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0], [0]], [[0]]]); |
| 273 |
}); |
| 274 |
|
| 275 |
it('keeps equivalent source segment', () => { |
| 276 |
const map = new GenMapping(); |
| 277 |
|
| 278 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 279 |
addSegment(map, 1, 0, 'input.js', 0, 0); |
| 280 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 281 |
|
| 282 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 283 |
[ |
| 284 |
[0, 0, 0, 0], |
| 285 |
[0, 0, 0, 0], |
| 286 |
], |
| 287 |
[[0, 0, 0, 0]], |
| 288 |
]); |
| 289 |
}); |
| 290 |
|
| 291 |
it('keeps equivalent named segment', () => { |
| 292 |
const map = new GenMapping(); |
| 293 |
|
| 294 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 295 |
addSegment(map, 1, 0, 'input.js', 0, 0, 'foo'); |
| 296 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 297 |
|
| 298 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 299 |
[ |
| 300 |
[0, 0, 0, 0, 0], |
| 301 |
[0, 0, 0, 0, 0], |
| 302 |
], |
| 303 |
[[0, 0, 0, 0, 0]], |
| 304 |
]); |
| 305 |
}); |
| 306 |
|
| 307 |
it('keeps sourcesContent in sync with sources', () => { |
| 308 |
const map = new GenMapping(); |
| 309 |
|
| 310 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 311 |
addSegment(map, 1, 0, 'foo.js', 0, 0); |
| 312 |
addSegment(map, 2, 0, 'bar.js', 0, 0); |
| 313 |
|
| 314 |
setSourceContent(map, 'bar.js', 'bar'); |
| 315 |
setSourceContent(map, 'input.js', 'input'); |
| 316 |
|
| 317 |
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input', null, 'bar']); |
| 318 |
}); |
| 319 |
|
| 320 |
it('can set content when adding', () => { |
| 321 |
const map = new GenMapping(); |
| 322 |
|
| 323 |
addSegment(map, 0, 0, 'input.js', 0, 0, '', 'input'); |
| 324 |
addSegment(map, 1, 0, 'foo.js', 0, 0, '', undefined); |
| 325 |
|
| 326 |
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input', null]); |
| 327 |
}); |
| 328 |
|
| 329 |
it('only sets content during initial source add', () => { |
| 330 |
const map = new GenMapping(); |
| 331 |
|
| 332 |
addSegment(map, 0, 0, 'input.js', 0, 0, '', 'first'); |
| 333 |
addSegment(map, 1, 0, 'input.js', 0, 0, '', 'second'); |
| 334 |
|
| 335 |
assert.deepEqual(toDecodedMap(map).sourcesContent, ['first']); |
| 336 |
}); |
| 337 |
|
| 338 |
it('ignores content when sourceless', () => { |
| 339 |
const map = new GenMapping(); |
| 340 |
|
| 341 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 342 |
addSegment(map, 0, 1, '', 0, 0, '', 'foo'); |
| 343 |
|
| 344 |
assert.deepEqual(toDecodedMap(map).sourcesContent, [null]); |
| 345 |
}); |
| 346 |
}); |
| 347 |
|
| 348 |
describe('addMapping', () => { |
| 349 |
it('requires generated line/column', () => { |
| 350 |
const map = new GenMapping(); |
| 351 |
addMapping(map, { generated: { line: 1, column: 0 } }); |
| 352 |
|
| 353 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0]]]); |
| 354 |
}); |
| 355 |
|
| 356 |
it('records nameless source segment', () => { |
| 357 |
const map = new GenMapping(); |
| 358 |
addMapping(map, { |
| 359 |
generated: { line: 1, column: 0 }, |
| 360 |
source: 'input.js', |
| 361 |
original: { line: 1, column: 0 }, |
| 362 |
}); |
| 363 |
|
| 364 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0]]]); |
| 365 |
}); |
| 366 |
|
| 367 |
it('records named source segment', () => { |
| 368 |
const map = new GenMapping(); |
| 369 |
addMapping(map, { |
| 370 |
generated: { line: 1, column: 0 }, |
| 371 |
source: 'input.js', |
| 372 |
original: { line: 1, column: 0 }, |
| 373 |
name: 'foo', |
| 374 |
}); |
| 375 |
|
| 376 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0, 0]]]); |
| 377 |
}); |
| 378 |
|
| 379 |
it('uses 1-based line', () => { |
| 380 |
const map = new GenMapping(); |
| 381 |
addMapping(map, { |
| 382 |
generated: { line: 2, column: 0 }, |
| 383 |
source: 'input.js', |
| 384 |
original: { line: 2, column: 0 }, |
| 385 |
name: 'foo', |
| 386 |
}); |
| 387 |
|
| 388 |
assert.deepEqual(toDecodedMap(map).mappings, [[], [[0, 0, 1, 0, 0]]]); |
| 389 |
}); |
| 390 |
}); |
| 391 |
|
| 392 |
describe('allMappings', () => { |
| 393 |
it('returns sourceless mappings', () => { |
| 394 |
const map = new GenMapping(); |
| 395 |
|
| 396 |
addSegment(map, 0, 0); |
| 397 |
|
| 398 |
assert.deepEqual(allMappings(map), [ |
| 399 |
{ |
| 400 |
generated: { line: 1, column: 0 }, |
| 401 |
source: undefined, |
| 402 |
original: undefined, |
| 403 |
name: undefined, |
| 404 |
}, |
| 405 |
]); |
| 406 |
}); |
| 407 |
|
| 408 |
it('returns source mappings', () => { |
| 409 |
const map = new GenMapping(); |
| 410 |
|
| 411 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 412 |
|
| 413 |
assert.deepEqual(allMappings(map), [ |
| 414 |
{ |
| 415 |
generated: { line: 1, column: 0 }, |
| 416 |
source: 'input.js', |
| 417 |
original: { line: 1, column: 0 }, |
| 418 |
name: undefined, |
| 419 |
}, |
| 420 |
]); |
| 421 |
}); |
| 422 |
|
| 423 |
it('returns named mappings', () => { |
| 424 |
const map = new GenMapping(); |
| 425 |
|
| 426 |
addSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 427 |
|
| 428 |
assert.deepEqual(allMappings(map), [ |
| 429 |
{ |
| 430 |
generated: { line: 1, column: 0 }, |
| 431 |
source: 'input.js', |
| 432 |
original: { line: 1, column: 0 }, |
| 433 |
name: 'foo', |
| 434 |
}, |
| 435 |
]); |
| 436 |
}); |
| 437 |
|
| 438 |
it('returns mappings in sorted order', () => { |
| 439 |
const map = new GenMapping(); |
| 440 |
|
| 441 |
addSegment(map, 0, 1, 'input.js', 0, 0); |
| 442 |
addSegment(map, 0, 2, 'input.js', 0, 0); |
| 443 |
addSegment(map, 0, 0, 'input.js', 0, 0); |
| 444 |
|
| 445 |
assert.deepEqual(allMappings(map), [ |
| 446 |
{ |
| 447 |
generated: { line: 1, column: 0 }, |
| 448 |
source: 'input.js', |
| 449 |
original: { line: 1, column: 0 }, |
| 450 |
name: undefined, |
| 451 |
}, |
| 452 |
{ |
| 453 |
generated: { line: 1, column: 1 }, |
| 454 |
source: 'input.js', |
| 455 |
original: { line: 1, column: 0 }, |
| 456 |
name: undefined, |
| 457 |
}, |
| 458 |
{ |
| 459 |
generated: { line: 1, column: 2 }, |
| 460 |
source: 'input.js', |
| 461 |
original: { line: 1, column: 0 }, |
| 462 |
name: undefined, |
| 463 |
}, |
| 464 |
]); |
| 465 |
}); |
| 466 |
}); |
| 467 |
|
| 468 |
describe('fromMap', () => { |
| 469 |
it('copies version', () => { |
| 470 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 471 |
version: 3, |
| 472 |
names: [], |
| 473 |
sources: ['input.js'], |
| 474 |
sourcesContent: [], |
| 475 |
mappings: [], |
| 476 |
}); |
| 477 |
const map = fromMap(input); |
| 478 |
|
| 479 |
assert.strictEqual(toDecodedMap(map).version, 3); |
| 480 |
}); |
| 481 |
|
| 482 |
it('copies file name', () => { |
| 483 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 484 |
version: 3, |
| 485 |
file: 'output.js', |
| 486 |
names: [], |
| 487 |
sources: ['input.js'], |
| 488 |
sourcesContent: [], |
| 489 |
mappings: [], |
| 490 |
}); |
| 491 |
const map = fromMap(input); |
| 492 |
|
| 493 |
assert.strictEqual(toDecodedMap(map).file, 'output.js'); |
| 494 |
}); |
| 495 |
|
| 496 |
it('copies sourceRoot', () => { |
| 497 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 498 |
version: 3, |
| 499 |
names: [], |
| 500 |
sourceRoot: 'foo/', |
| 501 |
sources: ['input.js'], |
| 502 |
sourcesContent: [], |
| 503 |
mappings: [], |
| 504 |
}); |
| 505 |
const map = fromMap(input); |
| 506 |
|
| 507 |
assert.strictEqual(toDecodedMap(map).sourceRoot, 'foo/'); |
| 508 |
}); |
| 509 |
|
| 510 |
it('copies sources', () => { |
| 511 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 512 |
version: 3, |
| 513 |
names: [], |
| 514 |
sources: ['input.js'], |
| 515 |
sourcesContent: [], |
| 516 |
mappings: [], |
| 517 |
}); |
| 518 |
const map = fromMap(input); |
| 519 |
|
| 520 |
assert.deepEqual(toDecodedMap(map).sources, ['input.js']); |
| 521 |
}); |
| 522 |
|
| 523 |
it('copies sourcesContent', () => { |
| 524 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 525 |
version: 3, |
| 526 |
names: [], |
| 527 |
sources: ['input.js'], |
| 528 |
sourcesContent: ['input'], |
| 529 |
mappings: [], |
| 530 |
}); |
| 531 |
const map = fromMap(input); |
| 532 |
|
| 533 |
assert.deepEqual(toDecodedMap(map).sourcesContent, ['input']); |
| 534 |
}); |
| 535 |
|
| 536 |
it('creates sourcesContent', () => { |
| 537 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 538 |
version: 3, |
| 539 |
names: [], |
| 540 |
sources: ['input.js'], |
| 541 |
mappings: [], |
| 542 |
}); |
| 543 |
const map = fromMap(input); |
| 544 |
|
| 545 |
assert.deepEqual(toDecodedMap(map).sourcesContent, [null]); |
| 546 |
}); |
| 547 |
|
| 548 |
it('copies names', () => { |
| 549 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 550 |
version: 3, |
| 551 |
names: ['foo'], |
| 552 |
sources: ['input.js'], |
| 553 |
sourcesContent: [], |
| 554 |
mappings: [[[0, 0, 0, 0, 0]]], |
| 555 |
}); |
| 556 |
const map = fromMap(input); |
| 557 |
|
| 558 |
assert.deepEqual(toDecodedMap(map).names, ['foo']); |
| 559 |
}); |
| 560 |
|
| 561 |
it('copies decoded mappings', () => { |
| 562 |
const input = /** @type {import('@jridgewell/trace-mapping').DecodedSourceMap} */ ({ |
| 563 |
version: 3, |
| 564 |
names: [], |
| 565 |
sources: ['input.js'], |
| 566 |
sourcesContent: [], |
| 567 |
mappings: [[[1, 0, 2, 3, 0]]], |
| 568 |
}); |
| 569 |
const map = fromMap(input); |
| 570 |
|
| 571 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 2, 3, 0]]]); |
| 572 |
}); |
| 573 |
|
| 574 |
it('copies encoded mappings', () => { |
| 575 |
const input = /** @type {import('@jridgewell/trace-mapping').EncodedSourceMap} */ ({ |
| 576 |
version: 3, |
| 577 |
names: [], |
| 578 |
sources: ['input.js'], |
| 579 |
sourcesContent: [], |
| 580 |
mappings: 'CAEGA', |
| 581 |
}); |
| 582 |
const map = fromMap(input); |
| 583 |
|
| 584 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 2, 3, 0]]]); |
| 585 |
}); |
| 586 |
}); |
| 587 |
|
| 588 |
describe('maybeAddSegment', () => { |
| 589 |
describe('sourceless segment added afterwards', () => { |
| 590 |
it('skips sourceless segment first on line', () => { |
| 591 |
const map = new GenMapping(); |
| 592 |
|
| 593 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0); |
| 594 |
maybeAddSegment(map, 1, 1); |
| 595 |
|
| 596 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 0, 0]]]); |
| 597 |
}); |
| 598 |
|
| 599 |
it('skips sourceless segment sorted first in line', () => { |
| 600 |
const map = new GenMapping(); |
| 601 |
|
| 602 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0); |
| 603 |
maybeAddSegment(map, 0, 0); |
| 604 |
|
| 605 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 0, 0]]]); |
| 606 |
}); |
| 607 |
|
| 608 |
it('skips equivalent sourceless segment', () => { |
| 609 |
const map = new GenMapping(); |
| 610 |
|
| 611 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 612 |
maybeAddSegment(map, 0, 1); |
| 613 |
maybeAddSegment(map, 0, 1); |
| 614 |
|
| 615 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [1]]]); |
| 616 |
}); |
| 617 |
|
| 618 |
it('skips runs of sourceless segment', () => { |
| 619 |
const map = new GenMapping(); |
| 620 |
|
| 621 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 622 |
maybeAddSegment(map, 0, 1); |
| 623 |
maybeAddSegment(map, 0, 2); |
| 624 |
|
| 625 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [1]]]); |
| 626 |
}); |
| 627 |
|
| 628 |
it('does not skip sourcless segment sorted before sourceless segment', () => { |
| 629 |
const map = new GenMapping(); |
| 630 |
|
| 631 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 632 |
maybeAddSegment(map, 0, 2); |
| 633 |
maybeAddSegment(map, 0, 1); |
| 634 |
|
| 635 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [1], [2]]]); |
| 636 |
}); |
| 637 |
|
| 638 |
it('does not skip sourcless segment matching source segment', () => { |
| 639 |
const map = new GenMapping(); |
| 640 |
|
| 641 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 642 |
maybeAddSegment(map, 0, 0); |
| 643 |
|
| 644 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [0]]]); |
| 645 |
}); |
| 646 |
}); |
| 647 |
|
| 648 |
describe('source segment added afterwards', () => { |
| 649 |
it('skips equivalent source segment', () => { |
| 650 |
const map = new GenMapping(); |
| 651 |
|
| 652 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 653 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 654 |
|
| 655 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0]]]); |
| 656 |
}); |
| 657 |
|
| 658 |
it('keeps source segment after matching named segment', () => { |
| 659 |
const map = new GenMapping(); |
| 660 |
|
| 661 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 662 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 663 |
|
| 664 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 665 |
[ |
| 666 |
[0, 0, 0, 0, 0], |
| 667 |
[0, 0, 0, 0], |
| 668 |
], |
| 669 |
]); |
| 670 |
}); |
| 671 |
|
| 672 |
it('keeps runs of source segment after matching named segment', () => { |
| 673 |
const map = new GenMapping(); |
| 674 |
|
| 675 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 676 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0); |
| 677 |
|
| 678 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 679 |
[ |
| 680 |
[0, 0, 0, 0, 0], |
| 681 |
[1, 0, 0, 0], |
| 682 |
], |
| 683 |
]); |
| 684 |
}); |
| 685 |
|
| 686 |
it('keeps named segment after matching source segment', () => { |
| 687 |
const map = new GenMapping(); |
| 688 |
|
| 689 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 690 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 691 |
|
| 692 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 693 |
[ |
| 694 |
[0, 0, 0, 0], |
| 695 |
[0, 0, 0, 0, 0], |
| 696 |
], |
| 697 |
]); |
| 698 |
}); |
| 699 |
|
| 700 |
it('keeps runs of named segment after matching source segment', () => { |
| 701 |
const map = new GenMapping(); |
| 702 |
|
| 703 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 704 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0, 'foo'); |
| 705 |
|
| 706 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 707 |
[ |
| 708 |
[0, 0, 0, 0], |
| 709 |
[1, 0, 0, 0, 0], |
| 710 |
], |
| 711 |
]); |
| 712 |
}); |
| 713 |
|
| 714 |
it('skips runs of matching source segment', () => { |
| 715 |
const map = new GenMapping(); |
| 716 |
|
| 717 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 718 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0); |
| 719 |
|
| 720 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0]]]); |
| 721 |
}); |
| 722 |
|
| 723 |
it('skips runs of matching named segment', () => { |
| 724 |
const map = new GenMapping(); |
| 725 |
|
| 726 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0, 'foo'); |
| 727 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0, 'foo'); |
| 728 |
|
| 729 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0, 0]]]); |
| 730 |
}); |
| 731 |
|
| 732 |
it('keeps source segment pointing to different source file', () => { |
| 733 |
const map = new GenMapping(); |
| 734 |
|
| 735 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 736 |
maybeAddSegment(map, 0, 0, 'foo.js', 0, 0); |
| 737 |
|
| 738 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 739 |
[ |
| 740 |
[0, 0, 0, 0], |
| 741 |
[0, 1, 0, 0], |
| 742 |
], |
| 743 |
]); |
| 744 |
}); |
| 745 |
|
| 746 |
it('keeps source segment pointing to different source line', () => { |
| 747 |
const map = new GenMapping(); |
| 748 |
|
| 749 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 750 |
maybeAddSegment(map, 0, 0, 'input.js', 1, 0); |
| 751 |
|
| 752 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 753 |
[ |
| 754 |
[0, 0, 0, 0], |
| 755 |
[0, 0, 1, 0], |
| 756 |
], |
| 757 |
]); |
| 758 |
}); |
| 759 |
|
| 760 |
it('keeps source segment pointing to different source column', () => { |
| 761 |
const map = new GenMapping(); |
| 762 |
|
| 763 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 764 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 1); |
| 765 |
|
| 766 |
assert.deepEqual(toDecodedMap(map).mappings, [ |
| 767 |
[ |
| 768 |
[0, 0, 0, 0], |
| 769 |
[0, 0, 0, 1], |
| 770 |
], |
| 771 |
]); |
| 772 |
}); |
| 773 |
|
| 774 |
it('keeps source segment after matching sourceless segment', () => { |
| 775 |
const map = new GenMapping(); |
| 776 |
|
| 777 |
maybeAddSegment(map, 0, 0, 'input.js', 0, 0); |
| 778 |
maybeAddSegment(map, 0, 1); |
| 779 |
maybeAddSegment(map, 0, 1, 'input.js', 0, 0); |
| 780 |
|
| 781 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [1], [1, 0, 0, 0]]]); |
| 782 |
}); |
| 783 |
}); |
| 784 |
}); |
| 785 |
|
| 786 |
describe('maybeAddMapping', () => { |
| 787 |
describe('sourceless segment added afterwards', () => { |
| 788 |
it('skips sourceless segment first on line', () => { |
| 789 |
const map = new GenMapping(); |
| 790 |
|
| 791 |
maybeAddMapping(map, { |
| 792 |
generated: { line: 1, column: 1 }, |
| 793 |
source: 'input.js', |
| 794 |
original: { |
| 795 |
line: 1, |
| 796 |
column: 0, |
| 797 |
}, |
| 798 |
}); |
| 799 |
maybeAddMapping(map, { generated: { line: 2, column: 1 } }); |
| 800 |
|
| 801 |
assert.deepEqual(toDecodedMap(map).mappings, [[[1, 0, 0, 0]]]); |
| 802 |
}); |
| 803 |
|
| 804 |
it('skips equivalent sourceless segment', () => { |
| 805 |
const map = new GenMapping(); |
| 806 |
|
| 807 |
maybeAddMapping(map, { |
| 808 |
generated: { line: 1, column: 0 }, |
| 809 |
source: 'input.js', |
| 810 |
original: { |
| 811 |
line: 1, |
| 812 |
column: 0, |
| 813 |
}, |
| 814 |
}); |
| 815 |
maybeAddMapping(map, { generated: { line: 1, column: 1 } }); |
| 816 |
maybeAddMapping(map, { generated: { line: 1, column: 1 } }); |
| 817 |
|
| 818 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0], [1]]]); |
| 819 |
}); |
| 820 |
}); |
| 821 |
|
| 822 |
describe('source segment added afterwards', () => { |
| 823 |
it('skips equivalent source segment', () => { |
| 824 |
const map = new GenMapping(); |
| 825 |
|
| 826 |
maybeAddMapping(map, { |
| 827 |
generated: { line: 1, column: 0 }, |
| 828 |
source: 'input.js', |
| 829 |
original: { |
| 830 |
line: 1, |
| 831 |
column: 0, |
| 832 |
}, |
| 833 |
}); |
| 834 |
maybeAddMapping(map, { |
| 835 |
generated: { line: 1, column: 0 }, |
| 836 |
source: 'input.js', |
| 837 |
original: { |
| 838 |
line: 1, |
| 839 |
column: 0, |
| 840 |
}, |
| 841 |
}); |
| 842 |
|
| 843 |
assert.deepEqual(toDecodedMap(map).mappings, [[[0, 0, 0, 0]]]); |
| 844 |
}); |
| 845 |
}); |
| 846 |
}); |
| 847 |
}); |