File size: 91,498 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 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 |
import { parsers as parsers$1 } from 'prettier/plugins/babel';
import { doc } from 'prettier/standalone';
import { VERSION, parse } from 'svelte/compiler';
// @see http://xahlee.info/js/html5_non-closing_tag.html
const selfClosingTags = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr',
];
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements#Elements
const blockElements = [
'address',
'article',
'aside',
'blockquote',
'details',
'dialog',
'dd',
'div',
'dl',
'dt',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'header',
'hgroup',
'hr',
'li',
'main',
'nav',
'ol',
'p',
'pre',
'section',
'table',
'ul',
];
/**
* HTML attributes that we may safely reformat (trim whitespace, add or remove newlines)
*/
const formattableAttributes = [
// None at the moment
// Prettier HTML does not format attributes at all
// and to be consistent we leave this array empty for now
];
// Base64 string encoding and decoding module.
// Uses Buffer for Node.js and btoa/atob for browser environments.
// We use TextEncoder/TextDecoder for browser environments because
// they can handle non-ASCII characters, unlike btoa/atob.
const stringToBase64 = typeof Buffer !== 'undefined'
? (str) => Buffer.from(str).toString('base64')
: (str) => btoa(new TextEncoder()
.encode(str)
.reduce((acc, byte) => acc + String.fromCharCode(byte), ''));
const base64ToString = typeof Buffer !== 'undefined'
? (str) => Buffer.from(str, 'base64').toString()
: (str) => new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0)));
const snippedTagContentAttribute = '✂prettier:content✂';
const scriptRegex = /<!--[^]*?-->|<script((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/script>/g;
const styleRegex = /<!--[^]*?-->|<style((?:\s+[^=>'"\/\s]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/\s]+)*\s*)>([^]*?)<\/style>/g;
const langTsRegex = /\slang=["']?ts["']?/;
function snipScriptAndStyleTagContent(source) {
let scriptMatchSpans = getMatchIndexes('script');
let styleMatchSpans = getMatchIndexes('style');
let isTypescript = false;
const text = snipTagContent(snipTagContent(source, 'script', '{}', styleMatchSpans), 'style', '', scriptMatchSpans);
return { text, isTypescript };
function getMatchIndexes(tagName) {
const regex = getRegexp(tagName);
const indexes = [];
let match = null;
while ((match = regex.exec(source)) != null) {
if (source.slice(match.index, match.index + 4) !== '<!--') {
indexes.push([match.index, regex.lastIndex]);
}
}
return indexes;
}
function snipTagContent(_source, tagName, placeholder, otherSpans) {
const regex = getRegexp(tagName);
let newScriptMatchSpans = scriptMatchSpans;
let newStyleMatchSpans = styleMatchSpans;
// Replace valid matches
const newSource = _source.replace(regex, (match, attributes, content, index) => {
if (match.startsWith('<!--') || withinOtherSpan(index)) {
return match;
}
if (langTsRegex.test(attributes)) {
isTypescript = true;
}
const encodedContent = stringToBase64(content);
const newContent = `<${tagName}${attributes} ${snippedTagContentAttribute}="${encodedContent}">${placeholder}</${tagName}>`;
// Adjust the spans because the source now has a different content length
const lengthDiff = match.length - newContent.length;
newScriptMatchSpans = adjustSpans(scriptMatchSpans, newScriptMatchSpans);
newStyleMatchSpans = adjustSpans(styleMatchSpans, newStyleMatchSpans);
function adjustSpans(oldSpans, newSpans) {
return oldSpans.map((oldSpan, idx) => {
const newSpan = newSpans[idx];
// Do the check using the old spans because the replace function works
// on the old spans. Replace oldSpans with newSpans afterwards.
if (oldSpan[0] > index) {
// span is after the match -> adjust start and end
return [newSpan[0] - lengthDiff, newSpan[1] - lengthDiff];
}
else if (oldSpan[0] === index) {
// span is the match -> adjust end only
return [newSpan[0], newSpan[1] - lengthDiff];
}
else {
// span is before the match -> nothing to adjust
return newSpan;
}
});
}
return newContent;
});
// Now that the replacement function ran, we can adjust the spans for the next run
scriptMatchSpans = newScriptMatchSpans;
styleMatchSpans = newStyleMatchSpans;
return newSource;
function withinOtherSpan(idx) {
return otherSpans.some((otherSpan) => idx > otherSpan[0] && idx < otherSpan[1]);
}
}
function getRegexp(tagName) {
return tagName === 'script' ? scriptRegex : styleRegex;
}
}
function hasSnippedContent(text) {
return text.includes(snippedTagContentAttribute);
}
const regex = /(<\w+.*?)\s*✂prettier:content✂="(.*?)">.*?(?=<\/)/gi;
function unsnipContent(text) {
return text.replace(regex, (_, start, encodedContent) => {
const content = base64ToString(encodedContent);
return `${start}>${content}`;
});
}
function makeChoice(choice) {
return { value: choice, description: choice };
}
const options = {
svelteSortOrder: {
category: 'Svelte',
type: 'choice',
default: 'options-scripts-markup-styles',
description: 'Sort order for scripts, markup, and styles',
choices: [
makeChoice('options-scripts-markup-styles'),
makeChoice('options-scripts-styles-markup'),
makeChoice('options-markup-styles-scripts'),
makeChoice('options-markup-scripts-styles'),
makeChoice('options-styles-markup-scripts'),
makeChoice('options-styles-scripts-markup'),
makeChoice('scripts-options-markup-styles'),
makeChoice('scripts-options-styles-markup'),
makeChoice('markup-options-styles-scripts'),
makeChoice('markup-options-scripts-styles'),
makeChoice('styles-options-markup-scripts'),
makeChoice('styles-options-scripts-markup'),
makeChoice('scripts-markup-options-styles'),
makeChoice('scripts-styles-options-markup'),
makeChoice('markup-styles-options-scripts'),
makeChoice('markup-scripts-options-styles'),
makeChoice('styles-markup-options-scripts'),
makeChoice('styles-scripts-options-markup'),
makeChoice('scripts-markup-styles-options'),
makeChoice('scripts-styles-markup-options'),
makeChoice('markup-styles-scripts-options'),
makeChoice('markup-scripts-styles-options'),
makeChoice('styles-markup-scripts-options'),
makeChoice('styles-scripts-markup-options'),
makeChoice('none'),
],
},
svelteStrictMode: {
category: 'Svelte',
type: 'boolean',
default: false,
description: 'More strict HTML syntax: Quotes in attributes, no self-closing DOM tags',
},
svelteBracketNewLine: {
category: 'Svelte',
type: 'boolean',
description: 'Put the `>` of a multiline element on a new line',
deprecated: '2.5.0',
},
svelteAllowShorthand: {
category: 'Svelte',
type: 'boolean',
default: true,
description: 'Option to enable/disable component attribute shorthand if attribute name and expressions are same',
},
svelteIndentScriptAndStyle: {
category: 'Svelte',
type: 'boolean',
default: true,
description: 'Whether or not to indent the code inside <script> and <style> tags in Svelte files',
},
};
const sortOrderSeparator = '-';
function parseSortOrder(sortOrder = 'options-scripts-markup-styles') {
if (sortOrder === 'none') {
return [];
}
const order = sortOrder.split(sortOrderSeparator);
// For backwards compatibility: Add options to beginning if not present
if (!order.includes('options')) {
throw new Error('svelteSortOrder is missing option `options`');
}
return order;
}
function isBracketSameLine(options) {
return options.svelteBracketNewLine != null
? !options.svelteBracketNewLine
: options.bracketSameLine != null
? options.bracketSameLine
: false;
}
/**
* Determines whether or not given node
* is the root of the Svelte AST.
*/
function isASTNode(n) {
return n && n.__isRoot;
}
function isPreTagContent(path) {
const stack = path.stack;
return stack.some((node) => (node.type === 'Element' && node.name.toLowerCase() === 'pre') ||
(node.type === 'Attribute' && !formattableAttributes.includes(node.name)));
}
function flatten(arrays) {
return [].concat.apply([], arrays);
}
function findLastIndex(isMatch, items) {
for (let i = items.length - 1; i >= 0; i--) {
if (isMatch(items[i], i)) {
return i;
}
}
return -1;
}
function replaceEndOfLineWith(text, replacement) {
const parts = [];
for (const part of text.split('\n')) {
if (parts.length > 0) {
parts.push(replacement);
}
if (part.endsWith('\r')) {
parts.push(part.slice(0, -1));
}
else {
parts.push(part);
}
}
return parts;
}
function getAttributeLine(node, options) {
const { hardline, line } = doc.builders;
const hasThisBinding = (node.type === 'InlineComponent' && !!node.expression) ||
(node.type === 'Element' && !!node.tag);
const attributes = node.attributes.filter((attribute) => attribute.name !== snippedTagContentAttribute);
return options.singleAttributePerLine &&
(attributes.length > 1 || (attributes.length && hasThisBinding))
? hardline
: line;
}
function printWithPrependedAttributeLine(node, options, print) {
return (path) => path.getNode().name !== snippedTagContentAttribute
? [getAttributeLine(node, options), path.call(print)]
: '';
}
/**
* Check if doc is a hardline.
* We can't just rely on a simple equality check because the doc could be created with another
* runtime version of prettier than what we import, making a reference check fail.
*/
function isHardline(docToCheck) {
return docToCheck === doc.builders.hardline || deepEqual(docToCheck, doc.builders.hardline);
}
/**
* Simple deep equal function which suits our needs. Only works properly on POJOs without cyclic deps.
*/
function deepEqual(x, y) {
if (x === y) {
return true;
}
else if (typeof x == 'object' && x != null && typeof y == 'object' && y != null) {
if (Object.keys(x).length != Object.keys(y).length)
return false;
for (var prop in x) {
if (y.hasOwnProperty(prop)) {
if (!deepEqual(x[prop], y[prop]))
return false;
}
else {
return false;
}
}
return true;
}
else {
return false;
}
}
function isDocCommand(doc) {
return typeof doc === 'object' && doc !== null;
}
function isLine(docToCheck) {
return (isHardline(docToCheck) ||
(isDocCommand(docToCheck) && docToCheck.type === 'line') ||
(Array.isArray(docToCheck) && docToCheck.every(isLine)));
}
/**
* Check if the doc is empty, i.e. consists of nothing more than empty strings (possibly nested).
*/
function isEmptyDoc(doc) {
if (typeof doc === 'string') {
return doc.length === 0;
}
if (isDocCommand(doc) && doc.type === 'line') {
return !doc.keepIfLonely;
}
if (Array.isArray(doc)) {
return doc.length === 0;
}
const { contents } = doc;
if (contents) {
return isEmptyDoc(contents);
}
const { parts } = doc;
if (parts) {
return isEmptyGroup(parts);
}
return false;
}
function isEmptyGroup(group) {
return !group.find((doc) => !isEmptyDoc(doc));
}
/**
* Trims both leading and trailing nodes matching `isWhitespace` independent of nesting level
* (though all trimmed adjacent nodes need to be a the same level). Modifies the `docs` array.
*/
function trim(docs, isWhitespace) {
trimLeft(docs, isWhitespace);
trimRight(docs, isWhitespace);
return docs;
}
/**
* Trims the leading nodes matching `isWhitespace` independent of nesting level (though all nodes need to be a the same level).
* If there are empty docs before the first whitespace, they are removed, too.
*/
function trimLeft(group, isWhitespace) {
let firstNonWhitespace = group.findIndex((doc) => !isEmptyDoc(doc) && !isWhitespace(doc));
if (firstNonWhitespace < 0 && group.length) {
firstNonWhitespace = group.length;
}
if (firstNonWhitespace > 0) {
const removed = group.splice(0, firstNonWhitespace);
if (removed.every(isEmptyDoc)) {
return trimLeft(group, isWhitespace);
}
}
else {
const parts = getParts(group[0]);
if (parts) {
return trimLeft(parts, isWhitespace);
}
}
}
/**
* Trims the trailing nodes matching `isWhitespace` independent of nesting level (though all nodes need to be a the same level).
* If there are empty docs after the last whitespace, they are removed, too.
*/
function trimRight(group, isWhitespace) {
let lastNonWhitespace = group.length
? findLastIndex((doc) => !isEmptyDoc(doc) && !isWhitespace(doc), group)
: 0;
if (lastNonWhitespace < group.length - 1) {
const removed = group.splice(lastNonWhitespace + 1);
if (removed.every(isEmptyDoc)) {
return trimRight(group, isWhitespace);
}
}
else {
const parts = getParts(group[group.length - 1]);
if (parts) {
return trimRight(parts, isWhitespace);
}
}
}
function getParts(doc) {
if (typeof doc === 'object') {
if (Array.isArray(doc)) {
return doc;
}
if (doc.type === 'fill') {
return doc.parts;
}
if (doc.type === 'group') {
return getParts(doc.contents);
}
}
}
/**
* `(foo = bar)` => `foo = bar`
*/
function removeParentheses(doc) {
return trim([doc], (_doc) => _doc === '(' || _doc === ')')[0];
}
const unsupportedLanguages = ['coffee', 'coffeescript', 'styl', 'stylus', 'sass'];
function isInlineElement(path, options, node) {
return (node && node.type === 'Element' && !isBlockElement(node, options) && !isPreTagContent(path));
}
function isBlockElement(node, options) {
return (node &&
node.type === 'Element' &&
options.htmlWhitespaceSensitivity !== 'strict' &&
(options.htmlWhitespaceSensitivity === 'ignore' ||
blockElements.includes(node.name)));
}
function isSvelteBlock(node) {
return [
'IfBlock',
'SnippetBlock',
'AwaitBlock',
'CatchBlock',
'EachBlock',
'ElseBlock',
'KeyBlock',
'PendingBlock',
'ThenBlock',
].includes(node.type);
}
function isNodeWithChildren(node) {
return node.children;
}
function getChildren(node) {
return isNodeWithChildren(node) ? node.children : [];
}
/**
* Returns siblings, that is, the children of the parent.
*/
function getSiblings(path) {
let parent = path.getParentNode();
if (isASTNode(parent)) {
parent = parent.html;
}
return getChildren(parent);
}
/**
* Returns the next sibling node.
*/
function getNextNode(path, node = path.getNode()) {
return getSiblings(path).find((child) => child.start === node.end);
}
/**
* Returns the comment that is above the current node.
*/
function getLeadingComment(path) {
const siblings = getSiblings(path);
let node = path.getNode();
let prev = siblings.find((child) => child.end === node.start);
while (prev) {
if (prev.type === 'Comment' &&
!isIgnoreStartDirective(prev) &&
!isIgnoreEndDirective(prev)) {
return prev;
}
else if (isEmptyTextNode(prev)) {
node = prev;
prev = siblings.find((child) => child.end === node.start);
}
else {
return undefined;
}
}
}
/**
* Did there use to be any embedded object (that has been snipped out of the AST to be moved)
* at the specified position?
*/
function doesEmbedStartAfterNode(node, path, siblings = getSiblings(path)) {
// If node is not at the top level of html, an embed cannot start after it,
// because embeds are only at the top level
if (!isNodeTopLevelHTML(node, path)) {
return false;
}
const position = node.end;
const root = path.stack[0];
const embeds = [root.css, root.html, root.instance, root.js, root.module];
const nextNode = siblings[siblings.indexOf(node) + 1];
return embeds.find((n) => n && n.start >= position && (!nextNode || n.end <= nextNode.start));
}
function isNodeTopLevelHTML(node, path) {
const root = path.stack[0];
return !!root.html && !!root.html.children && root.html.children.includes(node);
}
function isEmptyTextNode(node) {
return !!node && node.type === 'Text' && getUnencodedText(node).trim() === '';
}
function isIgnoreDirective(node) {
return !!node && node.type === 'Comment' && node.data.trim() === 'prettier-ignore';
}
function isIgnoreStartDirective(node) {
return !!node && node.type === 'Comment' && node.data.trim() === 'prettier-ignore-start';
}
function isIgnoreEndDirective(node) {
return !!node && node.type === 'Comment' && node.data.trim() === 'prettier-ignore-end';
}
function printRaw(node, originalText, stripLeadingAndTrailingNewline = false) {
if (node.children.length === 0) {
return '';
}
const firstChild = node.children[0];
const lastChild = node.children[node.children.length - 1];
let raw = originalText.substring(firstChild.start, lastChild.end);
if (!stripLeadingAndTrailingNewline) {
return raw;
}
if (startsWithLinebreak(raw)) {
raw = raw.substring(raw.indexOf('\n') + 1);
}
if (endsWithLinebreak(raw)) {
raw = raw.substring(0, raw.lastIndexOf('\n'));
if (raw.charAt(raw.length - 1) === '\r') {
raw = raw.substring(0, raw.length - 1);
}
}
return raw;
}
function isTextNode(node) {
return node.type === 'Text';
}
function getAttributeValue(attributeName, node) {
var _a;
const attributes = ((_a = node.attributes) !== null && _a !== void 0 ? _a : []);
const langAttribute = attributes.find((attribute) => attribute.name === attributeName);
return langAttribute && langAttribute.value;
}
function getAttributeTextValue(attributeName, node) {
const value = getAttributeValue(attributeName, node);
if (value != null && typeof value === 'object') {
const textValue = value.find(isTextNode);
if (textValue) {
return textValue.data;
}
}
return null;
}
function getLangAttribute(node) {
const value = getAttributeTextValue('lang', node) || getAttributeTextValue('type', node);
if (value != null) {
return value.replace(/^text\//, '');
}
else {
return null;
}
}
/**
* Checks whether the node contains a `lang` or `type` attribute with a value corresponding to
* a language we cannot format. This might for example be `<template lang="pug">`.
* If the node does not contain a `lang` attribute, the result is true.
*/
function isNodeSupportedLanguage(node) {
const lang = getLangAttribute(node);
return !(lang && unsupportedLanguages.includes(lang));
}
/**
* Checks whether the node contains a `lang` or `type` attribute which indicates that
* the script contents are written in TypeScript. Note that the absence of the tag
* does not mean it's not TypeScript, because the user could have set the default
* to TypeScript in his settings.
*/
function isTypeScript(node) {
const lang = getLangAttribute(node) || '';
return ['typescript', 'ts'].includes(lang);
}
function isJSON(node) {
const lang = getLangAttribute(node) || '';
// https://github.com/prettier/prettier/pull/6293
return lang.endsWith('json') || lang.endsWith('importmap');
}
function isLess(node) {
const lang = getLangAttribute(node) || '';
return ['less'].includes(lang);
}
function isScss(node) {
const lang = getLangAttribute(node) || '';
return ['sass', 'scss'].includes(lang);
}
function isPugTemplate(node) {
return node.type === 'Element' && node.name === 'template' && getLangAttribute(node) === 'pug';
}
function isLoneMustacheTag(node) {
return node !== true && node.length === 1 && node[0].type === 'MustacheTag';
}
function isAttributeShorthand(node) {
return node !== true && node.length === 1 && node[0].type === 'AttributeShorthand';
}
/**
* True if node is of type `{a}` or `a={a}`
*/
function isOrCanBeConvertedToShorthand(node) {
if (isAttributeShorthand(node.value)) {
return true;
}
if (isLoneMustacheTag(node.value)) {
const expression = node.value[0].expression;
return expression.type === 'Identifier' && expression.name === node.name;
}
return false;
}
function getUnencodedText(node) {
// `raw` will contain HTML entities in unencoded form
return node.raw || node.data;
}
function isTextNodeStartingWithLinebreak(node, nrLines = 1) {
return node.type === 'Text' && startsWithLinebreak(getUnencodedText(node), nrLines);
}
function startsWithLinebreak(text, nrLines = 1) {
return new RegExp(`^([\\t\\f\\r ]*\\n){${nrLines}}`).test(text);
}
function isTextNodeEndingWithLinebreak(node, nrLines = 1) {
return node.type === 'Text' && endsWithLinebreak(getUnencodedText(node), nrLines);
}
function endsWithLinebreak(text, nrLines = 1) {
return new RegExp(`(\\n[\\t\\f\\r ]*){${nrLines}}$`).test(text);
}
function isTextNodeStartingWithWhitespace(node) {
return node.type === 'Text' && /^\s/.test(getUnencodedText(node));
}
function isTextNodeEndingWithWhitespace(node) {
return node.type === 'Text' && /\s$/.test(getUnencodedText(node));
}
function trimTextNodeRight(node) {
node.raw = node.raw && node.raw.trimRight();
node.data = node.data && node.data.trimRight();
}
function trimTextNodeLeft(node) {
node.raw = node.raw && node.raw.trimLeft();
node.data = node.data && node.data.trimLeft();
}
/**
* Remove all leading whitespace up until the first non-empty text node,
* and all trailing whitespace from the last non-empty text node onwards.
*/
function trimChildren(children, path) {
let firstNonEmptyNode = children.findIndex((n) => !isEmptyTextNode(n) && !doesEmbedStartAfterNode(n, path));
firstNonEmptyNode = firstNonEmptyNode === -1 ? children.length - 1 : firstNonEmptyNode;
let lastNonEmptyNode = findLastIndex((n, idx) => {
// Last node is ok to end at the start of an embedded region,
// if it's not a comment (which should stick to the region)
return (!isEmptyTextNode(n) &&
((idx === children.length - 1 && n.type !== 'Comment') ||
!doesEmbedStartAfterNode(n, path)));
}, children);
lastNonEmptyNode = lastNonEmptyNode === -1 ? 0 : lastNonEmptyNode;
for (let i = 0; i <= firstNonEmptyNode; i++) {
const n = children[i];
if (n.type === 'Text') {
trimTextNodeLeft(n);
}
}
for (let i = children.length - 1; i >= lastNonEmptyNode; i--) {
const n = children[i];
if (n.type === 'Text') {
trimTextNodeRight(n);
}
}
}
/**
* Check if given node's start tag should hug its first child. This is the case for inline elements when there's
* no whitespace between the `>` and the first child.
*/
function shouldHugStart(node, isSupportedLanguage, options) {
if (!isSupportedLanguage) {
return true;
}
if (isBlockElement(node, options)) {
return false;
}
if (!isNodeWithChildren(node)) {
return false;
}
const children = node.children;
if (children.length === 0) {
return true;
}
if (options.htmlWhitespaceSensitivity === 'ignore') {
return false;
}
const firstChild = children[0];
return !isTextNodeStartingWithWhitespace(firstChild);
}
/**
* Check if given node's end tag should hug its last child. This is the case for inline elements when there's
* no whitespace between the last child and the `</`.
*/
function shouldHugEnd(node, isSupportedLanguage, options) {
if (!isSupportedLanguage) {
return true;
}
if (isBlockElement(node, options)) {
return false;
}
if (!isNodeWithChildren(node)) {
return false;
}
const children = node.children;
if (children.length === 0) {
return true;
}
if (options.htmlWhitespaceSensitivity === 'ignore') {
return false;
}
const lastChild = children[children.length - 1];
return !isTextNodeEndingWithWhitespace(lastChild);
}
/**
* Check for a svelte block if there's whitespace at the start and if it's a space or a line.
*/
function checkWhitespaceAtStartOfSvelteBlock(node, options) {
if (!isSvelteBlock(node) || !isNodeWithChildren(node)) {
return 'none';
}
const children = node.children;
if (children.length === 0) {
return 'none';
}
const firstChild = children[0];
if (isTextNodeStartingWithLinebreak(firstChild)) {
return 'line';
}
else if (isTextNodeStartingWithWhitespace(firstChild)) {
return 'space';
}
// This extra check is necessary because the Svelte AST might swallow whitespace between
// the block's starting end and its first child.
const parentOpeningEnd = options.originalText.lastIndexOf('}', firstChild.start);
if (parentOpeningEnd > 0 && firstChild.start > parentOpeningEnd + 1) {
const textBetween = options.originalText.substring(parentOpeningEnd + 1, firstChild.start);
if (textBetween.trim() === '') {
return startsWithLinebreak(textBetween) ? 'line' : 'space';
}
}
return 'none';
}
/**
* Check for a svelte block if there's whitespace at the end and if it's a space or a line.
*/
function checkWhitespaceAtEndOfSvelteBlock(node, options) {
if (!isSvelteBlock(node) || !isNodeWithChildren(node)) {
return 'none';
}
const children = node.children;
if (children.length === 0) {
return 'none';
}
const lastChild = children[children.length - 1];
if (isTextNodeEndingWithLinebreak(lastChild)) {
return 'line';
}
else if (isTextNodeEndingWithWhitespace(lastChild)) {
return 'space';
}
// This extra check is necessary because the Svelte AST might swallow whitespace between
// the last child and the block's ending start.
const parentClosingStart = options.originalText.indexOf('{', lastChild.end);
if (parentClosingStart > 0 && lastChild.end < parentClosingStart) {
const textBetween = options.originalText.substring(lastChild.end, parentClosingStart);
if (textBetween.trim() === '') {
return endsWithLinebreak(textBetween) ? 'line' : 'space';
}
}
return 'none';
}
function isInsideQuotedAttribute(path, options) {
const stack = path.stack;
return stack.some((node) => node.type === 'Attribute' &&
(!isLoneMustacheTag(node.value) ||
(options.svelteStrictMode && !options._svelte_is5Plus)));
}
/**
* Returns true if the softline between `</tagName` and `>` can be omitted.
*/
function canOmitSoftlineBeforeClosingTag(node, path, options) {
return (isBracketSameLine(options) &&
(!hugsStartOfNextNode(node, options) || isLastChildWithinParentBlockElement(path, options)));
}
/**
* Return true if given node does not hug the next node, meaning there's whitespace
* or the end of the doc afterwards.
*/
function hugsStartOfNextNode(node, options) {
if (node.end === options.originalText.length) {
// end of document
return false;
}
return !options.originalText.substring(node.end).match(/^\s/);
}
function isLastChildWithinParentBlockElement(path, options) {
const parent = path.getParentNode();
if (!parent || !isBlockElement(parent, options)) {
return false;
}
const children = getChildren(parent);
const lastChild = children[children.length - 1];
return lastChild === path.getNode();
}
function assignCommentsToNodes(ast) {
if (ast.module) {
ast.module.comments = removeAndGetLeadingComments(ast, ast.module);
}
if (ast.instance) {
ast.instance.comments = removeAndGetLeadingComments(ast, ast.instance);
}
if (ast.css) {
ast.css.comments = removeAndGetLeadingComments(ast, ast.css);
}
}
/**
* Returns the comments that are above the current node and deletes them from the html ast.
*/
function removeAndGetLeadingComments(ast, current) {
const siblings = getChildren(ast.html);
const comments = [];
const newlines = [];
if (!siblings.length) {
return [];
}
let node = current;
let prev = siblings.find((child) => child.end === node.start);
while (prev) {
if (prev.type === 'Comment' &&
!isIgnoreStartDirective(prev) &&
!isIgnoreEndDirective(prev)) {
comments.push(prev);
if (comments.length !== newlines.length) {
newlines.push({ type: 'Text', data: '', raw: '', start: -1, end: -1 });
}
}
else if (isEmptyTextNode(prev)) {
newlines.push(prev);
}
else {
break;
}
node = prev;
prev = siblings.find((child) => child.end === node.start);
}
newlines.length = comments.length; // could be one more if first comment is preceeded by empty text node
for (const comment of comments) {
siblings.splice(siblings.indexOf(comment), 1);
}
for (const text of newlines) {
siblings.splice(siblings.indexOf(text), 1);
}
return comments
.map((comment, i) => ({
comment,
emptyLineAfter: getUnencodedText(newlines[i]).split('\n').length > 2,
}))
.reverse();
}
const { join, line, group, indent, dedent, softline, hardline, fill, breakParent, literalline } = doc.builders;
function hasPragma(text) {
return /^\s*<!--\s*@(format|prettier)\W/.test(text);
}
let ignoreNext = false;
let ignoreRange = false;
let svelteOptionsDoc;
function print(path, options, print) {
var _a, _b;
const bracketSameLine = isBracketSameLine(options);
const n = path.getValue();
if (!n) {
return '';
}
if (isASTNode(n)) {
return printTopLevelParts(n, options, path, print);
}
const [open, close] = options.svelteStrictMode && !options._svelte_is5Plus ? ['"{', '}"'] : ['{', '}'];
const printJsExpression = () => [open, printJS(path, print, 'expression'), close];
const node = n;
if ((ignoreNext || (ignoreRange && !isIgnoreEndDirective(node))) &&
(node.type !== 'Text' || !isEmptyTextNode(node))) {
if (ignoreNext) {
ignoreNext = false;
}
return flatten(options.originalText
.slice(options.locStart(node), options.locEnd(node))
.split('\n')
.map((o, i) => (i == 0 ? [o] : [literalline, o])));
}
switch (node.type) {
case 'Fragment':
const children = node.children;
if (children.length === 0 || children.every(isEmptyTextNode)) {
return '';
}
if (!isPreTagContent(path)) {
trimChildren(node.children, path);
const output = trim([printChildren(path, print, options)], (n) => isLine(n) ||
(typeof n === 'string' && n.trim() === '') ||
// Because printChildren may append this at the end and
// may hide other lines before it
n === breakParent);
if (output.every((doc) => isEmptyDoc(doc))) {
return '';
}
return group([...output, hardline]);
}
else {
return group(path.map(print, 'children'));
}
case 'Text':
if (!isPreTagContent(path)) {
if (isEmptyTextNode(node)) {
const hasWhiteSpace = getUnencodedText(node).trim().length < getUnencodedText(node).length;
const hasOneOrMoreNewlines = /\n/.test(getUnencodedText(node));
const hasTwoOrMoreNewlines = /\n\r?\s*\n\r?/.test(getUnencodedText(node));
if (hasTwoOrMoreNewlines) {
return [hardline, hardline];
}
if (hasOneOrMoreNewlines) {
return hardline;
}
if (hasWhiteSpace) {
return line;
}
return '';
}
/**
* For non-empty text nodes each sequence of non-whitespace characters (effectively,
* each "word") is joined by a single `line`, which will be rendered as a single space
* until this node's current line is out of room, at which `fill` will break at the
* most convenient instance of `line`.
*/
return fill(splitTextToDocs(node));
}
else {
let rawText = getUnencodedText(node);
const parent = path.getParentNode();
if (parent.type === 'Attribute') {
// Direct child of attribute value -> add literallines at end of lines
// so that other things don't break in unexpected places
if (parent.name === 'class' && path.getParentNode(1).type === 'Element') {
// Special treatment for class attribute on html elements. Prettier
// will force everything into one line, we deviate from that and preserve lines.
rawText = rawText.replace(/([^ \t\n])(([ \t]+$)|([ \t]+(\r?\n))|[ \t]+)/g,
// Remove trailing whitespace in lines with non-whitespace characters
// except at the end of the string
(match, characterBeforeWhitespace, _, isEndOfString, isEndOfLine, endOfLine) => isEndOfString
? match
: characterBeforeWhitespace + (isEndOfLine ? endOfLine : ' '));
// Shrink trailing whitespace in case it's followed by a mustache tag
// and remove it completely if it's at the end of the string, but not
// if it's on its own line
rawText = rawText.replace(/([^ \t\n])[ \t]+$/, parent.value.indexOf(node) === parent.value.length - 1 ? '$1' : '$1 ');
}
return replaceEndOfLineWith(rawText, literalline);
}
return rawText;
}
case 'Element':
case 'InlineComponent':
case 'Slot':
case 'SlotTemplate':
case 'Window':
case 'Head':
case 'Title': {
const isSupportedLanguage = !(node.name === 'template' && !isNodeSupportedLanguage(node));
const isEmpty = node.children.every((child) => isEmptyTextNode(child));
const isDoctypeTag = node.name.toUpperCase() === '!DOCTYPE';
const didSelfClose = options.originalText[node.end - 2] === '/';
const isSelfClosingTag = isEmpty &&
((((node.type === 'Element' && !options.svelteStrictMode) ||
node.type === 'Head' ||
node.type === 'InlineComponent' ||
node.type === 'Slot' ||
node.type === 'SlotTemplate' ||
node.type === 'Title') &&
didSelfClose) ||
node.type === 'Window' ||
selfClosingTags.indexOf(node.name) !== -1 ||
isDoctypeTag);
// Order important: print attributes first
const attributes = path.map(printWithPrependedAttributeLine(node, options, print), 'attributes');
const attributeLine = getAttributeLine(node, options);
const possibleThisBinding = node.type === 'InlineComponent' && node.expression
? [attributeLine, 'this=', ...printJsExpression()]
: node.type === 'Element' && node.tag
? [
attributeLine,
'this=',
...(typeof node.tag === 'string'
? [`"${node.tag}"`]
: [open, printJS(path, print, 'tag'), close]),
]
: '';
if (isSelfClosingTag) {
return group([
'<',
node.name,
indent(group([
possibleThisBinding,
...attributes,
bracketSameLine || isDoctypeTag ? '' : dedent(line),
])),
...[bracketSameLine && !isDoctypeTag ? ' ' : '', `${isDoctypeTag ? '' : '/'}>`],
]);
}
const children = node.children;
const firstChild = children[0];
const lastChild = children[children.length - 1];
// Is a function which is invoked later because printChildren will manipulate child nodes
// which would wrongfully change the other checks about hugging etc done beforehand
let body;
const hugStart = shouldHugStart(node, isSupportedLanguage, options);
const hugEnd = shouldHugEnd(node, isSupportedLanguage, options);
if (isEmpty) {
body =
isInlineElement(path, options, node) &&
node.children.length &&
isTextNodeStartingWithWhitespace(node.children[0]) &&
!isPreTagContent(path)
? () => line
: () => (bracketSameLine ? softline : '');
}
else if (isPreTagContent(path)) {
body = () => printPre(node, options.originalText, path, print);
}
else if (!isSupportedLanguage) {
body = () => printRaw(node, options.originalText, true);
}
else if (isInlineElement(path, options, node) && !isPreTagContent(path)) {
body = () => printChildren(path, print, options);
}
else {
body = () => printChildren(path, print, options);
}
const openingTag = [
'<',
node.name,
indent(group([
possibleThisBinding,
...attributes,
hugStart && !isEmpty
? ''
: !bracketSameLine && !isPreTagContent(path)
? dedent(softline)
: '',
])),
];
if (!isSupportedLanguage && !isEmpty) {
// Format template tags so that there's a hardline but no indention.
// That way the `lang="X"` and the closing `>` of the start tag stay in one line
// which is the 99% use case.
return group([
...openingTag,
'>',
group([hardline, body(), hardline]),
`</${node.name}>`,
]);
}
if (hugStart && hugEnd) {
const huggedContent = [softline, group(['>', body(), `</${node.name}`])];
const omitSoftlineBeforeClosingTag = (isEmpty && !bracketSameLine) ||
canOmitSoftlineBeforeClosingTag(node, path, options);
return group([
...openingTag,
isEmpty ? group(huggedContent) : group(indent(huggedContent)),
omitSoftlineBeforeClosingTag ? '' : softline,
'>',
]);
}
// No hugging of content means it's either a block element and/or there's whitespace at the start/end
let noHugSeparatorStart = softline;
let noHugSeparatorEnd = softline;
if (isPreTagContent(path)) {
noHugSeparatorStart = '';
noHugSeparatorEnd = '';
}
else {
let didSetEndSeparator = false;
if (!hugStart && firstChild && firstChild.type === 'Text') {
if (isTextNodeStartingWithLinebreak(firstChild) &&
firstChild !== lastChild &&
(!isInlineElement(path, options, node) ||
isTextNodeEndingWithWhitespace(lastChild))) {
noHugSeparatorStart = hardline;
noHugSeparatorEnd = hardline;
didSetEndSeparator = true;
}
else if (isInlineElement(path, options, node)) {
noHugSeparatorStart = line;
}
trimTextNodeLeft(firstChild);
}
if (!hugEnd && lastChild && lastChild.type === 'Text') {
if (isInlineElement(path, options, node) && !didSetEndSeparator) {
noHugSeparatorEnd = line;
}
trimTextNodeRight(lastChild);
}
}
if (hugStart) {
return group([
...openingTag,
indent([softline, group(['>', body()])]),
noHugSeparatorEnd,
`</${node.name}>`,
]);
}
if (hugEnd) {
return group([
...openingTag,
'>',
indent([noHugSeparatorStart, group([body(), `</${node.name}`])]),
canOmitSoftlineBeforeClosingTag(node, path, options) ? '' : softline,
'>',
]);
}
if (isEmpty) {
return group([...openingTag, '>', body(), `</${node.name}>`]);
}
return group([
...openingTag,
'>',
indent([noHugSeparatorStart, body()]),
noHugSeparatorEnd,
`</${node.name}>`,
]);
}
case 'Options':
if (options.svelteSortOrder !== 'none') {
throw new Error('Options tags should have been handled by prepareChildren');
}
// else fall through to Body
case 'Body':
case 'Document':
return group([
'<',
node.name,
indent(group([
...path.map(printWithPrependedAttributeLine(node, options, print), 'attributes'),
bracketSameLine ? '' : dedent(line),
])),
...[bracketSameLine ? ' ' : '', '/>'],
]);
case 'Document':
return group([
'<',
node.name,
indent(group([
...path.map(printWithPrependedAttributeLine(node, options, print), 'attributes'),
bracketSameLine ? '' : dedent(line),
])),
...[bracketSameLine ? ' ' : '', '/>'],
]);
case 'Identifier':
return node.name;
case 'AttributeShorthand': {
return node.expression.name;
}
case 'Attribute': {
if (isOrCanBeConvertedToShorthand(node)) {
if (options.svelteAllowShorthand) {
return ['{', node.name, '}'];
}
else {
return [node.name, `=${open}`, node.name, close];
}
}
else {
if (node.value === true) {
return [node.name];
}
const quotes = !isLoneMustacheTag(node.value) ||
((_a = (options.svelteStrictMode && !options._svelte_is5Plus)) !== null && _a !== void 0 ? _a : false);
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
if (quotes) {
return [node.name, '=', '"', attrNodeValue, '"'];
}
else {
return [node.name, '=', attrNodeValue];
}
}
}
case 'MustacheTag':
return ['{', printJS(path, print, 'expression'), '}'];
case 'IfBlock': {
const def = [
'{#if ',
printJS(path, print, 'expression'),
'}',
printSvelteBlockChildren(path, print, options),
];
if (node.else) {
def.push(path.call(print, 'else'));
}
def.push('{/if}');
return group([def, breakParent]);
}
case 'ElseBlock': {
// Else if
const parent = path.getParentNode();
if (node.children.length === 1 &&
node.children[0].type === 'IfBlock' &&
parent.type !== 'EachBlock') {
const ifNode = node.children[0];
const def = [
'{:else if ',
path.map((ifPath) => printJS(ifPath, print, 'expression'), 'children')[0],
'}',
path.map((ifPath) => printSvelteBlockChildren(ifPath, print, options), 'children')[0],
];
if (ifNode.else) {
def.push(path.map((ifPath) => ifPath.call(print, 'else'), 'children')[0]);
}
return def;
}
return ['{:else}', printSvelteBlockChildren(path, print, options)];
}
case 'EachBlock': {
const def = [
'{#each ',
printJS(path, print, 'expression'),
' as',
expandNode(node.context, options.originalText),
];
if (node.index) {
def.push(', ', node.index);
}
if (node.key) {
def.push(' (', printJS(path, print, 'key'), ')');
}
def.push('}', printSvelteBlockChildren(path, print, options));
if (node.else) {
def.push(path.call(print, 'else'));
}
def.push('{/each}');
return group([def, breakParent]);
}
case 'AwaitBlock': {
const hasPendingBlock = node.pending.children.some((n) => !isEmptyTextNode(n));
const hasThenBlock = node.then.children.some((n) => !isEmptyTextNode(n));
const hasCatchBlock = node.catch.children.some((n) => !isEmptyTextNode(n));
let block = [];
if (!hasPendingBlock && hasThenBlock) {
block.push(group([
'{#await ',
printJS(path, print, 'expression'),
' then',
expandNode(node.value, options.originalText),
'}',
]), path.call(print, 'then'));
}
else if (!hasPendingBlock && hasCatchBlock) {
block.push(group([
'{#await ',
printJS(path, print, 'expression'),
' catch',
expandNode(node.error, options.originalText),
'}',
]), path.call(print, 'catch'));
}
else {
block.push(group(['{#await ', printJS(path, print, 'expression'), '}']));
if (hasPendingBlock) {
block.push(path.call(print, 'pending'));
}
if (hasThenBlock) {
block.push(group(['{:then', expandNode(node.value, options.originalText), '}']), path.call(print, 'then'));
}
}
if ((hasPendingBlock || hasThenBlock) && hasCatchBlock) {
block.push(group(['{:catch', expandNode(node.error, options.originalText), '}']), path.call(print, 'catch'));
}
block.push('{/await}');
return group(block);
}
case 'KeyBlock': {
const def = [
'{#key ',
printJS(path, print, 'expression'),
'}',
printSvelteBlockChildren(path, print, options),
];
def.push('{/key}');
return group([def, breakParent]);
}
case 'ThenBlock':
case 'PendingBlock':
case 'CatchBlock':
return printSvelteBlockChildren(path, print, options);
// Svelte 5 only
case 'SnippetBlock': {
const snippet = ['{#snippet ', printJS(path, print, 'expression')];
snippet.push('}', printSvelteBlockChildren(path, print, options), '{/snippet}');
return snippet;
}
case 'EventHandler':
return [
'on:',
node.name,
node.modifiers && node.modifiers.length ? ['|', join('|', node.modifiers)] : '',
node.expression ? ['=', ...printJsExpression()] : '',
];
case 'Binding':
return [
'bind:',
node.name,
node.expression.type === 'Identifier' &&
node.expression.name === node.name &&
options.svelteAllowShorthand
? ''
: ['=', ...printJsExpression()],
];
case 'Class':
return [
'class:',
node.name,
node.expression.type === 'Identifier' &&
node.expression.name === node.name &&
options.svelteAllowShorthand
? ''
: ['=', ...printJsExpression()],
];
case 'StyleDirective':
const prefix = [
'style:',
node.name,
node.modifiers && node.modifiers.length ? ['|', join('|', node.modifiers)] : '',
];
if (isOrCanBeConvertedToShorthand(node) || node.value === true) {
if (options.svelteAllowShorthand) {
return [...prefix];
}
else {
return [...prefix, `=${open}`, node.name, close];
}
}
else {
const quotes = !isLoneMustacheTag(node.value) ||
((_b = (options.svelteStrictMode && !options._svelte_is5Plus)) !== null && _b !== void 0 ? _b : false);
const attrNodeValue = printAttributeNodeValue(path, print, quotes, node);
if (quotes) {
return [...prefix, '=', '"', attrNodeValue, '"'];
}
else {
return [...prefix, '=', attrNodeValue];
}
}
case 'Let':
return [
'let:',
node.name,
// shorthand let directives have `null` expressions
!node.expression ||
(node.expression.type === 'Identifier' && node.expression.name === node.name)
? ''
: ['=', ...printJsExpression()],
];
case 'DebugTag':
return [
'{@debug',
node.identifiers.length > 0
? [' ', join(', ', path.map(print, 'identifiers'))]
: '',
'}',
];
case 'Ref':
return ['ref:', node.name];
case 'Comment': {
const nodeAfterComment = getNextNode(path);
if (isIgnoreStartDirective(node) && isNodeTopLevelHTML(node, path)) {
ignoreRange = true;
}
else if (isIgnoreEndDirective(node) && isNodeTopLevelHTML(node, path)) {
ignoreRange = false;
}
else if (
// If there is no sibling node that starts right after us but the parent indicates
// that there used to be, that means that node was actually an embedded `<style>`
// or `<script>` node that was cut out.
// If so, the comment does not refer to the next line we will see.
// The `embed` function handles printing the comment in the right place.
doesEmbedStartAfterNode(node, path) ||
(isEmptyTextNode(nodeAfterComment) &&
doesEmbedStartAfterNode(nodeAfterComment, path))) {
return '';
}
else if (isIgnoreDirective(node)) {
ignoreNext = true;
}
return printComment(node);
}
case 'Transition':
const kind = node.intro && node.outro ? 'transition' : node.intro ? 'in' : 'out';
return [
kind,
':',
node.name,
node.modifiers && node.modifiers.length ? ['|', join('|', node.modifiers)] : '',
node.expression ? ['=', ...printJsExpression()] : '',
];
case 'Action':
return ['use:', node.name, node.expression ? ['=', ...printJsExpression()] : ''];
case 'Animation':
return ['animate:', node.name, node.expression ? ['=', ...printJsExpression()] : ''];
case 'RawMustacheTag':
return ['{@html ', printJS(path, print, 'expression'), '}'];
// Svelte 5 only
case 'RenderTag': {
const render = ['{@render ', printJS(path, print, 'expression'), '}'];
return render;
}
case 'Spread':
return ['{...', printJS(path, print, 'expression'), '}'];
case 'ConstTag':
return ['{@const ', printJS(path, print, 'expression'), '}'];
}
console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
}
function printTopLevelParts(n, options, path, print) {
if (options.svelteSortOrder === 'none') {
const topLevelPartsByEnd = {};
if (n.module) {
topLevelPartsByEnd[n.module.end] = n.module;
}
if (n.instance) {
topLevelPartsByEnd[n.instance.end] = n.instance;
}
if (n.css) {
topLevelPartsByEnd[n.css.end] = n.css;
}
const children = getChildren(n.html);
for (let i = 0; i < children.length; i++) {
const node = children[i];
if (topLevelPartsByEnd[node.start]) {
children.splice(i, 0, topLevelPartsByEnd[node.start]);
delete topLevelPartsByEnd[node.start];
}
}
const result = path.call(print, 'html');
if (options.insertPragma && !hasPragma(options.originalText)) {
return [`<!-- @format -->`, hardline, result];
}
else {
return result;
}
}
const parts = {
options: [],
scripts: [],
markup: [],
styles: [],
};
// scripts
if (n.module) {
parts.scripts.push(path.call(print, 'module'));
}
if (n.instance) {
parts.scripts.push(path.call(print, 'instance'));
}
// styles
if (n.css) {
parts.styles.push(path.call(print, 'css'));
}
// markup
const htmlDoc = path.call(print, 'html');
if (htmlDoc) {
parts.markup.push(htmlDoc);
}
if (svelteOptionsDoc) {
parts.options.push(svelteOptionsDoc);
}
const docs = flatten(parseSortOrder(options.svelteSortOrder).map((p) => parts[p]));
// Need to reset these because they are global and could affect the next formatting run
ignoreNext = false;
ignoreRange = false;
svelteOptionsDoc = undefined;
// If this is invoked as an embed of markdown, remove the last hardline.
// The markdown parser tries this, too, but fails because it does not
// recurse into concats. Doing this will prevent an empty line
// at the end of the embedded code block.
if (options.parentParser === 'markdown') {
const lastDoc = docs[docs.length - 1];
trimRight([lastDoc], isLine);
}
if (options.insertPragma && !hasPragma(options.originalText)) {
return [`<!-- @format -->`, hardline, group(docs)];
}
else {
return group([join(hardline, docs)]);
}
}
function printAttributeNodeValue(path, print, quotes, node) {
const valueDocs = path.map((childPath) => childPath.call(print), 'value');
if (!quotes || !formattableAttributes.includes(node.name)) {
return valueDocs;
}
else {
return indent(group(trim(valueDocs, isLine)));
}
}
function printSvelteBlockChildren(path, print, options) {
const node = path.getValue();
const children = node.children;
if (!children || children.length === 0) {
return '';
}
const whitespaceAtStartOfBlock = checkWhitespaceAtStartOfSvelteBlock(node, options);
const whitespaceAtEndOfBlock = checkWhitespaceAtEndOfSvelteBlock(node, options);
const startline = whitespaceAtStartOfBlock === 'none'
? ''
: whitespaceAtEndOfBlock === 'line' || whitespaceAtStartOfBlock === 'line'
? hardline
: line;
const endline = whitespaceAtEndOfBlock === 'none'
? ''
: whitespaceAtEndOfBlock === 'line' || whitespaceAtStartOfBlock === 'line'
? hardline
: line;
const firstChild = children[0];
const lastChild = children[children.length - 1];
if (isTextNodeStartingWithWhitespace(firstChild)) {
trimTextNodeLeft(firstChild);
}
if (isTextNodeEndingWithWhitespace(lastChild)) {
trimTextNodeRight(lastChild);
}
return [indent([startline, group(printChildren(path, print, options))]), endline];
}
function printPre(node, originalText, path, print) {
const result = [];
const length = node.children.length;
for (let i = 0; i < length; i++) {
const child = node.children[i];
if (child.type === 'Text') {
const lines = originalText.substring(child.start, child.end).split(/\r?\n/);
lines.forEach((line, j) => {
if (j > 0)
result.push(literalline);
result.push(line);
});
}
else {
result.push(path.call(print, 'children', i));
}
}
return result;
}
function printChildren(path, print, options) {
if (isPreTagContent(path)) {
return path.map(print, 'children');
}
const childNodes = prepareChildren(path.getValue().children, path, print, options);
// modify original array because it's accessed later through map(print, 'children', idx)
path.getValue().children = childNodes;
if (childNodes.length === 0) {
return '';
}
const childDocs = [];
let handleWhitespaceOfPrevTextNode = false;
for (let i = 0; i < childNodes.length; i++) {
const childNode = childNodes[i];
if (childNode.type === 'Text') {
handleTextChild(i, childNode);
}
else if (isBlockElement(childNode, options)) {
handleBlockChild(i);
}
else if (isInlineElement(path, options, childNode)) {
handleInlineChild(i);
}
else {
childDocs.push(printChild(i));
handleWhitespaceOfPrevTextNode = false;
}
}
// If there's at least one block element and more than one node, break content
const forceBreakContent = childNodes.length > 1 && childNodes.some((child) => isBlockElement(child, options));
if (forceBreakContent) {
childDocs.push(breakParent);
}
return childDocs;
function printChild(idx) {
return path.call(print, 'children', idx);
}
/**
* Print inline child. Hug whitespace of previous text child if there was one.
*/
function handleInlineChild(idx) {
if (handleWhitespaceOfPrevTextNode) {
childDocs.push(group([line, printChild(idx)]));
}
else {
childDocs.push(printChild(idx));
}
handleWhitespaceOfPrevTextNode = false;
}
/**
* Print block element. Add softlines around it if needed
* so it breaks into a separate line if children are broken up.
* Don't add lines at the start/end if it's the first/last child because this
* kind of whitespace handling is done in the parent already.
*/
function handleBlockChild(idx) {
const prevChild = childNodes[idx - 1];
if (prevChild &&
!isBlockElement(prevChild, options) &&
(prevChild.type !== 'Text' ||
handleWhitespaceOfPrevTextNode ||
!isTextNodeEndingWithWhitespace(prevChild))) {
childDocs.push(softline);
}
childDocs.push(printChild(idx));
const nextChild = childNodes[idx + 1];
if (nextChild &&
(nextChild.type !== 'Text' ||
// Only handle text which starts with a whitespace and has text afterwards,
// or is empty but followed by an inline element. The latter is done
// so that if the children break, the inline element afterwards is in a separate line.
((!isEmptyTextNode(nextChild) ||
(childNodes[idx + 2] && isInlineElement(path, options, childNodes[idx + 2]))) &&
!isTextNodeStartingWithLinebreak(nextChild)))) {
childDocs.push(softline);
}
handleWhitespaceOfPrevTextNode = false;
}
/**
* Print text child. First/last child white space handling
* is done in parent already. By definition of the Svelte AST,
* a text node always is inbetween other tags. Add hardlines
* if the users wants to have them inbetween.
* If the text is trimmed right, toggle flag telling
* subsequent (inline)block element to alter its printing logic
* to check if they need to hug or print lines themselves.
*/
function handleTextChild(idx, childNode) {
handleWhitespaceOfPrevTextNode = false;
if (idx === 0 || idx === childNodes.length - 1) {
childDocs.push(printChild(idx));
return;
}
const prevNode = childNodes[idx - 1];
const nextNode = childNodes[idx + 1];
if (isTextNodeStartingWithWhitespace(childNode) &&
// If node is empty, go straight through to checking the right end
!isEmptyTextNode(childNode)) {
if (isInlineElement(path, options, prevNode) &&
!isTextNodeStartingWithLinebreak(childNode)) {
trimTextNodeLeft(childNode);
const lastChildDoc = childDocs.pop();
childDocs.push(group([lastChildDoc, line]));
}
if (isBlockElement(prevNode, options) && !isTextNodeStartingWithLinebreak(childNode)) {
trimTextNodeLeft(childNode);
}
}
if (isTextNodeEndingWithWhitespace(childNode)) {
if (isInlineElement(path, options, nextNode) &&
!isTextNodeEndingWithLinebreak(childNode)) {
handleWhitespaceOfPrevTextNode = !prevNode || !isBlockElement(prevNode, options);
trimTextNodeRight(childNode);
}
if (isBlockElement(nextNode, options) && !isTextNodeEndingWithLinebreak(childNode, 2)) {
handleWhitespaceOfPrevTextNode = !prevNode || !isBlockElement(prevNode, options);
trimTextNodeRight(childNode);
}
}
childDocs.push(printChild(idx));
}
}
/**
* `svelte:options` is part of the html part but needs to be snipped out and handled
* separately to reorder it as configured. The comment above it should be moved with it.
* Do that here.
*/
function prepareChildren(children, path, print, options) {
let svelteOptionsComment;
const childrenWithoutOptions = [];
const bracketSameLine = isBracketSameLine(options);
for (let idx = 0; idx < children.length; idx++) {
const currentChild = children[idx];
if (currentChild.type === 'Text' && getUnencodedText(currentChild) === '') {
continue;
}
if (isEmptyTextNode(currentChild) && doesEmbedStartAfterNode(currentChild, path)) {
continue;
}
if (options.svelteSortOrder !== 'none') {
if (isCommentFollowedByOptions(currentChild, idx)) {
svelteOptionsComment = printComment(currentChild);
const nextChild = children[idx + 1];
idx += nextChild && isEmptyTextNode(nextChild) ? 1 : 0;
continue;
}
if (currentChild.type === 'Options') {
printSvelteOptions(currentChild, idx, path, print);
continue;
}
}
childrenWithoutOptions.push(currentChild);
}
const mergedChildrenWithoutOptions = [];
for (let idx = 0; idx < childrenWithoutOptions.length; idx++) {
const currentChild = childrenWithoutOptions[idx];
const nextChild = childrenWithoutOptions[idx + 1];
if (currentChild.type === 'Text' && nextChild && nextChild.type === 'Text') {
// A tag was snipped out (f.e. svelte:options). Join text
currentChild.raw += nextChild.raw;
currentChild.data += nextChild.data;
idx++;
}
mergedChildrenWithoutOptions.push(currentChild);
}
return mergedChildrenWithoutOptions;
function printSvelteOptions(node, idx, path, print) {
svelteOptionsDoc = group([
[
'<',
node.name,
indent(group([
...path.map(printWithPrependedAttributeLine(node, options, print), 'children', idx, 'attributes'),
bracketSameLine ? '' : dedent(line),
])),
...[bracketSameLine ? ' ' : '', '/>'],
],
hardline,
]);
if (svelteOptionsComment) {
svelteOptionsDoc = group([svelteOptionsComment, hardline, svelteOptionsDoc]);
}
}
function isCommentFollowedByOptions(node, idx) {
if (node.type !== 'Comment' || isIgnoreEndDirective(node) || isIgnoreStartDirective(node)) {
return false;
}
const nextChild = children[idx + 1];
if (nextChild) {
if (isEmptyTextNode(nextChild)) {
const afterNext = children[idx + 2];
return afterNext && afterNext.type === 'Options';
}
return nextChild.type === 'Options';
}
return false;
}
}
/**
* Split the text into words separated by whitespace. Replace the whitespaces by lines,
* collapsing multiple whitespaces into a single line.
*
* If the text starts or ends with multiple newlines, two of those should be kept.
*/
function splitTextToDocs(node) {
const text = getUnencodedText(node);
const lines = text.split(/[\t\n\f\r ]+/);
let docs = join(line, lines).filter((doc) => doc !== '');
if (startsWithLinebreak(text)) {
docs[0] = hardline;
}
if (startsWithLinebreak(text, 2)) {
docs = [hardline, ...docs];
}
if (endsWithLinebreak(text)) {
docs[docs.length - 1] = hardline;
}
if (endsWithLinebreak(text, 2)) {
docs = [...docs, hardline];
}
return docs;
}
function printJS(path, print, name) {
return path.call(print, name);
}
function expandNode(node, original) {
let str = _expandNode(node);
if (node === null || node === void 0 ? void 0 : node.typeAnnotation) {
str += ': ' + original.slice(node.typeAnnotation.start, node.typeAnnotation.end);
}
return str;
}
function _expandNode(node, parent) {
if (node === null) {
return '';
}
if (typeof node === 'string') {
// pre-v3.20 AST
return ' ' + node;
}
switch (node.type) {
case 'ArrayExpression':
case 'ArrayPattern':
return ' [' + node.elements.map(_expandNode).join(',').slice(1) + ']';
case 'AssignmentPattern':
return _expandNode(node.left) + ' =' + _expandNode(node.right);
case 'Identifier':
return ' ' + node.name;
case 'Literal':
return ' ' + node.raw;
case 'ObjectExpression':
return ' {' + node.properties.map((p) => _expandNode(p, node)).join(',') + ' }';
case 'ObjectPattern':
return ' {' + node.properties.map(_expandNode).join(',') + ' }';
case 'Property':
if (node.value.type === 'ObjectPattern' || node.value.type === 'ArrayPattern') {
return ' ' + node.key.name + ':' + _expandNode(node.value);
}
else if ((node.value.type === 'Identifier' && node.key.name !== node.value.name) ||
(parent && parent.type === 'ObjectExpression')) {
return _expandNode(node.key) + ':' + _expandNode(node.value);
}
else {
return _expandNode(node.value);
}
case 'RestElement':
return ' ...' + node.argument.name;
}
console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
}
function printComment(node) {
let text = node.data;
if (hasSnippedContent(text)) {
text = unsnipContent(text);
}
return group(['<!--', text, '-->']);
}
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
function getText(node, options, unsnip = false) {
const leadingComments = node.leadingComments;
const text = options.originalText.slice(options.locStart(
// if there are comments before the node they are not included
// in the `start` of the node itself
(leadingComments && leadingComments[0]) || node), options.locEnd(node));
if (!unsnip || !hasSnippedContent(text)) {
return text;
}
return unsnipContent(text);
}
const extractAttributesRegex = /<[a-z]+((?:\s+[^=>'"\/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)|\s+[^=>'"\/]+)*\s*)>/im;
const attributeRegex = /([^\s=]+)(?:=(?:(?:("|')([\s\S]*?)\2)|(?:([^>\s]+?)(?:\s|>|$))))?/gim;
function extractAttributes(html) {
const [, attributesString] = html.match(extractAttributesRegex);
const attrs = [];
let match;
while ((match = attributeRegex.exec(attributesString))) {
const [all, name, quotes, valueQuoted, valueUnquoted] = match;
const value = valueQuoted || valueUnquoted;
const attrStart = match.index;
let valueNode;
if (!value) {
valueNode = true;
}
else {
let valueStart = attrStart + name.length;
if (quotes) {
valueStart += 2;
}
valueNode = [
{
type: 'Text',
data: value,
start: valueStart,
end: valueStart + value.length,
},
];
}
attrs.push({
type: 'Attribute',
name,
value: valueNode,
start: attrStart,
end: attrStart + all.length,
});
}
return attrs;
}
const { builders: { group: group$1, hardline: hardline$1, softline: softline$1, indent: indent$1, dedent: dedent$1, literalline: literalline$1 }, utils: { removeLines }, } = doc;
const leaveAlone = new Set([
'Script',
'Style',
'Identifier',
'MemberExpression',
'CallExpression',
'ArrowFunctionExpression',
]);
const dontTraverse = new Set(['start', 'end', 'type']);
function getVisitorKeys(node, nonTraversableKeys) {
return Object.keys(node).filter((key) => {
return !nonTraversableKeys.has(key) && !leaveAlone.has(node.type) && !dontTraverse.has(key);
});
}
// Embed works like this in Prettier v3:
// - do depth first traversal of all node properties
// - deepest property is calling embed first
// - if embed returns a function, it will be called after the traversal in a second pass, in the same order (deepest first)
// For performance reasons we try to only return functions when we're sure we need to transform something.
function embed(path, _options) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
const node = path.getNode();
const options = _options;
if (!options.locStart || !options.locEnd || !options.originalText) {
throw new Error('Missing required options');
}
if (isASTNode(node)) {
assignCommentsToNodes(node);
if (node.module) {
node.module.type = 'Script';
node.module.attributes = extractAttributes(getText(node.module, options));
}
if (node.instance) {
node.instance.type = 'Script';
node.instance.attributes = extractAttributes(getText(node.instance, options));
}
if (node.css) {
node.css.type = 'Style';
node.css.content.type = 'StyleProgram';
}
return null;
}
// embed does depth first traversal with deepest node called first, therefore we need to
// check the parent to see if we are inside an expression that should be embedded.
const parent = path.getParentNode();
const printJsExpression = () => {
var _a;
return parent.expression
? printJS$1(parent, (_a = (options.svelteStrictMode && !options._svelte_is5Plus)) !== null && _a !== void 0 ? _a : false, false, false, 'expression')
: undefined;
};
const printSvelteBlockJS = (name) => printJS$1(parent, false, true, false, name);
switch (parent.type) {
case 'IfBlock':
case 'ElseBlock':
case 'AwaitBlock':
case 'KeyBlock':
printSvelteBlockJS('expression');
break;
case 'EachBlock':
printSvelteBlockJS('expression');
printSvelteBlockJS('key');
break;
case 'SnippetBlock':
// We merge the two parts into one expression, which future-proofs this for template TS support
if (node === parent.expression) {
parent.expression.end =
options.originalText.indexOf(')', (_e = (_b = (_a = parent.context) === null || _a === void 0 ? void 0 : _a.end) !== null && _b !== void 0 ? _b : (_d = (_c = parent.parameters) === null || _c === void 0 ? void 0 : _c[parent.parameters.length - 1]) === null || _d === void 0 ? void 0 : _d.end) !== null && _e !== void 0 ? _e : parent.expression.end) + 1;
parent.context = null;
parent.parameters = null;
node.isJS = true;
node.asFunction = true;
}
break;
case 'Element':
printJS$1(parent, (_f = (options.svelteStrictMode && !options._svelte_is5Plus)) !== null && _f !== void 0 ? _f : false, false, false, 'tag');
break;
case 'MustacheTag':
printJS$1(parent, isInsideQuotedAttribute(path, options), false, false, 'expression');
break;
case 'RawMustacheTag':
printJS$1(parent, false, false, false, 'expression');
break;
case 'Spread':
printJS$1(parent, false, false, false, 'expression');
break;
case 'ConstTag':
printJS$1(parent, false, false, true, 'expression');
break;
case 'RenderTag':
if (node === parent.expression) {
// TODO: remove this if block at some point, snippet API changed in .next-..
if ('argument' in parent || 'arguments' in parent) {
parent.expression.end =
options.originalText.indexOf(')', (_l = (_h = (_g = parent.argument) === null || _g === void 0 ? void 0 : _g.end) !== null && _h !== void 0 ? _h : (_k = (_j = parent.arguments) === null || _j === void 0 ? void 0 : _j[parent.arguments.length - 1]) === null || _k === void 0 ? void 0 : _k.end) !== null && _l !== void 0 ? _l : parent.expression.end) + 1;
parent.argument = null;
parent.arguments = null;
}
printJS$1(parent, false, false, false, 'expression');
}
break;
case 'EventHandler':
case 'Binding':
case 'Class':
case 'Let':
case 'Transition':
case 'Action':
case 'Animation':
case 'InlineComponent':
printJsExpression();
break;
}
if (node.isJS) {
return (textToDoc) => __awaiter(this, void 0, void 0, function* () {
try {
const embeddedOptions = {
// Prettier only allows string references as parsers from v3 onwards,
// so we need to have another public parser and defer to that
parser: options._svelte_ts
? 'svelteTSExpressionParser'
: 'svelteExpressionParser',
singleQuote: node.forceSingleQuote ? true : options.singleQuote,
_svelte_asFunction: node.asFunction,
};
// If we have snipped content, it was done wrongly and we need to unsnip it.
// This happens for example for {@html `<script>{foo}</script>`}
const text = getText(node, options, true);
let docs = yield textToDoc(node.asFunction ? forceIntoFunction(text) : forceIntoExpression(text), embeddedOptions);
if (node.forceSingleLine) {
docs = removeLines(docs);
}
if (node.removeParentheses) {
docs = removeParentheses(docs);
}
if (node.asFunction) {
if (Array.isArray(docs) && typeof docs[0] === 'string') {
docs[0] = docs[0].replace('function ', '');
docs.splice(-1, 1);
}
else {
throw new Error('Prettier AST changed, asFunction logic needs to change');
}
}
return docs;
}
catch (e) {
return getText(node, options, true);
}
});
}
const embedType = (tag, parser, isTopLevel) => {
return (textToDoc, print) => __awaiter(this, void 0, void 0, function* () {
return embedTag(tag, options.originalText, path, (content) => formatBodyContent(content, parser, textToDoc, options), print, isTopLevel, options);
});
};
const embedScript = (isTopLevel) => embedType('script',
// Use babel-ts as fallback because the absence does not mean the content is not TS,
// the user could have set the default language. babel-ts will format things a little
// bit different though, especially preserving parentheses around dot notation which
// fixes https://github.com/sveltejs/prettier-plugin-svelte/issues/218
isTypeScript(node) ? 'typescript' : isJSON(node) ? 'json' : 'babel-ts', isTopLevel);
const embedStyle = (isTopLevel) => embedType('style', isLess(node) ? 'less' : isScss(node) ? 'scss' : 'css', isTopLevel);
const embedPug = () => embedType('template', 'pug', false);
switch (node.type) {
case 'Script':
return embedScript(true);
case 'Style':
return embedStyle(true);
case 'Element': {
if (node.name === 'script') {
return embedScript(false);
}
else if (node.name === 'style') {
return embedStyle(false);
}
else if (isPugTemplate(node)) {
return embedPug();
}
}
}
return null;
}
function forceIntoExpression(statement) {
// note the trailing newline: if the statement ends in a // comment,
// we can't add the closing bracket right afterwards
return `(${statement}\n)`;
}
function forceIntoFunction(statement) {
return `function ${statement} {}`;
}
function preformattedBody(str) {
if (!str) {
return '';
}
const firstNewline = /^[\t\f\r ]*\n/;
const lastNewline = /\n[\t\f\r ]*$/;
// If we do not start with a new line prettier might try to break the opening tag
// to keep it together with the string. Use a literal line to skip indentation.
return [literalline$1, str.replace(firstNewline, '').replace(lastNewline, ''), hardline$1];
}
function getSnippedContent(node) {
const encodedContent = getAttributeTextValue(snippedTagContentAttribute, node);
if (encodedContent) {
return base64ToString(encodedContent);
}
else {
return '';
}
}
function formatBodyContent(content, parser, textToDoc, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const body = yield textToDoc(content, { parser });
if (parser === 'pug' && typeof body === 'string') {
// Pug returns no docs but a final string.
// Therefore prepend the line offsets
const whitespace = options.useTabs
? '\t'
: ' '.repeat(options.pugTabWidth && options.pugTabWidth > 0
? options.pugTabWidth
: options.tabWidth);
const pugBody = body
.split('\n')
.map((line) => (line ? whitespace + line : line))
.join('\n');
return [hardline$1, pugBody];
}
const indentIfDesired = (doc) => options.svelteIndentScriptAndStyle ? indent$1(doc) : doc;
trimRight([body], isLine);
return [indentIfDesired([hardline$1, body]), hardline$1];
}
catch (error) {
if (process.env.PRETTIER_DEBUG) {
throw error;
}
// We will wind up here if there is a syntax error in the embedded code. If we throw an error,
// prettier will try to print the node with the printer. That will fail with a hard-to-interpret
// error message (e.g. "Unsupported node type", referring to `<script>`).
// Therefore, fall back on just returning the unformatted text.
console.error(error);
return preformattedBody(content);
}
});
}
function embedTag(tag, text, path, formatBodyContent, print, isTopLevel, options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const node = path.getNode();
const content = tag === 'template' ? printRaw(node, text) : getSnippedContent(node);
const previousComments = node.type === 'Script' || node.type === 'Style'
? node.comments
: [getLeadingComment(path)]
.filter(Boolean)
.map((comment) => ({ comment: comment, emptyLineAfter: false }));
const canFormat = isNodeSupportedLanguage(node) &&
!isIgnoreDirective((_a = previousComments[previousComments.length - 1]) === null || _a === void 0 ? void 0 : _a.comment) &&
(tag !== 'template' ||
options.plugins.some((plugin) => typeof plugin !== 'string' && plugin.parsers && plugin.parsers.pug));
const body = canFormat
? content.trim() !== ''
? yield formatBodyContent(content)
: content === ''
? ''
: hardline$1
: preformattedBody(content);
const openingTag = group$1([
'<',
tag,
indent$1(group$1([
...path.map(printWithPrependedAttributeLine(node, options, print), 'attributes'),
isBracketSameLine(options) ? '' : dedent$1(softline$1),
])),
'>',
]);
let result = group$1([openingTag, body, '</', tag, '>']);
const comments = [];
for (const comment of previousComments) {
comments.push('<!--', comment.comment.data, '-->');
comments.push(hardline$1);
if (comment.emptyLineAfter) {
comments.push(hardline$1);
}
}
if (isTopLevel && options.svelteSortOrder !== 'none') {
// top level embedded nodes have been moved from their normal position in the
// node tree. if there is a comment referring to it, it must be recreated at
// the new position.
return [...comments, result, hardline$1];
}
else {
// Only comments at the top level get the special "move comment" treatment.
return isTopLevel && comments.length ? [...comments, result] : result;
}
});
}
function printJS$1(node, forceSingleQuote, forceSingleLine, removeParentheses, name) {
const part = node[name];
if (!part || typeof part !== 'object') {
return;
}
part.isJS = true;
part.forceSingleQuote = forceSingleQuote;
part.forceSingleLine = forceSingleLine;
part.removeParentheses = removeParentheses;
}
const babelParser = parsers$1.babel;
const typescriptParser = parsers$1['babel-ts']; // TODO use TypeScript parser in next major?
const isSvelte5Plus = Number(VERSION.split('.')[0]) >= 5;
function locStart(node) {
return node.start;
}
function locEnd(node) {
return node.end;
}
const languages = [
{
name: 'svelte',
parsers: ['svelte'],
extensions: ['.svelte'],
vscodeLanguageIds: ['svelte'],
},
];
const parsers = {
svelte: {
hasPragma,
parse: (text) => {
try {
return Object.assign(Object.assign({}, parse(text)), { __isRoot: true });
}
catch (err) {
if (err.start != null && err.end != null) {
// Prettier expects error objects to have loc.start and loc.end fields.
// Svelte uses start and end directly on the error.
err.loc = {
start: err.start,
end: err.end,
};
}
throw err;
}
},
preprocess: (text, options) => {
const result = snipScriptAndStyleTagContent(text);
text = result.text.trim();
// Prettier sets the preprocessed text as the originalText in case
// the Svelte formatter is called directly. In case it's called
// as an embedded parser (for example when there's a Svelte code block
// inside markdown), the originalText is not updated after preprocessing.
// Therefore we do it ourselves here.
options.originalText = text;
// Only Svelte 5 can have TS in the template
options._svelte_ts = isSvelte5Plus && result.isTypescript;
options._svelte_is5Plus = isSvelte5Plus;
return text;
},
locStart,
locEnd,
astFormat: 'svelte-ast',
},
svelteExpressionParser: Object.assign(Object.assign({}, babelParser), { parse: (text, options) => {
const ast = babelParser.parse(text, options);
let program = ast.program.body[0];
if (!options._svelte_asFunction) {
program = program.expression;
}
return Object.assign(Object.assign({}, ast), { program });
} }),
svelteTSExpressionParser: Object.assign(Object.assign({}, typescriptParser), { parse: (text, options) => {
const ast = typescriptParser.parse(text, options);
let program = ast.program.body[0];
if (!options._svelte_asFunction) {
program = program.expression;
}
return Object.assign(Object.assign({}, ast), { program });
} }),
};
const printers = {
'svelte-ast': {
print,
embed,
// @ts-expect-error Prettier's type definitions are wrong
getVisitorKeys,
},
};
export { languages, options, parsers, printers };
|