1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
|
Description: upstream switched to vitest which is not available
Author: Yadd <yadd@debian.org>
Forwarded: not-needed
Last-Update: 2025-08-24
--- a/test/Bundle.test.js
+++ b/test/Bundle.test.js
@@ -1,23 +1,24 @@
-import { describe, it } from 'vitest';
-import assert from 'assert';
-import MagicString, { Bundle } from '../';
-import { SourceMapConsumer } from 'source-map-js';
+const assert = require('assert');
+const SourceMapConsumer = require('source-map-js').SourceMapConsumer;
+const MagicString = require('../');
-describe('Bundle', () => {
+require('source-map-support').install();
+
+describe('MagicString.Bundle', () => {
describe('addSource', () => {
it('should return this', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const source = new MagicString('abcdefghijkl');
assert.strictEqual(b.addSource({ content: source }), b);
});
it('should accept MagicString instance as a single argument', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const array = [];
const source = new MagicString('abcdefghijkl', {
filename: 'foo.js',
- indentExclusionRanges: array,
+ indentExclusionRanges: array
});
b.addSource(source);
@@ -27,12 +28,12 @@
});
it('should accept ignore-list hint', () => {
- const b = new Bundle();
- const foo = new MagicString('foo', { filename: 'foo.js' });
- const bar = new MagicString('bar', { filename: 'bar.js' });
+ const b = new MagicString.Bundle();
+ const foo = new MagicString('foo', {filename: 'foo.js'});
+ const bar = new MagicString('bar', {filename: 'bar.js'});
- b.addSource({ content: foo, ignoreList: true });
- b.addSource({ content: bar, ignoreList: false });
+ b.addSource({content: foo, ignoreList: true});
+ b.addSource({content: bar, ignoreList: false});
assert.strictEqual(b.sources[0].content, foo);
assert.strictEqual(b.sources[0].ignoreList, true);
assert.strictEqual(b.sources[1].content, bar);
@@ -40,12 +41,12 @@
});
it('respects MagicString init options with { content: source }', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const array = [];
const source = new MagicString('abcdefghijkl', {
filename: 'foo.js',
ignoreList: false,
- indentExclusionRanges: array,
+ indentExclusionRanges: array
});
b.addSource({ content: source });
@@ -58,7 +59,7 @@
describe('append', () => {
it('should append content', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({ content: new MagicString('*') });
@@ -67,7 +68,7 @@
});
it('should append content before subsequent sources', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource(new MagicString('*'));
@@ -76,7 +77,7 @@
});
it('should return this', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
assert.strictEqual(b.append('x'), b);
});
});
@@ -85,7 +86,7 @@
it('should clone a bundle', () => {
const s1 = new MagicString('abcdef');
const s2 = new MagicString('ghijkl');
- const b = new Bundle()
+ const b = new MagicString.Bundle()
.addSource({ content: s1 })
.addSource({ content: s2 })
.prepend('>>>')
@@ -102,20 +103,21 @@
describe('generateMap', () => {
it('should generate a sourcemap', () => {
- const b = new Bundle()
+ const b = new MagicString.Bundle()
.addSource({
filename: 'foo.js',
- content: new MagicString('var answer = 42;'),
+ content: new MagicString('var answer = 42;')
})
.addSource({
filename: 'bar.js',
- content: new MagicString('console.log( answer );'),
+ content: new MagicString('console.log( answer );')
});
+
const map = b.generateMap({
file: 'bundle.js',
includeContent: true,
- hires: true,
+ hires: true
});
assert.equal(map.version, 3);
@@ -148,20 +150,20 @@
});
it('should handle Windows-style paths', () => {
- const b = new Bundle()
+ const b = new MagicString.Bundle()
.addSource({
filename: 'path\\to\\foo.js',
- content: new MagicString('var answer = 42;'),
+ content: new MagicString('var answer = 42;')
})
.addSource({
filename: 'path\\to\\bar.js',
- content: new MagicString('console.log( answer );'),
+ content: new MagicString('console.log( answer );')
});
const map = b.generateMap({
file: 'bundle.js',
includeContent: true,
- hires: true,
+ hires: true
});
assert.equal(map.version, 3);
@@ -169,10 +171,7 @@
assert.deepEqual(map.sources, ['path/to/foo.js', 'path/to/bar.js']);
assert.deepEqual(map.sourcesContent, ['var answer = 42;', 'console.log( answer );']);
- assert.equal(
- map.toString(),
- '{"version":3,"file":"bundle.js","sources":["path/to/foo.js","path/to/bar.js"],"sourcesContent":["var answer = 42;","console.log( answer );"],"names":[],"mappings":"AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;ACAf,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}',
- );
+ assert.equal(map.toString(), '{"version":3,"file":"bundle.js","sources":["path/to/foo.js","path/to/bar.js"],"sourcesContent":["var answer = 42;","console.log( answer );"],"names":[],"mappings":"AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;ACAf,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC"}');
const smc = new SourceMapConsumer(map);
let loc;
@@ -199,23 +198,21 @@
});
it('should handle edge case with intro content', () => {
- const b = new Bundle()
+ const b = new MagicString.Bundle()
.addSource({
filename: 'foo.js',
- content: new MagicString('var answer = 42;'),
+ content: new MagicString('var answer = 42;')
})
.addSource({
filename: 'bar.js',
- content: new MagicString('\nconsole.log( answer );'),
+ content: new MagicString('\nconsole.log( answer );')
})
- .indent()
- .prepend('(function () {\n')
- .append('\n}());');
+ .indent().prepend('(function () {\n').append('\n}());');
const map = b.generateMap({
file: 'bundle.js',
includeContent: true,
- hires: true,
+ hires: true
});
const smc = new SourceMapConsumer(map);
@@ -243,26 +240,26 @@
});
it('should allow missing file option when generating map', () => {
- new Bundle()
+ new MagicString.Bundle()
.addSource({
filename: 'foo.js',
- content: new MagicString('var answer = 42;'),
+ content: new MagicString('var answer = 42;')
})
.generateMap({
includeContent: true,
- hires: true,
+ hires: true
});
});
it('should handle repeated sources', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const foo = new MagicString('var one = 1;\nvar three = 3;', {
- filename: 'foo.js',
+ filename: 'foo.js'
});
const bar = new MagicString('var two = 2;\nvar four = 4;', {
- filename: 'bar.js',
+ filename: 'bar.js'
});
b.addSource(foo.snip(0, 12));
@@ -275,7 +272,7 @@
const map = b.generateMap({
includeContent: true,
- hires: true,
+ hires: true
});
assert.equal(map.sources.length, 2);
@@ -306,7 +303,7 @@
});
it('should recover original names', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const one = new MagicString('function one () {}', { filename: 'one.js' });
const two = new MagicString('function two () {}', { filename: 'two.js' });
@@ -320,7 +317,7 @@
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -334,7 +331,7 @@
});
it('should exclude sources without filename from sourcemap', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const one = new MagicString('function one () {}', { filename: 'one.js' });
const two = new MagicString('function two () {}', { filename: null });
@@ -347,7 +344,7 @@
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -364,7 +361,7 @@
});
it('should generate x_google_ignoreList correctly', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const one = new MagicString('function one () {}', { filename: 'one.js' });
const two = new MagicString('function two () {}', { filename: 'two.js' });
@@ -377,17 +374,17 @@
b.addSource({ content: four });
const map = b.generateMap({
- file: 'output.js',
+ file: 'output.js'
});
assert.deepEqual(map.x_google_ignoreList, [
map.sources.indexOf('two.js'),
- map.sources.indexOf('three.js'),
+ map.sources.indexOf('three.js')
]);
});
it('handles prepended content', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const one = new MagicString('function one () {}', { filename: 'one.js' });
const two = new MagicString('function two () {}', { filename: 'two.js' });
@@ -399,7 +396,7 @@
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -413,7 +410,7 @@
});
it('handles appended content', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
const one = new MagicString('function one () {}', { filename: 'one.js' });
one.append('\nfunction oneAndAHalf() {}');
@@ -425,7 +422,7 @@
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -439,12 +436,12 @@
});
it('should handle empty separator', () => {
- const b = new Bundle({
- separator: '',
+ const b = new MagicString.Bundle({
+ separator: ''
});
b.addSource({
- content: new MagicString('if ( foo ) { '),
+ content: new MagicString('if ( foo ) { ')
});
const s = new MagicString('console.log( 42 );');
@@ -453,11 +450,11 @@
b.addSource({
filename: 'input.js',
- content: s,
+ content: s
});
b.addSource({
- content: new MagicString(' }'),
+ content: new MagicString(' }')
});
assert.equal(b.toString(), 'if ( foo ) { console.log( 42 ); }');
@@ -465,7 +462,7 @@
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -475,18 +472,17 @@
source: 'input.js',
name: null,
line: 1,
- column: 8,
+ column: 8
});
});
// TODO tidy this up. is a recreation of a bug in Svelte
it('generates a correct sourcemap for a Svelte component', () => {
- const b = new Bundle({
- separator: '',
+ const b = new MagicString.Bundle({
+ separator: ''
});
- const s = new MagicString(
- `
+ const s = new MagicString(`
<div></div>
<script>
@@ -495,10 +491,9 @@
console.log( 42 );
}
}
-</script>`.trim(),
- );
+</script>`.trim());
- [21, 23, 38, 42, 50, 51, 54, 59, 66, 67, 70, 72, 74, 76, 77, 81, 84, 85].forEach((pos) => {
+ [21, 23, 38, 42, 50, 51, 54, 59, 66, 67, 70, 72, 74, 76, 77, 81, 84, 85].forEach(pos => {
s.addSourcemapLocation(pos);
});
@@ -510,25 +505,22 @@
b.addSource({
content: s,
- filename: 'input.js',
+ filename: 'input.js'
});
- assert.equal(
- b.toString(),
- `
+ assert.equal(b.toString(), `
var template = (function () {
return {
onrender () {
console.log( 42 );
}
}
-}());`.trim(),
- );
+}());`.trim());
const map = b.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -538,14 +530,14 @@
source: 'input.js',
name: null,
line: 6,
- column: 16,
+ column: 16
});
});
});
describe('indent', () => {
it('should indent a bundle', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({ content: new MagicString('abcdef') });
b.addSource({ content: new MagicString('ghijkl') });
@@ -555,7 +547,7 @@
});
it('should ignore non-indented sources when guessing indentation', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({ content: new MagicString('abcdef') });
b.addSource({ content: new MagicString('ghijkl') });
@@ -566,11 +558,11 @@
});
it('should respect indent exclusion ranges', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({
content: new MagicString('abc\ndef\nghi\njkl'),
- indentExclusionRanges: [7, 15],
+ indentExclusionRanges: [7, 15]
});
b.indent(' ');
@@ -581,7 +573,7 @@
});
it('does not indent sources with no preceding newline, i.e. append()', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource(new MagicString('abcdef'));
b.addSource(new MagicString('ghijkl'));
@@ -591,7 +583,7 @@
});
it('should noop with an empty string', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource(new MagicString('abcdef'));
b.addSource(new MagicString('ghijkl'));
@@ -601,14 +593,14 @@
});
it('indents prepended content', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.prepend('a\nb').indent();
assert.equal(b.toString(), '\ta\n\tb');
});
it('indents content immediately following intro with trailing newline', () => {
- const b = new Bundle({ separator: '\n\n' });
+ const b = new MagicString.Bundle({ separator: '\n\n' });
const s = new MagicString('2');
b.addSource({ content: s });
@@ -619,19 +611,19 @@
});
it('should return this', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
assert.strictEqual(b.indent(), b);
});
it('should return this on noop', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
assert.strictEqual(b.indent(''), b);
});
});
describe('prepend', () => {
it('should append content', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({ content: new MagicString('*') });
@@ -640,21 +632,21 @@
});
it('should return this', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
assert.strictEqual(b.prepend('x'), b);
});
});
describe('trim', () => {
it('should trim bundle', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({
- content: new MagicString(' abcdef '),
+ content: new MagicString(' abcdef ')
});
b.addSource({
- content: new MagicString(' ghijkl '),
+ content: new MagicString(' ghijkl ')
});
b.trim();
@@ -662,14 +654,14 @@
});
it('should handle funky edge cases', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource({
- content: new MagicString(' abcdef '),
+ content: new MagicString(' abcdef ')
});
b.addSource({
- content: new MagicString(' x '),
+ content: new MagicString(' x ')
});
b.prepend('\n>>>\n').append(' ');
@@ -679,14 +671,14 @@
});
it('should return this', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
assert.strictEqual(b.trim(), b);
});
});
describe('toString', () => {
it('should separate with a newline by default', () => {
- const b = new Bundle();
+ const b = new MagicString.Bundle();
b.addSource(new MagicString('abc'));
b.addSource(new MagicString('def'));
@@ -695,7 +687,7 @@
});
it('should accept separator option', () => {
- const b = new Bundle({ separator: '==' });
+ const b = new MagicString.Bundle({ separator: '==' });
b.addSource(new MagicString('abc'));
b.addSource(new MagicString('def'));
@@ -704,7 +696,7 @@
});
it('should accept empty string separator option', () => {
- const b = new Bundle({ separator: '' });
+ const b = new MagicString.Bundle({ separator: '' });
b.addSource(new MagicString('abc'));
b.addSource(new MagicString('def'));
@@ -721,17 +713,17 @@
const s2 = 'VWXYZ';
const ms2 = new MagicString(s2, { filename: 'second' });
- const bundle = new Bundle();
+ const bundle = new MagicString.Bundle();
bundle.addSource(ms1);
bundle.addSource(ms2);
- ms1.remove(2, 4); // ABE
+ ms1.remove(2,4); // ABE
ms1.move(0, 1, 5); // BEA
- ms2.remove(2, 4); // VWZ
+ ms2.remove(2,4); // VWZ
ms2.move(0, 1, 5); // WZV
- const map = bundle.generateMap({ file: 'result', hires: true, includeContent: true });
+ const map = bundle.generateMap({file: 'result', hires: true, includeContent: true});
const smc = new SourceMapConsumer(map);
const result1 = ms1.toString();
@@ -760,10 +752,7 @@
assert.strictEqual(s2[loc.column], result2[i]);
}
- assert.strictEqual(
- map.toString(),
- '{"version":3,"file":"result","sources":["first","second"],"sourcesContent":["ABCDE","VWXYZ"],"names":[],"mappings":"AAAC,CAAG,CAAJ;ACAC,CAAG,CAAJ"}',
- );
+ assert.strictEqual(map.toString(), '{"version":3,"file":"result","sources":["first","second"],"sourcesContent":["ABCDE","VWXYZ"],"names":[],"mappings":"AAAC,CAAG,CAAJ;ACAC,CAAG,CAAJ"}');
});
});
});
--- a/test/MagicString.test.js
+++ b/test/MagicString.test.js
@@ -1,13 +1,14 @@
-import assert from 'assert';
-import { SourceMapConsumer } from 'source-map-js';
-import MagicString from './utils/IntegrityCheckingMagicString';
-import { describe, it } from 'vitest';
+const assert = require('assert');
+const SourceMapConsumer = require('source-map-js').SourceMapConsumer;
+const MagicString = require('./utils/IntegrityCheckingMagicString');
+
+require('source-map-support').install();
describe('MagicString', () => {
describe('options', () => {
it('stores source file information', () => {
const s = new MagicString('abc', {
- filename: 'foo.js',
+ filename: 'foo.js'
});
assert.equal(s.filename, 'foo.js');
@@ -98,26 +99,6 @@
assert.equal(s.toString(), 'x4213');
});
-
- it('should append/prepend at end of string when index is out of upper bound', () => {
- const s = new MagicString('x');
- s.prependLeft(6, 'A');
- s.appendLeft(6, 'B');
- s.prependRight(6, 'C');
- s.appendRight(6, 'D');
-
- assert.equal(s.toString(), 'ABxCD');
- });
-
- it('should append/prepend on empty string when index is out of upper bound', () => {
- const s = new MagicString('');
- s.prependLeft(6, 'A');
- s.appendLeft(6, 'B');
- s.prependRight(6, 'C');
- s.appendRight(6, 'D');
-
- assert.equal(s.toString(), 'ABCD');
- });
});
describe('appendLeft', () => {
@@ -156,7 +137,7 @@
const array = [3, 6];
const source = new MagicString('abcdefghijkl', {
filename: 'foo.js',
- indentExclusionRanges: array,
+ indentExclusionRanges: array
});
const clone = source.clone();
@@ -166,13 +147,10 @@
});
it('should clone complex indentExclusionRanges', () => {
- const array = [
- [3, 6],
- [7, 9],
- ];
+ const array = [[3, 6], [7, 9]];
const source = new MagicString('abcdefghijkl', {
filename: 'foo.js',
- indentExclusionRanges: array,
+ indentExclusionRanges: array
});
const clone = source.clone();
@@ -183,7 +161,7 @@
it('should clone sourcemapLocations', () => {
const source = new MagicString('abcdefghijkl', {
- filename: 'foo.js',
+ filename: 'foo.js'
});
source.addSourcemapLocation(3);
@@ -214,7 +192,7 @@
file: 'output.md',
source: 'input.md',
includeContent: true,
- hires: true,
+ hires: true
});
assert.equal(map.version, 3);
@@ -223,14 +201,8 @@
assert.deepEqual(map.sourcesContent, ['abcdefghijkl']);
assert.equal(map.mappings, 'AAAA,CAAC,CAAC,CAAO,CAAC,CAAC');
- assert.equal(
- map.toString(),
- '{"version":3,"file":"output.md","sources":["input.md"],"sourcesContent":["abcdefghijkl"],"names":[],"mappings":"AAAA,CAAC,CAAC,CAAO,CAAC,CAAC"}',
- );
- assert.equal(
- map.toUrl(),
- 'data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0cHV0Lm1kIiwic291cmNlcyI6WyJpbnB1dC5tZCJdLCJzb3VyY2VzQ29udGVudCI6WyJhYmNkZWZnaGlqa2wiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsQ0FBQyxDQUFDLENBQU8sQ0FBQyxDQUFDIn0=',
- );
+ assert.equal(map.toString(), '{"version":3,"file":"output.md","sources":["input.md"],"sourcesContent":["abcdefghijkl"],"names":[],"mappings":"AAAA,CAAC,CAAC,CAAO,CAAC,CAAC"}');
+ assert.equal(map.toUrl(), 'data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0cHV0Lm1kIiwic291cmNlcyI6WyJpbnB1dC5tZCJdLCJzb3VyY2VzQ29udGVudCI6WyJhYmNkZWZnaGlqa2wiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsQ0FBQyxDQUFDLENBQU8sQ0FBQyxDQUFDIn0=');
const smc = new SourceMapConsumer(map);
let loc;
@@ -257,19 +229,19 @@
includeContent: true,
});
- assert.equal(map.mappings, ';AAAA;AACA');
+ assert.equal(map.mappings,';AAAA;AACA');
});
it('should generate a correct sourcemap for indented content', () => {
const s = new MagicString('var answer = 42;\nconsole.log("the answer is %s", answer);');
- s.prepend("'use strict';\n\n");
+ s.prepend('\'use strict\';\n\n');
s.indent('\t').prepend('(function () {\n').append('\n}).call(global);');
const map = s.generateMap({
source: 'input.md',
includeContent: true,
- hires: true,
+ hires: true
});
const smc = new SourceMapConsumer(map);
@@ -290,7 +262,7 @@
const map = s.generateMap({
file: 'output.md',
source: 'input.md',
- includeContent: true,
+ includeContent: true
});
assert.equal(map.version, 3);
@@ -298,14 +270,8 @@
assert.deepEqual(map.sources, ['input.md']);
assert.deepEqual(map.sourcesContent, ['abcdefghijkl']);
- assert.equal(
- map.toString(),
- '{"version":3,"file":"output.md","sources":["input.md"],"sourcesContent":["abcdefghijkl"],"names":[],"mappings":"AAAA,GAAG,GAAM,CAAC"}',
- );
- assert.equal(
- map.toUrl(),
- 'data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0cHV0Lm1kIiwic291cmNlcyI6WyJpbnB1dC5tZCJdLCJzb3VyY2VzQ29udGVudCI6WyJhYmNkZWZnaGlqa2wiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsR0FBRyxHQUFNLENBQUMifQ==',
- );
+ assert.equal(map.toString(), '{"version":3,"file":"output.md","sources":["input.md"],"sourcesContent":["abcdefghijkl"],"names":[],"mappings":"AAAA,GAAG,GAAM,CAAC"}');
+ assert.equal(map.toUrl(), 'data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3V0cHV0Lm1kIiwic291cmNlcyI6WyJpbnB1dC5tZCJdLCJzb3VyY2VzQ29udGVudCI6WyJhYmNkZWZnaGlqa2wiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsR0FBRyxHQUFNLENBQUMifQ==');
const smc = new SourceMapConsumer(map);
let loc;
@@ -331,7 +297,7 @@
const map = s.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -362,7 +328,7 @@
const map = s.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
@@ -378,13 +344,13 @@
const map = s.generateMap({
file: 'output.js',
source: 'input.js',
- includeContent: true,
+ includeContent: true
});
const smc = new SourceMapConsumer(map);
let numMappings = 0;
- smc.eachMapping(() => (numMappings += 1));
+ smc.eachMapping(() => numMappings += 1);
assert.equal(numMappings, 3); // one at 0, one at the edit, one afterwards
});
@@ -398,7 +364,7 @@
file: 'output.js',
source: 'input.js',
includeContent: true,
- hires: true,
+ hires: true
});
const smc = new SourceMapConsumer(map);
@@ -418,7 +384,7 @@
file: 'output',
source: 'input',
includeContent: true,
- hires: true,
+ hires: true
});
const smc1 = new SourceMapConsumer(map1);
@@ -431,7 +397,7 @@
file: 'output',
source: 'input',
includeContent: true,
- hires: true,
+ hires: true
});
const smc2 = new SourceMapConsumer(map2);
@@ -461,8 +427,8 @@
it('generates x_google_ignoreList', () => {
const s = new MagicString('function foo(){}', {
- ignoreList: true,
- });
+ ignoreList: true
+ });
const map = s.generateMap({ source: 'foo.js' });
assert.deepEqual(map.sources, ['foo.js']);
@@ -479,13 +445,10 @@
file: 'output.js',
source: 'input.js',
includeContent: true,
- hires: 'boundary',
+ hires: 'boundary'
});
- assert.equal(
- map.mappings,
- 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAG,CAAC,CAAC,CAAC',
- );
+ assert.equal(map.mappings, 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAG,CAAC,CAAC,CAAC');
const smc = new SourceMapConsumer(map);
let loc;
@@ -507,33 +470,6 @@
assert.equal(loc.column, 33);
});
- it('generates segments per word boundary with hires "boundary" in the next line', () => {
- const s = new MagicString('// foo\nconsole.log("bar")');
-
- // rename bar to hello
- s.overwrite(20, 23, 'hello');
-
- const map = s.generateMap({
- file: 'output.js',
- source: 'input.js',
- includeContent: true,
- hires: 'boundary',
- });
-
- assert.equal(map.mappings, 'AAAA,CAAC,CAAC,CAAC;AACH,OAAO,CAAC,GAAG,CAAC,CAAC,KAAG,CAAC');
-
- const smc = new SourceMapConsumer(map);
- let loc;
-
- loc = smc.originalPositionFor({ line: 2, column: 2 });
- assert.equal(loc.line, 2);
- assert.equal(loc.column, 0);
-
- loc = smc.originalPositionFor({ line: 2, column: 12 });
- assert.equal(loc.line, 2);
- assert.equal(loc.column, 12);
- });
-
it('generates a correct source map with update using a content containing a new line', () => {
const s = new MagicString('foobar');
s.update(3, 4, '\nbb');
@@ -592,22 +528,6 @@
assert.equal(loc8.line, 1);
assert.equal(loc8.column, 5);
});
-
- it('generates a source map without unneeded line break mappings', () => {
- const s = new MagicString('function foo(){\n console.log("bar")\n}');
-
- const map = s.generateMap({
- file: 'output.js',
- source: 'input.js',
- includeContent: true,
- hires: 'boundary',
- });
-
- assert.equal(
- map.mappings,
- 'AAAA,QAAQ,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC;AACnB',
- );
- });
});
describe('getIndentString', () => {
@@ -924,10 +844,7 @@
s.overwrite(7, 11, 'xx');
- assert.throws(
- () => s.overwrite(8, 12, 'yy'),
- /Cannot split a chunk that has already been edited/,
- );
+ assert.throws(() => s.overwrite(8, 12, 'yy'), /Cannot split a chunk that has already been edited/);
assert.equal(s.toString(), 'abcdefgxxl');
@@ -987,10 +904,7 @@
it('should disallow overwriting zero-length ranges', () => {
const s = new MagicString('x');
- assert.throws(
- () => s.overwrite(0, 0, 'anything'),
- /Cannot overwrite a zero-length range – use appendLeft or prependRight instead/,
- );
+ assert.throws(() => s.overwrite(0, 0, 'anything'), /Cannot overwrite a zero-length range – use appendLeft or prependRight instead/);
});
it('should throw when given non-string content', () => {
@@ -1065,10 +979,7 @@
s.update(7, 11, 'xx');
- assert.throws(
- () => s.update(8, 12, 'yy'),
- /Cannot split a chunk that has already been edited/,
- );
+ assert.throws(() => s.update(8, 12, 'yy'), /Cannot split a chunk that has already been edited/);
assert.equal(s.toString(), 'abcdefgxxl');
@@ -1128,10 +1039,7 @@
it('should disallow updating zero-length ranges', () => {
const s = new MagicString('x');
- assert.throws(
- () => s.update(0, 0, 'anything'),
- /Cannot overwrite a zero-length range – use appendLeft or prependRight instead/,
- );
+ assert.throws(() => s.update(0, 0, 'anything'), /Cannot overwrite a zero-length range – use appendLeft or prependRight instead/);
});
it('should throw when given non-string content', () => {
@@ -1326,19 +1234,6 @@
assert.equal(s.toString(), 'abchidejkl');
});
-
- it('should accept negative indices', () => {
- const s = new MagicString('abcde');
- // "abcde"
- // ^
- s.remove(-2, -1);
- assert.equal(s.toString(), 'abce');
- });
-
- it('should throw error when using negative indices with empty string', () => {
- const s = new MagicString('');
- assert.throws(() => s.remove(-2, -1), /Error: Character is out of bounds/);
- });
});
describe('reset', () => {
@@ -1580,7 +1475,7 @@
describe('snip', () => {
it('should return a clone with content outside `start` and `end` removed', () => {
const s = new MagicString('abcdefghijkl', {
- filename: 'foo.js',
+ filename: 'foo.js'
});
s.overwrite(6, 9, 'GHI');
@@ -1805,15 +1700,24 @@
});
it('Should not treat string as regexp', () => {
- assert.strictEqual(new MagicString('1234').replace('.', '*').toString(), '1234');
+ assert.strictEqual(
+ new MagicString('1234').replace('.', '*').toString(),
+ '1234'
+ );
});
it('Should use substitution directly', () => {
- assert.strictEqual(new MagicString('11').replace('1', '$0$1').toString(), '$0$11');
+ assert.strictEqual(
+ new MagicString('11').replace('1', '$0$1').toString(),
+ '$0$11'
+ );
});
it('Should not search back', () => {
- assert.strictEqual(new MagicString('122121').replace('12', '21').toString(), '212121');
+ assert.strictEqual(
+ new MagicString('122121').replace('12', '21').toString(),
+ '212121'
+ );
});
it('works with global regex replace', () => {
@@ -1829,7 +1733,7 @@
s.replace(/(\d)/g, '$$');
- assert.strictEqual(s.toString(), '$ $ $ $ a b c');
+ assert.strictEqual(s.toString(),'$ $ $ $ a b c');
});
it('works with global regex replace function', () => {
@@ -1838,7 +1742,7 @@
s.replace(/(\w)(\w+)/g, (_, $1, $2) => `${$1.toUpperCase()}${$2}`);
- assert.strictEqual(s.toString(), 'Hey This Is Magic');
+ assert.strictEqual(s.toString(),'Hey This Is Magic');
});
it('replace function offset', () => {
@@ -1851,7 +1755,7 @@
const regex = /([^\d]*)(\d*)([^\w]*)/;
assert.strictEqual(
code.replace(regex, replacer),
- new MagicString(code).replace(regex, replacer).toString(),
+ new MagicString(code).replace(regex, replacer).toString()
);
});
@@ -1877,19 +1781,31 @@
describe('replaceAll', () => {
it('works with string replace', () => {
- assert.strictEqual(new MagicString('1212').replaceAll('2', '3').toString(), '1313');
+ assert.strictEqual(
+ new MagicString('1212').replaceAll('2', '3').toString(),
+ '1313',
+ );
});
it('Should not treat string as regexp', () => {
- assert.strictEqual(new MagicString('1234').replaceAll('.', '*').toString(), '1234');
+ assert.strictEqual(
+ new MagicString('1234').replaceAll('.', '*').toString(),
+ '1234'
+ );
});
it('Should use substitution directly', () => {
- assert.strictEqual(new MagicString('11').replaceAll('1', '$0$1').toString(), '$0$1$0$1');
+ assert.strictEqual(
+ new MagicString('11').replaceAll('1', '$0$1').toString(),
+ '$0$1$0$1'
+ );
});
it('Should not search back', () => {
- assert.strictEqual(new MagicString('121212').replaceAll('12', '21').toString(), '212121');
+ assert.strictEqual(
+ new MagicString('121212').replaceAll('12', '21').toString(),
+ '212121'
+ );
});
it('global regex result the same as .replace', () => {
@@ -1904,38 +1820,19 @@
);
assert.strictEqual(
- new MagicString('hey this is magic')
- .replaceAll(/(\w)(\w+)/g, (_, $1, $2) => `${$1.toUpperCase()}${$2}`)
- .toString(),
- new MagicString('hey this is magic')
- .replace(/(\w)(\w+)/g, (_, $1, $2) => `${$1.toUpperCase()}${$2}`)
- .toString(),
+ new MagicString('hey this is magic').replaceAll(/(\w)(\w+)/g, (_, $1, $2) => `${$1.toUpperCase()}${$2}`).toString(),
+ new MagicString('hey this is magic').replace(/(\w)(\w+)/g, (_, $1, $2) => `${$1.toUpperCase()}${$2}`).toString(),
);
});
it('rejects with non-global regexp', () => {
- assert.throws(() => new MagicString('123').replaceAll(/./, ''), {
- name: 'TypeError',
- message: 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
- });
- });
-
- it('with offset', () => {
- const s = new MagicString('hello world', { offset: 6 });
- assert.equal(s.slice(0, 5), 'world');
- assert.equal(s.remove(0, 5).toString(), 'hello ');
- assert.equal(s.prependLeft(0, 'w').toString(), 'hello w');
- assert.equal(s.appendLeft(0, 'o').toString(), 'hello wo');
- assert.equal(s.prependRight(0, 'r').toString(), 'hello wor');
- assert.equal(s.appendRight(0, 'l').toString(), 'hello worl');
- assert.equal(s.reset(4, 5).toString(), 'hello world');
- assert.equal(s.update(0, 5, 'd').toString(), 'hello world');
- assert.equal(s.overwrite(0, 5, 'rld').toString(), 'hello world');
-
- s.offset = 1;
- const s1 = s.clone();
- assert.strictEqual(s1.slice(), 'ello world');
- assert.equal(s1.move(0, 1, 2).slice(0), 'elo world');
+ assert.throws(
+ () => new MagicString('123').replaceAll(/./, ''),
+ {
+ name: 'TypeError',
+ message: 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
+ },
+ );
});
});
});
--- a/test/SourceMap.test.js
+++ b/test/SourceMap.test.js
@@ -1,17 +1,18 @@
-import { describe, it } from 'vitest';
-import assert from 'assert';
-import { SourceMap } from '../';
+const assert = require('assert');
+const MagicString = require('../');
+
+require('source-map-support').install();
describe('MagicString.SourceMap', () => {
describe('options', () => {
it('preserves ignore list information', () => {
- const map = new SourceMap({
+ const map = new MagicString.SourceMap({
file: 'foo.min.js',
sources: ['foo.js'],
sourcesContent: ['42'],
names: [],
mappings: [[0, 0]],
- x_google_ignoreList: [0],
+ x_google_ignoreList: [0]
});
assert.deepEqual(map.x_google_ignoreList, [0]);
@@ -20,19 +21,16 @@
describe('toString', () => {
it('serializes ignore list information', () => {
- const map = new SourceMap({
+ const map = new MagicString.SourceMap({
file: 'foo.min.js',
sources: ['foo.js'],
sourcesContent: ['42'],
names: [],
mappings: [[0, 0]],
- x_google_ignoreList: [0],
+ x_google_ignoreList: [0]
});
- assert.equal(
- map.toString(),
- '{"version":3,"file":"foo.min.js","sources":["foo.js"],"sourcesContent":["42"],"names":[],"mappings":"AAAAA,AAAAA","x_google_ignoreList":[0]}',
- );
+ assert.equal(map.toString(), '{"version":3,"file":"foo.min.js","sources":["foo.js"],"sourcesContent":["42"],"names":[],"mappings":"AAAAA,AAAAA","x_google_ignoreList":[0]}');
});
});
});
--- a/test/utils/IntegrityCheckingMagicString.js
+++ b/test/utils/IntegrityCheckingMagicString.js
@@ -1,5 +1,5 @@
-import MagicString from '../../src';
-import { assert } from 'vitest';
+const MagicString = require('../../');
+const assert = require('assert');
class IntegrityCheckingMagicString extends MagicString {
checkIntegrity() {
@@ -44,4 +44,4 @@
}
}
-export default IntegrityCheckingMagicString;
+module.exports = IntegrityCheckingMagicString;
|