File size: 57,263 Bytes
bc20498 |
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 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 |
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import { walk } from 'estree-walker';
import { getLocator } from 'locate-character';
import { reserved, is_valid } from '../utils/names.js';
import globals from '../utils/globals.js';
import { namespaces, valid_namespaces } from '../utils/namespaces.js';
import create_module from './create_module.js';
import { create_scopes, extract_names, extract_identifiers } from './utils/scope.js';
import Stylesheet from './css/Stylesheet.js';
import Fragment from './nodes/Fragment.js';
import internal_exports from './internal_exports.js';
import error from '../utils/error.js';
import get_code_frame from '../utils/get_code_frame.js';
import flatten_reference from './utils/flatten_reference.js';
import is_used_as_reference from './utils/is_used_as_reference.js';
import is_reference from 'is-reference';
import fuzzymatch from '../utils/fuzzymatch.js';
import get_object from './utils/get_object.js';
import add_to_set from './utils/add_to_set.js';
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';
import { print, b } from 'code-red';
import { is_reserved_keyword } from './utils/reserved_keywords.js';
import { apply_preprocessor_sourcemap } from '../utils/mapped_code.js';
import { clone } from '../utils/clone.js';
import compiler_warnings from './compiler_warnings.js';
import compiler_errors from './compiler_errors.js';
import {
extract_ignores_above_position,
extract_svelte_ignore_from_comments
} from '../utils/extract_svelte_ignore.js';
import check_enable_sourcemap from './utils/check_enable_sourcemap.js';
import { VERSION } from '../../shared/version.js';
const regex_leading_directory_separator = /^[/\\]/;
const regex_starts_with_term_export = /^Export/;
const regex_contains_term_function = /Function/;
export default class Component {
/** @type {import('../Stats.js').default} */
stats;
/** @type {import('../interfaces.js').Warning[]} */
warnings;
/** @type {Set<string>} */
ignores;
/** @type {Array<Set<string>>} */
ignore_stack = [];
/** @type {import('../interfaces.js').Ast} */
ast;
/** @type {import('../interfaces.js').Ast} */
original_ast;
/** @type {string} */
source;
/** @type {import('estree').Identifier} */
name;
/** @type {import('../interfaces.js').CompileOptions} */
compile_options;
/** @type {import('./nodes/Fragment.js').default} */
fragment;
/** @type {import('./utils/scope.js').Scope} */
module_scope;
/** @type {import('./utils/scope.js').Scope} */
instance_scope;
/** @type {WeakMap<import('estree').Node, import('./utils/scope.js').Scope>} */
instance_scope_map;
/** @type {ComponentOptions} */
component_options;
/** @type {string} */
namespace;
/** @type {string} */
tag;
/** @type {boolean} */
accessors;
/** @type {import('../interfaces.js').Var[]} */
vars = [];
/** @type {Map<string, import('../interfaces.js').Var>} */
var_lookup = new Map();
/** @type {import('estree').ImportDeclaration[]} */
imports = [];
/** @type {import('estree').ExportNamedDeclaration[]} */
exports_from = [];
/** @type {import('estree').ExportNamedDeclaration[]} */
instance_exports_from = [];
/** @type {Set<import('estree').Node>} */
hoistable_nodes = new Set();
/** @type {Map<string, import('estree').Node>} */
node_for_declaration = new Map();
/** @type {Array<import('estree').Node | import('estree').Node[]>} */
partly_hoisted = [];
/** @type {Array<import('estree').Node | import('estree').Node[]>} */
fully_hoisted = [];
/**
* @type {Array<{
* assignees: Set<string>;
* dependencies: Set<string>;
* node: import('estree').Node;
* declaration: import('estree').Node;
* }>}
*/
reactive_declarations = [];
/** @type {Set<import('estree').Node>} */
reactive_declaration_nodes = new Set();
/** */
has_reactive_assignments = false;
/** @type {Set<string>} */
injected_reactive_declaration_vars = new Set();
/** @type {Map<string, import('estree').Identifier>} */
helpers = new Map();
/** @type {Map<string, import('estree').Identifier>} */
globals = new Map();
/** @type {Map<string, Set<string>>} */
indirect_dependencies = new Map();
/** @type {string} */
file;
/**
* Use this for stack traces. It is 1-based and acts on pre-processed sources.
* Use `meta_locate` for metadata on DOM elements.
* @type {(c: number) => { line: number; column: number }}
*/
locate;
/**
* Use this for metadata on DOM elements. It is 1-based and acts on sources that have not been pre-processed.
* Use `locate` for source mappings.
* @type {(c: number) => { line: number; column: number }}
*/
meta_locate;
/** @type {import('./nodes/Element.js').default[]} */
elements = [];
/** @type {import('./css/Stylesheet.js').default} */
stylesheet;
/** @type {Map<string, import('estree').Identifier>} */
aliases = new Map();
/** @type {Set<string>} */
used_names = new Set();
/** @type {Set<string>} */
globally_used_names = new Set();
/** @type {Map<string, import('./nodes/Slot.js').default>} */
slots = new Map();
/** @type {Set<string>} */
slot_outlets = new Set();
/** @type {import('./nodes/shared/Tag.js').default[]} */
tags = [];
/**
* @param {import('../interfaces.js').Ast} ast
* @param {string} source
* @param {string} name
* @param {import('../interfaces.js').CompileOptions} compile_options
* @param {import('../Stats.js').default} stats
* @param {import('../interfaces.js').Warning[]} warnings
*/
constructor(ast, source, name, compile_options, stats, warnings) {
this.name = { type: 'Identifier', name };
this.stats = stats;
this.warnings = warnings;
this.ast = ast;
this.source = source;
this.compile_options = compile_options;
// the instance JS gets mutated, so we park
// a copy here for later. TODO this feels gross
this.original_ast = clone({
html: ast.html,
css: ast.css,
instance: ast.instance,
module: ast.module
});
this.file =
compile_options.filename &&
(typeof process !== 'undefined'
? compile_options.filename
.replace(process.cwd(), '')
.replace(regex_leading_directory_separator, '')
: compile_options.filename);
// line numbers in stack trace frames are 1-based. source maps are 0-based
this.locate = getLocator(this.source, { offsetLine: 1 });
/** @type {TraceMap | null | undefined} initialise lazy because only used in dev mode */
let tracer;
this.meta_locate = (c) => {
/** @type {{ line: number, column: number }} */
let location = this.locate(c);
if (tracer === undefined) {
// @ts-expect-error - fix the type of CompileOptions.sourcemap
tracer = compile_options.sourcemap ? new TraceMap(compile_options.sourcemap) : null;
}
if (tracer) {
// originalPositionFor returns 1-based lines like locator
location = originalPositionFor(tracer, location);
}
return location;
};
// styles
this.stylesheet = new Stylesheet({
source,
ast,
filename: compile_options.filename,
component_name: name,
dev: compile_options.dev,
get_css_hash: compile_options.cssHash
});
this.stylesheet.validate(this);
this.component_options = process_component_options(this, this.ast.html.children);
this.namespace =
namespaces[this.component_options.namespace] || this.component_options.namespace;
if (compile_options.customElement) {
this.tag = this.component_options.customElement?.tag || compile_options.tag || this.name.name;
} else {
this.tag = this.name.name;
}
this.walk_module_js();
this.push_ignores(
this.ast.instance
? extract_ignores_above_position(this.ast.instance.start, this.ast.html.children)
: []
);
this.walk_instance_js_pre_template();
this.pop_ignores();
this.fragment = new Fragment(this, ast.html);
this.name = this.get_unique_name(name);
this.push_ignores(
this.ast.instance
? extract_ignores_above_position(this.ast.instance.start, this.ast.html.children)
: []
);
this.walk_instance_js_post_template();
this.pop_ignores();
this.elements.forEach((element) => this.stylesheet.apply(element));
this.stylesheet.reify();
this.stylesheet.warn_on_unused_selectors(this);
}
/**
* @param {import('estree').Node} node
* @param {import('../interfaces.js').Var} variable
* @param {any} add_to_lookup
*/
add_var(node, variable, add_to_lookup = true) {
this.vars.push(variable);
if (add_to_lookup) {
if (this.var_lookup.has(variable.name)) {
const exists_var = this.var_lookup.get(variable.name);
if (exists_var.module && exists_var.imported) {
this.error(/** @type {any} */ (node), compiler_errors.illegal_variable_declaration);
}
}
this.var_lookup.set(variable.name, variable);
}
}
/**
* @param {import('estree').Node} node
* @param {string} name
*/
add_reference(node, name) {
const variable = this.var_lookup.get(name);
if (variable) {
variable.referenced = true;
} else if (is_reserved_keyword(name)) {
this.add_var(node, {
name,
injected: true,
referenced: true
});
} else if (name[0] === '$') {
this.add_var(node, {
name,
injected: true,
referenced: true,
mutated: true,
writable: true
});
const subscribable_name = name.slice(1);
const variable = this.var_lookup.get(subscribable_name);
if (variable) {
variable.referenced = true;
variable.subscribable = true;
}
} else {
if (this.compile_options.varsReport === 'full') {
this.add_var(node, { name, referenced: true }, false);
}
this.used_names.add(name);
}
}
/** @param {string} name */
alias(name) {
if (!this.aliases.has(name)) {
this.aliases.set(name, this.get_unique_name(name));
}
return this.aliases.get(name);
}
/** @param {import('./nodes/Element.js').default} element */
apply_stylesheet(element) {
this.elements.push(element);
}
/** @param {string} name */
global(name) {
const alias = this.alias(name);
this.globals.set(name, alias);
return alias;
}
/**
* @param {{ js: import('estree').Node[]; css: import('../interfaces.js').CssResult }} [result]
* @returns {import('../interfaces.js').CompileResult}
*/
generate(result) {
let js = null;
let css = null;
if (result) {
const { compile_options, name } = this;
const banner = `${this.file ? `${this.file} ` : ''}generated by Svelte v${VERSION}`;
/** @type {any} */
const program = { type: 'Program', body: result.js };
walk(program, {
enter: /**
* @param {import('estree').Node} node
* @param {import('estree').Node} parent
* @param {any} key
*/ (node, parent, key) => {
if (node.type === 'Identifier') {
if (node.name[0] === '@') {
if (node.name[1] === '_') {
const alias = this.global(node.name.slice(2));
node.name = alias.name;
} else {
let name = node.name.slice(1);
if (compile_options.hydratable) {
if (internal_exports.has(`${name}_hydration`)) {
name += '_hydration';
} else if (internal_exports.has(`${name}Hydration`)) {
name += 'Hydration';
}
}
if (compile_options.dev) {
if (internal_exports.has(`${name}_dev`)) {
name += '_dev';
} else if (internal_exports.has(`${name}Dev`)) {
name += 'Dev';
}
}
const alias = this.alias(name);
this.helpers.set(name, alias);
node.name = alias.name;
}
} else if (node.name[0] !== '#' && !is_valid(node.name)) {
// this hack allows x`foo.${bar}` where bar could be invalid
/** @type {import('estree').Literal} */
const literal = { type: 'Literal', value: node.name };
if (parent.type === 'Property' && key === 'key') {
parent.key = literal;
} else if (parent.type === 'MemberExpression' && key === 'property') {
parent.property = literal;
parent.computed = true;
}
}
}
}
});
const referenced_globals = Array.from(
this.globals,
([name, alias]) => name !== alias.name && { name, alias }
).filter(Boolean);
if (referenced_globals.length) {
this.helpers.set('globals', this.alias('globals'));
}
const imported_helpers = Array.from(this.helpers, ([name, alias]) => ({
name,
alias
}));
create_module(
program,
name,
banner,
compile_options.sveltePath,
imported_helpers,
referenced_globals,
this.imports,
this.vars
.filter((variable) => variable.module && variable.export_name)
.map((variable) => ({
name: variable.name,
as: variable.export_name
})),
this.exports_from
);
css = compile_options.customElement ? { code: null, map: null } : result.css;
const js_sourcemap_enabled = check_enable_sourcemap(compile_options.enableSourcemap, 'js');
if (!js_sourcemap_enabled) {
js = print(program);
js.map = null;
} else {
const sourcemap_source_filename = get_sourcemap_source_filename(compile_options);
js = print(program, {
sourceMapSource: sourcemap_source_filename
});
js.map.sources = [sourcemap_source_filename];
js.map.sourcesContent = [this.source];
js.map = apply_preprocessor_sourcemap(
sourcemap_source_filename,
js.map,
/** @type {string | import('@ampproject/remapping').RawSourceMap | import('@ampproject/remapping').DecodedSourceMap} */ (
compile_options.sourcemap
)
);
}
}
return {
js,
css,
ast: this.original_ast,
warnings: this.warnings,
vars: this.get_vars_report(),
stats: this.stats.render()
};
}
/**
* @param {string} name
* @param {import('./utils/scope.js').Scope} [scope]
* @returns {import('estree').Identifier}
*/
get_unique_name(name, scope) {
let alias = name;
for (
let i = 1;
reserved.has(alias) ||
this.var_lookup.has(alias) ||
this.used_names.has(alias) ||
this.globally_used_names.has(alias) ||
(scope && scope.has(alias));
) {
alias = `${name}_${i++}`;
}
this.used_names.add(alias);
return { type: 'Identifier', name: alias };
}
get_unique_name_maker() {
const local_used_names = new Set();
/** @param {string} name */
function add(name) {
local_used_names.add(name);
}
reserved.forEach(add);
internal_exports.forEach(add);
this.var_lookup.forEach((_value, key) => add(key));
/**
* @param {string} name
* @returns {import('estree').Identifier}
*/
return (name) => {
let alias = name;
for (
let i = 1;
this.used_names.has(alias) || local_used_names.has(alias);
alias = `${name}_${i++}`
);
local_used_names.add(alias);
this.globally_used_names.add(alias);
return {
type: 'Identifier',
name: alias
};
};
}
/** @returns {import('../interfaces.js').Var[]} */
get_vars_report() {
const { compile_options, vars } = this;
const vars_report =
compile_options.varsReport === false
? []
: compile_options.varsReport === 'full'
? vars
: vars.filter((v) => !v.global && !v.internal);
return vars_report.map((v) => ({
name: v.name,
export_name: v.export_name || null,
injected: v.injected || false,
module: v.module || false,
mutated: v.mutated || false,
reassigned: v.reassigned || false,
referenced: v.referenced || false,
writable: v.writable || false,
referenced_from_script: v.referenced_from_script || false
}));
}
/**
* @param {{
* start: number;
* end: number;
* }} pos
* @param {{
* code: string;
* message: string;
* }} e
*/
error(pos, e) {
if (this.compile_options.errorMode === 'warn') {
this.warn(pos, e);
} else {
error(e.message, {
name: 'ValidationError',
code: e.code,
source: this.source,
start: pos.start,
end: pos.end,
filename: this.compile_options.filename
});
}
}
/**
* @param {{
* start: number;
* end: number;
* }} pos
* @param {{
* code: string;
* message: string;
* }} warning
*/
warn(pos, warning) {
if (this.ignores && this.ignores.has(warning.code)) {
return;
}
const start = this.locate(pos.start);
const end = this.locate(pos.end);
const frame = get_code_frame(this.source, start.line - 1, start.column);
this.warnings.push({
code: warning.code,
message: warning.message,
frame,
start,
end,
pos: pos.start,
filename: this.compile_options.filename,
toString: () => `${warning.message} (${start.line}:${start.column})\n${frame}`
});
}
/** @param {any} node */
extract_imports(node) {
this.imports.push(node);
}
/**
* @param {any} node
* @param {any} module_script
*/
extract_exports(node, module_script = false) {
const ignores = extract_svelte_ignore_from_comments(node);
if (ignores.length) this.push_ignores(ignores);
const result = this._extract_exports(node, module_script);
if (ignores.length) this.pop_ignores();
return result;
}
/**
* @private
* @param {import('estree').ExportDefaultDeclaration | import('estree').ExportNamedDeclaration | import('estree').ExportAllDeclaration} node
* @param {boolean} module_script
*/
_extract_exports(node, module_script) {
if (node.type === 'ExportDefaultDeclaration') {
return this.error(/** @type {any} */ (node), compiler_errors.default_export);
}
if (node.type === 'ExportNamedDeclaration') {
if (node.source) {
if (module_script) {
this.exports_from.push(node);
} else {
this.instance_exports_from.push(node);
}
return null;
}
if (node.declaration) {
if (node.declaration.type === 'VariableDeclaration') {
node.declaration.declarations.forEach((declarator) => {
extract_names(declarator.id).forEach((name) => {
const variable = this.var_lookup.get(name);
variable.export_name = name;
if (
declarator.init?.type === 'Literal' &&
typeof declarator.init.value === 'boolean'
) {
variable.is_boolean = true;
}
if (
!module_script &&
variable.writable &&
!(variable.referenced || variable.referenced_from_script || variable.subscribable)
) {
this.warn(
/** @type {any} */ (declarator),
compiler_warnings.unused_export_let(this.name.name, name)
);
}
});
});
} else {
const { name } = node.declaration.id;
const variable = this.var_lookup.get(name);
variable.export_name = name;
}
return node.declaration;
} else {
node.specifiers.forEach((specifier) => {
const variable = this.var_lookup.get(specifier.local.name);
if (variable) {
variable.export_name = specifier.exported.name;
if (
!module_script &&
variable.writable &&
!(variable.referenced || variable.referenced_from_script || variable.subscribable)
) {
this.warn(
/** @type {any} */ (specifier),
compiler_warnings.unused_export_let(this.name.name, specifier.exported.name)
);
}
}
});
return null;
}
}
}
/** @param {any} script */
extract_javascript(script) {
if (!script) return null;
return script.content.body.filter((node) => {
if (!node) return false;
if (this.hoistable_nodes.has(node)) return false;
if (this.reactive_declaration_nodes.has(node)) return false;
if (node.type === 'ImportDeclaration') return false;
if (node.type === 'ExportDeclaration' && node.specifiers.length > 0) return false;
return true;
});
}
walk_module_js() {
const component = this;
const script = this.ast.module;
if (!script) return;
walk(script.content, {
/** @param {import('estree').Node} node */
enter(node) {
if (node.type === 'LabeledStatement' && node.label.name === '$') {
component.warn(
/** @type {any} */ (node),
compiler_warnings.module_script_reactive_declaration
);
}
}
});
const { scope, globals } = create_scopes(script.content);
this.module_scope = scope;
scope.declarations.forEach((node, name) => {
if (name[0] === '$') {
return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration);
}
const writable =
node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
const imported = node.type.startsWith('Import');
this.add_var(node, {
name,
module: true,
hoistable: true,
writable,
imported
});
});
globals.forEach((node, name) => {
if (name[0] === '$') {
return this.error(/** @type {any} */ (node), compiler_errors.illegal_subscription);
} else {
this.add_var(node, {
name,
global: true,
hoistable: true
});
}
});
const { body } = script.content;
let i = body.length;
while (--i >= 0) {
const node = body[i];
if (node.type === 'ImportDeclaration') {
this.extract_imports(node);
body.splice(i, 1);
}
if (regex_starts_with_term_export.test(node.type)) {
const replacement = this.extract_exports(node, true);
if (replacement) {
body[i] = replacement;
} else {
body.splice(i, 1);
}
}
}
}
walk_instance_js_pre_template() {
const script = this.ast.instance;
if (!script) return;
// inject vars for reactive declarations
script.content.body.forEach((node) => {
if (node.type !== 'LabeledStatement') return;
if (node.body.type !== 'ExpressionStatement') return;
const { expression } = node.body;
if (expression.type !== 'AssignmentExpression') return;
if (expression.left.type === 'MemberExpression') return;
extract_names(expression.left).forEach((name) => {
if (!this.var_lookup.has(name) && name[0] !== '$') {
this.injected_reactive_declaration_vars.add(name);
}
});
});
const { scope: instance_scope, map, globals } = create_scopes(script.content);
this.instance_scope = instance_scope;
this.instance_scope_map = map;
instance_scope.declarations.forEach((node, name) => {
if (name[0] === '$') {
return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration);
}
const { type } = node;
this.add_var(node, {
name,
initialised: instance_scope.initialised_declarations.has(name),
imported: type.startsWith('Import'),
writable: type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let')
});
this.node_for_declaration.set(name, node);
});
// NOTE: add store variable first, then only $store value
// as `$store` will mark `store` variable as referenced and subscribable
const global_keys = Array.from(globals.keys());
const sorted_globals = [
...global_keys.filter((key) => key[0] !== '$'),
...global_keys.filter((key) => key[0] === '$')
];
sorted_globals.forEach((name) => {
if (this.var_lookup.has(name)) return;
const node = globals.get(name);
if (this.injected_reactive_declaration_vars.has(name)) {
this.add_var(node, {
name,
injected: true,
writable: true,
reassigned: true,
initialised: true
});
} else if (is_reserved_keyword(name)) {
this.add_var(node, {
name,
injected: true
});
} else if (name[0] === '$') {
if (name === '$' || name[1] === '$') {
return this.error(/** @type {any} */ (node), compiler_errors.illegal_global(name));
}
this.add_var(node, {
name,
injected: true,
mutated: true,
writable: true
});
this.add_reference(node, name.slice(1));
const variable = this.var_lookup.get(name.slice(1));
if (variable) {
variable.subscribable = true;
variable.referenced_from_script = true;
}
} else {
this.add_var(node, {
name,
global: true,
hoistable: true
});
}
});
this.track_references_and_mutations();
}
walk_instance_js_post_template() {
const script = this.ast.instance;
if (!script) return;
this.post_template_walk();
this.hoist_instance_declarations();
this.extract_reactive_declarations();
this.check_if_tags_content_dynamic();
}
post_template_walk() {
const script = this.ast.instance;
if (!script) return;
const component = this;
const { content } = script;
const { instance_scope, instance_scope_map: map } = this;
let scope = instance_scope;
const to_remove = [];
/**
* @param {any} parent
* @param {any} prop
* @param {any} index
*/
const remove = (parent, prop, index) => {
to_remove.unshift([parent, prop, index]);
};
let scope_updated = false;
const current_function_stack = [];
/** @type {import('estree').FunctionDeclaration | import('estree').FunctionExpression} */
let current_function = null;
walk(content, {
/** @type {import('estree-walker').SyncHandler} */
enter(node, parent, prop, index) {
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
current_function_stack.push((current_function = node));
}
if (map.has(node)) {
scope = map.get(node);
}
let deep = false;
/** @type {string[]} */
let names = [];
if (node.type === 'AssignmentExpression') {
if (node.left.type === 'ArrayPattern') {
walk(node.left, {
/**
* @param {import('estree').Node} node
* @param {import('estree').Node} parent
*/
enter(node, parent) {
if (
node.type === 'Identifier' &&
parent.type !== 'MemberExpression' &&
(parent.type !== 'AssignmentPattern' || parent.right !== node)
) {
names.push(node.name);
}
}
});
} else {
deep = node.left.type === 'MemberExpression';
names = deep ? [get_object(node.left).name] : extract_names(node.left);
}
} else if (node.type === 'UpdateExpression') {
deep = node.argument.type === 'MemberExpression';
const { name } = get_object(node.argument);
names.push(name);
}
if (names.length > 0) {
names.forEach((name) => {
let current_scope = scope;
let declaration;
while (current_scope) {
if (current_scope.declarations.has(name)) {
declaration = current_scope.declarations.get(name);
break;
}
current_scope = current_scope.parent;
}
if (
declaration &&
/** @type {import('estree').VariableDeclaration} */ (declaration).kind === 'const' &&
!deep
) {
component.error(/** @type {any} */ (node), {
code: 'assignment-to-const',
message: 'You are assigning to a const'
});
}
});
}
if (node.type === 'ImportDeclaration') {
component.extract_imports(node);
// TODO: to use actual remove
remove(parent, prop, index);
return this.skip();
}
if (regex_starts_with_term_export.test(node.type)) {
const replacement = component.extract_exports(node);
if (replacement) {
this.replace(replacement);
} else {
// TODO: to use actual remove
remove(parent, prop, index);
}
return this.skip();
}
component.warn_on_undefined_store_value_references(node, parent, prop, scope);
},
/** @param {import('estree').Node} node */
leave(node) {
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
current_function_stack.pop();
current_function = current_function_stack[current_function_stack.length - 1];
}
// do it on leave, to prevent infinite loop
if (
component.compile_options.dev &&
component.compile_options.loopGuardTimeout > 0 &&
(!current_function || (!current_function.generator && !current_function.async))
) {
const to_replace_for_loop_protect = component.loop_protect(
node,
scope,
component.compile_options.loopGuardTimeout
);
if (to_replace_for_loop_protect) {
this.replace(to_replace_for_loop_protect);
scope_updated = true;
}
}
if (map.has(node)) {
scope = scope.parent;
}
}
});
for (const [parent, prop, index] of to_remove) {
if (parent) {
if (index !== null) {
parent[prop].splice(index, 1);
} else {
delete parent[prop];
}
}
}
if (scope_updated) {
const { scope, map } = create_scopes(script.content);
this.instance_scope = scope;
this.instance_scope_map = map;
}
}
track_references_and_mutations() {
const script = this.ast.instance;
if (!script) return;
const component = this;
const { content } = script;
const { instance_scope, module_scope, instance_scope_map: map } = this;
let scope = instance_scope;
walk(content, {
/**
* @param {import('estree').Node} node
* @param {import('estree').Node} parent
*/
enter(node, parent) {
if (map.has(node)) {
scope = map.get(node);
}
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') {
const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument;
const names = extract_names(/** @type {import('estree').Node} */ (assignee));
const deep = assignee.type === 'MemberExpression';
names.forEach((name) => {
const scope_owner = scope.find_owner(name);
if (
scope_owner !== null
? scope_owner === instance_scope
: module_scope && module_scope.has(name)
) {
const variable = component.var_lookup.get(name);
variable[deep ? 'mutated' : 'reassigned'] = true;
}
});
}
if (is_used_as_reference(node, parent)) {
const object = get_object(node);
if (scope.find_owner(object.name) === instance_scope) {
const variable = component.var_lookup.get(object.name);
variable.referenced_from_script = true;
}
}
},
/** @param {import('estree').Node} node */
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
}
});
}
/**
* @param {import('estree').Node} node
* @param {import('estree').Node} parent
* @param {string | number | symbol} prop
* @param {import('./utils/scope.js').Scope} scope
*/
warn_on_undefined_store_value_references(node, parent, prop, scope) {
if (node.type === 'LabeledStatement' && node.label.name === '$' && parent.type !== 'Program') {
this.warn(/** @type {any} */ (node), compiler_warnings.non_top_level_reactive_declaration);
}
if (
is_reference(
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node),
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent)
)
) {
const object = get_object(node);
const { name } = object;
if (name[0] === '$') {
if (!scope.has(name)) {
this.warn_if_undefined(name, object, null);
}
if (
name[1] !== '$' &&
scope.has(name.slice(1)) &&
scope.find_owner(name.slice(1)) !== this.instance_scope
) {
if (
!(
(regex_contains_term_function.test(parent.type) && prop === 'params') ||
(parent.type === 'VariableDeclarator' && prop === 'id')
)
) {
return this.error(/** @type {any} */ (node), compiler_errors.contextual_store);
}
}
}
}
}
/**
* @param {any} node
* @param {import('./utils/scope.js').Scope} scope
* @param {number} timeout
* @returns {import('estree').Node}
*/
loop_protect(node, scope, timeout) {
if (
node.type === 'WhileStatement' ||
node.type === 'ForStatement' ||
node.type === 'DoWhileStatement'
) {
const guard = this.get_unique_name('guard', scope);
this.used_names.add(guard.name);
const before = b`const ${guard} = @loop_guard(${timeout})`;
const inside = b`${guard}();`;
// wrap expression statement with BlockStatement
if (node.body.type !== 'BlockStatement') {
node.body = {
type: 'BlockStatement',
body: [node.body]
};
}
node.body.body.push(inside[0]);
return {
type: 'BlockStatement',
body: [before[0], node]
};
}
return null;
}
/** @param {(variable: import('../interfaces.js').Var) => import('estree').Node[]} get_insert */
rewrite_props(get_insert) {
if (!this.ast.instance) return;
const component = this;
const { instance_scope, instance_scope_map: map } = this;
let scope = instance_scope;
walk(this.ast.instance.content, {
/** @param {import('estree').Node} node */
enter(node) {
if (regex_contains_term_function.test(node.type)) {
return this.skip();
}
if (map.has(node)) {
scope = map.get(node);
}
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
return this.replace(node.declaration);
}
if (node.type === 'VariableDeclaration') {
// NOTE: `var` does not follow block scoping
if (node.kind === 'var' || scope === instance_scope) {
const inserts = [];
const props = [];
/**
* @param {import('estree').Identifier} exported
* @param {import('estree').Pattern} local
* @param {import('estree').Expression} default_value
*/
function add_new_props(exported, local, default_value) {
props.push({
type: 'Property',
method: false,
shorthand: false,
computed: false,
kind: 'init',
key: exported,
value: default_value
? {
type: 'AssignmentPattern',
left: local,
right: default_value
}
: local
});
}
// transform
// ```
// export let { x, y = 123 } = OBJ, z = 456
// ```
// into
// ```
// let { x: x$, y: y$ = 123 } = OBJ;
// let { x = x$, y = y$, z = 456 } = $$props;
// ```
for (let index = 0; index < node.declarations.length; index++) {
const declarator = node.declarations[index];
if (declarator.id.type !== 'Identifier') {
/**
* @param {import('estree').Identifier} local
* @returns {import('estree').Identifier}
*/
function get_new_name(local) {
const variable = component.var_lookup.get(local.name);
if (variable.subscribable) {
inserts.push(get_insert(variable));
}
if (variable.export_name && variable.writable) {
const alias_name = component.get_unique_name(local.name);
add_new_props(
{ type: 'Identifier', name: variable.export_name },
local,
alias_name
);
return alias_name;
}
return local;
}
/** @param {import('estree').Pattern} param */
function rename_identifiers(param) {
switch (param.type) {
case 'ObjectPattern': {
/** @param {import('estree').Property | import('estree').RestElement} prop */
const handle_prop = (prop) => {
if (prop.type === 'RestElement') {
rename_identifiers(prop);
} else if (prop.value.type === 'Identifier') {
prop.value = get_new_name(prop.value);
} else {
rename_identifiers(/** @type {import('estree').Pattern} */ (prop.value));
}
};
param.properties.forEach(handle_prop);
break;
}
case 'ArrayPattern': {
/**
* @param {import('estree').Pattern | null} element
* @param {number} index
* @param {Array<import('estree').Pattern | null>} array
*/
const handle_element = (element, index, array) => {
if (element) {
if (element.type === 'Identifier') {
array[index] = get_new_name(element);
} else {
rename_identifiers(element);
}
}
};
param.elements.forEach(handle_element);
break;
}
case 'RestElement':
if (param.argument.type === 'Identifier') {
param.argument = get_new_name(param.argument);
} else {
rename_identifiers(param.argument);
}
break;
case 'AssignmentPattern':
if (param.left.type === 'Identifier') {
param.left = get_new_name(param.left);
} else {
rename_identifiers(param.left);
}
break;
}
}
rename_identifiers(declarator.id);
} else {
const { name } = declarator.id;
const variable = component.var_lookup.get(name);
const is_props = variable.export_name && variable.writable;
if (is_props) {
add_new_props(
{ type: 'Identifier', name: variable.export_name },
declarator.id,
declarator.init
);
node.declarations.splice(index--, 1);
}
if (variable.subscribable && (is_props || declarator.init)) {
inserts.push(get_insert(variable));
}
}
}
this.replace(
/** @type {any} */ (
b`
${node.declarations.length ? node : null}
${props.length > 0 && b`let { ${props} } = $$props;`}
${inserts}
`
)
);
return this.skip();
}
}
},
/** @param {import('estree').Node} node */
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
}
});
}
hoist_instance_declarations() {
// we can safely hoist variable declarations that are
// initialised to literals, and functions that don't
// reference instance variables other than other
// hoistable functions. TODO others?
const { hoistable_nodes, var_lookup, injected_reactive_declaration_vars, imports } = this;
const top_level_function_declarations = new Map();
const { body } = this.ast.instance.content;
for (let i = 0; i < body.length; i += 1) {
const node = body[i];
if (node.type === 'VariableDeclaration') {
const all_hoistable = node.declarations.every((d) => {
if (!d.init) return false;
if (d.init.type !== 'Literal') return false;
// everything except const values can be changed by e.g. svelte devtools
// which means we can't hoist it
if (node.kind !== 'const' && this.compile_options.dev) return false;
for (const name of extract_names(d.id)) {
const v = this.var_lookup.get(name);
if (v.reassigned) return false;
if (v.export_name) return false;
if (this.vars.find((variable) => variable.name === name && variable.module)) {
return false;
}
}
return true;
});
if (all_hoistable) {
node.declarations.forEach((d) => {
for (const name of extract_names(d.id)) {
this.var_lookup.get(name).hoistable = true;
}
});
hoistable_nodes.add(node);
body.splice(i--, 1);
this.fully_hoisted.push(node);
}
}
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.type === 'FunctionDeclaration'
) {
top_level_function_declarations.set(node.declaration.id.name, node);
}
if (node.type === 'FunctionDeclaration') {
top_level_function_declarations.set(node.id.name, node);
}
}
const checked = new Set();
const walking = new Set();
/** @param {any} fn_declaration */
const is_hoistable = (fn_declaration) => {
if (fn_declaration.type === 'ExportNamedDeclaration') {
fn_declaration = fn_declaration.declaration;
}
const instance_scope = this.instance_scope;
let scope = this.instance_scope;
const map = this.instance_scope_map;
let hoistable = true;
// handle cycles
walking.add(fn_declaration);
walk(fn_declaration, {
/** @type {import('estree-walker').SyncHandler} */
enter(node, parent) {
if (!hoistable) return this.skip();
if (map.has(node)) {
scope = map.get(node);
}
if (
is_reference(
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node),
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent)
)
) {
const { name } = flatten_reference(node);
const owner = scope.find_owner(name);
if (injected_reactive_declaration_vars.has(name)) {
hoistable = false;
} else if (name[0] === '$' && !owner) {
hoistable = false;
} else if (owner === instance_scope) {
const variable = var_lookup.get(name);
if (variable.reassigned || variable.mutated) hoistable = false;
if (name === fn_declaration.id.name) return;
if (variable.hoistable) return;
if (top_level_function_declarations.has(name)) {
const other_declaration = top_level_function_declarations.get(name);
if (walking.has(other_declaration)) {
hoistable = false;
} else if (
other_declaration.type === 'ExportNamedDeclaration' &&
walking.has(other_declaration.declaration)
) {
hoistable = false;
} else if (!is_hoistable(other_declaration)) {
hoistable = false;
}
} else {
hoistable = false;
}
}
this.skip();
}
},
/** @param {import('estree').Node} node */
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
}
});
checked.add(fn_declaration);
walking.delete(fn_declaration);
return hoistable;
};
for (const [name, node] of top_level_function_declarations) {
if (is_hoistable(node)) {
const variable = this.var_lookup.get(name);
variable.hoistable = true;
hoistable_nodes.add(node);
const i = body.indexOf(node);
body.splice(i, 1);
this.fully_hoisted.push(node);
}
}
for (const { specifiers } of imports) {
for (const specifier of specifiers) {
const variable = var_lookup.get(specifier.local.name);
if (!variable.mutated || variable.subscribable) {
variable.hoistable = true;
}
}
}
}
extract_reactive_declarations() {
const component = this;
/**
* @type {Array<{
* assignees: Set<string>;
* dependencies: Set<string>;
* node: import('estree').Node;
* declaration: import('estree').Node;
* }>}
*/
const unsorted_reactive_declarations = [];
this.ast.instance.content.body.forEach((node) => {
const ignores = extract_svelte_ignore_from_comments(node);
if (ignores.length) this.push_ignores(ignores);
if (node.type === 'LabeledStatement' && node.label.name === '$') {
this.reactive_declaration_nodes.add(node);
const assignees = new Set();
const assignee_nodes = new Set();
const dependencies = new Set();
const module_dependencies = new Set();
let scope = this.instance_scope;
const { declarations: outset_scope_decalarations } = this.instance_scope;
const map = this.instance_scope_map;
walk(node.body, {
/** @type {import('estree-walker').SyncHandler} */
enter(node, parent) {
if (node.type === 'VariableDeclaration' && node.kind === 'var') {
const is_var_in_outset = node.declarations.some(
/** @param {import('estree').VariableDeclarator} declaration */ (declaration) => {
/** @type {string[]} */
const names = extract_names(declaration.id);
return !!names.find(
/** @param {string} name */ (name) => {
const var_node = outset_scope_decalarations.get(name);
return var_node === node;
}
);
}
);
if (is_var_in_outset) {
return component.error(
/** @type {any} */ (node),
compiler_errors.invalid_var_declaration
);
}
}
if (map.has(node)) {
scope = map.get(node);
}
if (node.type === 'AssignmentExpression') {
const left = get_object(node.left);
extract_identifiers(left).forEach((node) => {
assignee_nodes.add(node);
assignees.add(node.name);
});
if (node.operator !== '=') {
dependencies.add(left.name);
}
} else if (node.type === 'UpdateExpression') {
const identifier = get_object(node.argument);
assignees.add(identifier.name);
} else if (
is_reference(
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (node),
/** @type {import('is-reference').NodeWithPropertyDefinition} */ (parent)
)
) {
const identifier = get_object(node);
if (!assignee_nodes.has(identifier)) {
const { name } = identifier;
const owner = scope.find_owner(name);
const variable = component.var_lookup.get(name);
let should_add_as_dependency = true;
if (variable) {
variable.is_reactive_dependency = true;
if (variable.module && variable.writable) {
should_add_as_dependency = false;
module_dependencies.add(name);
}
}
const is_writable_or_mutated = variable && (variable.writable || variable.mutated);
if (
should_add_as_dependency &&
(!owner || owner === component.instance_scope) &&
(name[0] === '$' || is_writable_or_mutated)
) {
dependencies.add(name);
}
}
this.skip();
}
},
/** @param {import('estree').Node} node */
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
}
});
if (module_dependencies.size > 0 && dependencies.size === 0) {
component.warn(
/** @type {any} */ (node.body),
compiler_warnings.module_script_variable_reactive_declaration(
Array.from(module_dependencies)
)
);
}
const { expression } = /** @type {import('estree').ExpressionStatement} */ (node.body);
const declaration =
expression && /** @type {import('estree').AssignmentExpression} */ (expression).left;
unsorted_reactive_declarations.push({
assignees,
dependencies,
node,
declaration
});
}
if (ignores.length) this.pop_ignores();
});
const lookup = new Map();
unsorted_reactive_declarations.forEach((declaration) => {
declaration.assignees.forEach((name) => {
if (!lookup.has(name)) {
lookup.set(name, []);
}
// TODO warn or error if a name is assigned to in
// multiple reactive declarations?
lookup.get(name).push(declaration);
});
});
const cycle = check_graph_for_cycles(
unsorted_reactive_declarations.reduce((acc, declaration) => {
declaration.assignees.forEach((v) => {
declaration.dependencies.forEach((w) => {
if (!declaration.assignees.has(w)) {
acc.push([v, w]);
}
});
});
return acc;
}, [])
);
if (cycle && cycle.length) {
const declaration_list = lookup.get(cycle[0]);
const declaration = declaration_list[0];
return this.error(declaration.node, compiler_errors.cyclical_reactive_declaration(cycle));
}
/** @param {any} declaration */
const add_declaration = (declaration) => {
if (this.reactive_declarations.includes(declaration)) return;
declaration.dependencies.forEach((name) => {
if (declaration.assignees.has(name)) return;
const earlier_declarations = lookup.get(name);
if (earlier_declarations) {
earlier_declarations.forEach(add_declaration);
}
});
this.reactive_declarations.push(declaration);
};
unsorted_reactive_declarations.forEach(add_declaration);
}
check_if_tags_content_dynamic() {
this.tags.forEach((tag) => {
tag.check_if_content_dynamic();
});
}
/**
* @param {string} name
* @param {any} node
* @param {import('./nodes/shared/TemplateScope.js').default} template_scope
* @param {import("./nodes/shared/Node.js").default} [owner]
*/
warn_if_undefined(name, node, template_scope, owner) {
if (name[0] === '$') {
if (name === '$' || (name[1] === '$' && !is_reserved_keyword(name))) {
return this.error(node, compiler_errors.illegal_global(name));
}
this.has_reactive_assignments = true; // TODO does this belong here?
if (is_reserved_keyword(name)) return;
name = name.slice(1);
}
if (this.var_lookup.has(name) && !this.var_lookup.get(name).global) return;
if (template_scope && template_scope.names.has(name)) return;
if (globals.has(name) && node.type !== 'InlineComponent') return;
function has_out_of_scope_let() {
for (let parent = owner.parent; parent; parent = parent.parent) {
if (parent.type === 'InlineComponent') {
const { let_attributes } = parent;
for (const attr of let_attributes) {
if (
// @ts-expect-error
// TODO extract_names only considers patterns but let attributes return expressions
(attr.expression && extract_names(attr.expression).includes(name)) ||
attr.name === name
)
return true;
}
}
}
return false;
}
if (owner && has_out_of_scope_let()) {
return this.warn(node, {
code: 'missing-declaration',
message: `let:${name} declared on parent component cannot be used inside named slot`
});
}
this.warn(node, compiler_warnings.missing_declaration(name, !!this.ast.instance));
}
/** @param {any} ignores */
push_ignores(ignores) {
this.ignores = new Set(this.ignores || []);
add_to_set(this.ignores, ignores);
this.ignore_stack.push(this.ignores);
}
pop_ignores() {
this.ignore_stack.pop();
this.ignores = this.ignore_stack[this.ignore_stack.length - 1];
}
}
const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;
/**
* @param {Component} component
* @param {any} nodes
*/
function process_component_options(component, nodes) {
/** @type {ComponentOptions} */
const component_options = {
immutable: component.compile_options.immutable || false,
accessors:
'accessors' in component.compile_options
? component.compile_options.accessors
: !!component.compile_options.customElement,
preserveWhitespace: !!component.compile_options.preserveWhitespace,
namespace: component.compile_options.namespace
};
const node = nodes.find((node) => node.name === 'svelte:options');
/**
* @param {any} attribute
* @param {any} params_0
*/
function get_value(attribute, { code, message }) {
const { value } = attribute;
const chunk = value[0];
if (!chunk) return true;
if (value.length > 1) {
return component.error(attribute, { code, message });
}
if (chunk.type === 'Text') return chunk.data;
if (chunk.expression.type !== 'Literal') {
return component.error(attribute, { code, message });
}
return chunk.expression.value;
}
if (node) {
node.attributes.forEach((attribute) => {
if (attribute.type === 'Attribute') {
const { name } = attribute;
/**
* @param {import('../interfaces.js').Attribute} attribute
* @param {string} tag
*/
function parse_tag(attribute, tag) {
if (typeof tag !== 'string' && tag !== null) {
return component.error(attribute, compiler_errors.invalid_tag_attribute);
}
if (tag && !regex_valid_tag_name.test(tag)) {
return component.error(attribute, compiler_errors.invalid_tag_property);
}
if (tag && !component.compile_options.customElement) {
component.warn(attribute, compiler_warnings.missing_custom_element_compile_options);
}
component_options.customElement =
component_options.customElement || /** @type {any} */ ({});
component_options.customElement.tag = tag;
}
switch (name) {
case 'tag': {
component.warn(attribute, compiler_warnings.tag_option_deprecated);
parse_tag(attribute, get_value(attribute, compiler_errors.invalid_tag_attribute));
break;
}
case 'customElement': {
component_options.customElement =
component_options.customElement || /** @type {any} */ ({});
const { value } = attribute;
if (value[0].type === 'MustacheTag' && value[0].expression?.value === null) {
component_options.customElement.tag = null;
break;
} else if (value[0].type === 'Text') {
parse_tag(attribute, get_value(attribute, compiler_errors.invalid_tag_attribute));
break;
} else if (value[0].expression.type !== 'ObjectExpression') {
return component.error(attribute, compiler_errors.invalid_customElement_attribute);
}
const tag = value[0].expression.properties.find((prop) => prop.key.name === 'tag');
if (tag) {
parse_tag(tag, tag.value?.value);
} else {
return component.error(attribute, compiler_errors.invalid_customElement_attribute);
}
const props = value[0].expression.properties.find((prop) => prop.key.name === 'props');
if (props) {
const error = () =>
component.error(attribute, compiler_errors.invalid_props_attribute);
if (props.value?.type !== 'ObjectExpression') {
return error();
}
component_options.customElement.props = {};
for (const property of /** @type {import('estree').ObjectExpression} */ (props.value)
.properties) {
if (
property.type !== 'Property' ||
property.computed ||
property.key.type !== 'Identifier' ||
property.value.type !== 'ObjectExpression'
) {
return error();
}
component_options.customElement.props[property.key.name] = {};
for (const prop of property.value.properties) {
if (
prop.type !== 'Property' ||
prop.computed ||
prop.key.type !== 'Identifier' ||
prop.value.type !== 'Literal'
) {
return error();
}
if (
['reflect', 'attribute', 'type'].indexOf(prop.key.name) === -1 ||
(prop.key.name === 'type' &&
['String', 'Number', 'Boolean', 'Array', 'Object'].indexOf(
/** @type {string} */ (prop.value.value)
) === -1) ||
(prop.key.name === 'reflect' && typeof prop.value.value !== 'boolean') ||
(prop.key.name === 'attribute' && typeof prop.value.value !== 'string')
) {
return error();
}
component_options.customElement.props[property.key.name][prop.key.name] =
prop.value.value;
}
}
}
const shadow = value[0].expression.properties.find(
(prop) => prop.key.name === 'shadow'
);
if (shadow) {
const shadowdom = shadow.value?.value;
if (shadowdom !== 'open' && shadowdom !== 'none') {
return component.error(shadow, compiler_errors.invalid_shadow_attribute);
}
component_options.customElement.shadow = shadowdom;
}
const extend = value[0].expression.properties.find(
(prop) => prop.key.name === 'extend'
);
if (extend?.value) {
component_options.customElement.extend = extend.value;
}
break;
}
case 'namespace': {
const ns = get_value(attribute, compiler_errors.invalid_namespace_attribute);
if (typeof ns !== 'string') {
return component.error(attribute, compiler_errors.invalid_namespace_attribute);
}
if (valid_namespaces.indexOf(ns) === -1) {
const match = fuzzymatch(ns, valid_namespaces);
return component.error(
attribute,
compiler_errors.invalid_namespace_property(ns, match)
);
}
component_options.namespace = ns;
break;
}
case 'accessors':
case 'immutable':
case 'preserveWhitespace': {
const value = get_value(attribute, compiler_errors.invalid_attribute_value(name));
if (typeof value !== 'boolean') {
return component.error(attribute, compiler_errors.invalid_attribute_value(name));
}
component_options[name] = value;
break;
}
default:
return component.error(
attribute,
compiler_errors.invalid_options_attribute_unknown(name)
);
}
} else {
return component.error(attribute, compiler_errors.invalid_options_attribute);
}
});
}
return component_options;
}
/**
* @param {string} from
* @param {string} to
*/
function get_relative_path(from, to) {
const from_parts = from.split(/[/\\]/);
const to_parts = to.split(/[/\\]/);
from_parts.pop(); // get dirname
while (from_parts[0] === to_parts[0]) {
from_parts.shift();
to_parts.shift();
}
if (from_parts.length) {
let i = from_parts.length;
while (i--) from_parts[i] = '..';
}
return from_parts.concat(to_parts).join('/');
}
/** @param {string} filename */
function get_basename(filename) {
return filename.split(/[/\\]/).pop();
}
/** @param {import('../interfaces.js').CompileOptions} compile_options */
function get_sourcemap_source_filename(compile_options) {
if (!compile_options.filename) return null;
return compile_options.outputFilename
? get_relative_path(compile_options.outputFilename, compile_options.filename)
: get_basename(compile_options.filename);
}
/**
* @typedef {Object} ComponentOptions
* @property {string} [namespace]
* @property {boolean} [immutable]
* @property {boolean} [accessors]
* @property {boolean} [preserveWhitespace]
* @property {Object} [customElement]
* @property {string|null} customElement.tag
* @property {'open'|'none'} [customElement.shadow]
* @property {Record<string,{attribute?:string;reflect?:boolean;type?:'String'|'Boolean'|'Number'|'Array'|'Object';}>} [customElement.props]
* @property {(ceClass: new () => HTMLElement) => new () => HTMLElement} [customElement.extend]
*/
|