svelte2tsx 0.4.10 → 0.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +6 -2
- package/index.js +249 -73
- package/index.mjs +249 -73
- package/package.json +3 -3
- package/svelte-jsx.d.ts +626 -633
- package/svelte-shims.d.ts +1 -0
package/index.d.ts
CHANGED
|
@@ -55,7 +55,7 @@ export function svelte2tsx(
|
|
|
55
55
|
}
|
|
56
56
|
): SvelteCompiledToTsx
|
|
57
57
|
|
|
58
|
-
export interface
|
|
58
|
+
export interface EmitDtsConfig {
|
|
59
59
|
/**
|
|
60
60
|
* Where to output the declaration files
|
|
61
61
|
*/
|
|
@@ -74,10 +74,14 @@ export interface EmitDtsConig {
|
|
|
74
74
|
libRoot?: string;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
// to make typo fix non-breaking, continue to export the old name but mark it as deprecated
|
|
78
|
+
/**@deprecated*/
|
|
79
|
+
export interface EmitDtsConig extends EmitDtsConfig {} /* eslint-disable-line @typescript-eslint/no-empty-interface */
|
|
80
|
+
|
|
77
81
|
/**
|
|
78
82
|
* Searches for a jsconfig or tsconfig starting at `root` and emits d.ts files
|
|
79
83
|
* into `declarationDir` using the ambient file from `svelteShimsPath`.
|
|
80
84
|
* Note: Handwritten `d.ts` files are not copied over; TypeScript does not
|
|
81
85
|
* touch these files.
|
|
82
86
|
*/
|
|
83
|
-
export function emitDts(config:
|
|
87
|
+
export function emitDts(config: EmitDtsConfig): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1677,6 +1677,21 @@ function usesLet(node) {
|
|
|
1677
1677
|
var _a;
|
|
1678
1678
|
return (_a = node.attributes) === null || _a === void 0 ? void 0 : _a.some((attr) => attr.type === 'Let');
|
|
1679
1679
|
}
|
|
1680
|
+
function buildTemplateString(attr, str, htmlx, leadingOverride, trailingOverride, overrideStart) {
|
|
1681
|
+
overrideStart = overrideStart !== null && overrideStart !== void 0 ? overrideStart : htmlx.lastIndexOf('=', attr.value[0].start);
|
|
1682
|
+
str.overwrite(overrideStart, attr.value[0].start, leadingOverride);
|
|
1683
|
+
for (const n of attr.value) {
|
|
1684
|
+
if (n.type == 'MustacheTag') {
|
|
1685
|
+
str.appendRight(n.start, '$');
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
if (isQuote(htmlx[attr.end - 1])) {
|
|
1689
|
+
str.overwrite(attr.end - 1, attr.end, trailingOverride);
|
|
1690
|
+
}
|
|
1691
|
+
else {
|
|
1692
|
+
str.appendLeft(attr.end, trailingOverride);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1680
1695
|
|
|
1681
1696
|
/**
|
|
1682
1697
|
* use:xxx={params} ---> {...__sveltets_1_ensureAction(xxx(__sveltets_1_mapElementTag('ParentNodeName'),(params)))}
|
|
@@ -1799,8 +1814,9 @@ function handleAttribute(htmlx, str, attr, parent, preserveCase) {
|
|
|
1799
1814
|
}
|
|
1800
1815
|
return;
|
|
1801
1816
|
}
|
|
1802
|
-
if (attr.value.length == 0)
|
|
1817
|
+
if (attr.value.length == 0) {
|
|
1803
1818
|
return; //wut?
|
|
1819
|
+
}
|
|
1804
1820
|
//handle single value
|
|
1805
1821
|
if (attr.value.length == 1) {
|
|
1806
1822
|
const attrVal = attr.value[0];
|
|
@@ -1878,21 +1894,6 @@ function handleAttribute(htmlx, str, attr, parent, preserveCase) {
|
|
|
1878
1894
|
// We have multiple attribute values, so we build a template string out of them.
|
|
1879
1895
|
buildTemplateString(attr, str, htmlx, shouldApplySlotCheck ? `={${ensureSlotStr}\`` : '={`', shouldApplySlotCheck ? '`)}' : '`}');
|
|
1880
1896
|
}
|
|
1881
|
-
function buildTemplateString(attr, str, htmlx, leadingOverride, trailingOverride) {
|
|
1882
|
-
const equals = htmlx.lastIndexOf('=', attr.value[0].start);
|
|
1883
|
-
str.overwrite(equals, attr.value[0].start, leadingOverride);
|
|
1884
|
-
for (const n of attr.value) {
|
|
1885
|
-
if (n.type == 'MustacheTag') {
|
|
1886
|
-
str.appendRight(n.start, '$');
|
|
1887
|
-
}
|
|
1888
|
-
}
|
|
1889
|
-
if (isQuote(htmlx[attr.end - 1])) {
|
|
1890
|
-
str.overwrite(attr.end - 1, attr.end, trailingOverride);
|
|
1891
|
-
}
|
|
1892
|
-
else {
|
|
1893
|
-
str.appendLeft(attr.end, trailingOverride);
|
|
1894
|
-
}
|
|
1895
|
-
}
|
|
1896
1897
|
function sanitizeLeadingChars(attrName) {
|
|
1897
1898
|
let sanitizedName = '';
|
|
1898
1899
|
for (let i = 0; i < attrName.length; i++) {
|
|
@@ -1907,6 +1908,24 @@ function sanitizeLeadingChars(attrName) {
|
|
|
1907
1908
|
return sanitizedName;
|
|
1908
1909
|
}
|
|
1909
1910
|
|
|
1911
|
+
function extractConstTags(children) {
|
|
1912
|
+
const tags = [];
|
|
1913
|
+
for (const child of children) {
|
|
1914
|
+
if (child.type === 'ConstTag') {
|
|
1915
|
+
const constTag = child;
|
|
1916
|
+
tags.push((insertionPoint, str) => {
|
|
1917
|
+
str.appendRight(constTag.expression.left.start, 'const ');
|
|
1918
|
+
str.move(constTag.expression.left.start, constTag.expression.right.end, insertionPoint);
|
|
1919
|
+
str.appendLeft(constTag.expression.right.end, ';');
|
|
1920
|
+
str.overwrite(constTag.start + 1, constTag.expression.left.start - 1, '', {
|
|
1921
|
+
contentOnly: true
|
|
1922
|
+
});
|
|
1923
|
+
});
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return tags;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1910
1929
|
/**
|
|
1911
1930
|
* Transform {#await ...} into something JSX understands
|
|
1912
1931
|
*/
|
|
@@ -1947,7 +1966,7 @@ function handleAwaitThen(awaitBlock, htmlx, str, ifScope) {
|
|
|
1947
1966
|
if (awaitBlock.then.skip) {
|
|
1948
1967
|
return;
|
|
1949
1968
|
}
|
|
1950
|
-
// then value } | {:then value} | {await ..} .. {/await} ->
|
|
1969
|
+
// then value } | then} | {:then value} | {await ..} .. {/await} ->
|
|
1951
1970
|
// __sveltets_1_awaitThen(_$$p, (value) => {(possibleIfCondition && )<>
|
|
1952
1971
|
let thenStart;
|
|
1953
1972
|
let thenEnd;
|
|
@@ -1972,22 +1991,54 @@ function handleAwaitThen(awaitBlock, htmlx, str, ifScope) {
|
|
|
1972
1991
|
}
|
|
1973
1992
|
if (awaitBlock.value) {
|
|
1974
1993
|
str.overwrite(thenStart, awaitBlock.value.start, '__sveltets_1_awaitThen(_$$p, (');
|
|
1975
|
-
str.overwrite(awaitBlock.value.end, thenEnd,
|
|
1994
|
+
str.overwrite(awaitBlock.value.end, thenEnd, ') => {');
|
|
1995
|
+
extractConstTags(awaitBlock.then.children).forEach((insertion) => {
|
|
1996
|
+
insertion(thenEnd, str);
|
|
1997
|
+
});
|
|
1998
|
+
str.appendRight(thenEnd, `${ifScope.addPossibleIfCondition()}<>`);
|
|
1976
1999
|
}
|
|
1977
2000
|
else {
|
|
1978
|
-
const awaitThenFn =
|
|
2001
|
+
const awaitThenFn = '__sveltets_1_awaitThen(_$$p, () => {';
|
|
1979
2002
|
if (thenStart === thenEnd) {
|
|
1980
2003
|
str.appendLeft(thenStart, awaitThenFn);
|
|
1981
2004
|
}
|
|
1982
2005
|
else {
|
|
1983
2006
|
str.overwrite(thenStart, thenEnd, awaitThenFn);
|
|
1984
2007
|
}
|
|
2008
|
+
extractConstTags(awaitBlock.then.children).forEach((insertion) => {
|
|
2009
|
+
insertion(thenEnd, str);
|
|
2010
|
+
});
|
|
2011
|
+
str.appendRight(thenEnd, `${ifScope.addPossibleIfCondition()}<>`); // eslint-disable-line
|
|
1985
2012
|
}
|
|
1986
2013
|
}
|
|
1987
2014
|
function handleAwaitCatch(awaitBlock, htmlx, str, ifScope) {
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
2015
|
+
if (awaitBlock.catch.skip) {
|
|
2016
|
+
return;
|
|
2017
|
+
}
|
|
2018
|
+
if (awaitBlock.pending.skip && awaitBlock.then.skip) {
|
|
2019
|
+
if (awaitBlock.error) {
|
|
2020
|
+
// {#await ... catch ...}
|
|
2021
|
+
const catchBegin = htmlx.indexOf('}', awaitBlock.error.end) + 1;
|
|
2022
|
+
str.overwrite(awaitBlock.expression.end, awaitBlock.error.start, '); __sveltets_1_awaitThen(_$$p, () => {}, (');
|
|
2023
|
+
str.overwrite(awaitBlock.error.end, catchBegin, ') => {');
|
|
2024
|
+
extractConstTags(awaitBlock.catch.children).forEach((insertion) => {
|
|
2025
|
+
insertion(catchBegin, str);
|
|
2026
|
+
});
|
|
2027
|
+
str.appendRight(catchBegin, '<>');
|
|
2028
|
+
}
|
|
2029
|
+
else {
|
|
2030
|
+
// {#await ... catch}
|
|
2031
|
+
const catchBegin = htmlx.indexOf('}', awaitBlock.expression.end) + 1;
|
|
2032
|
+
str.overwrite(awaitBlock.expression.end, catchBegin, '); __sveltets_1_awaitThen(_$$p, () => {}, () => {');
|
|
2033
|
+
extractConstTags(awaitBlock.catch.children).forEach((insertion) => {
|
|
2034
|
+
insertion(catchBegin, str);
|
|
2035
|
+
});
|
|
2036
|
+
str.appendRight(catchBegin, '<>');
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
else {
|
|
2040
|
+
//{:catch error} ->
|
|
2041
|
+
//</>}, (error) => {<>
|
|
1991
2042
|
//catch block includes the {:catch}
|
|
1992
2043
|
const catchStart = awaitBlock.catch.start;
|
|
1993
2044
|
const catchSymbolEnd = htmlx.indexOf(':catch', catchStart) + ':catch'.length;
|
|
@@ -1995,7 +2046,11 @@ function handleAwaitCatch(awaitBlock, htmlx, str, ifScope) {
|
|
|
1995
2046
|
const errorEnd = awaitBlock.error ? awaitBlock.error.end : errorStart;
|
|
1996
2047
|
const catchEnd = htmlx.indexOf('}', errorEnd) + 1;
|
|
1997
2048
|
str.overwrite(catchStart, errorStart, '</>}, (');
|
|
1998
|
-
str.overwrite(errorEnd, catchEnd,
|
|
2049
|
+
str.overwrite(errorEnd, catchEnd, ') => {');
|
|
2050
|
+
extractConstTags(awaitBlock.catch.children).forEach((insertion) => {
|
|
2051
|
+
insertion(catchEnd, str);
|
|
2052
|
+
});
|
|
2053
|
+
str.appendRight(catchEnd, `${ifScope.addPossibleIfCondition()}<>`);
|
|
1999
2054
|
}
|
|
2000
2055
|
}
|
|
2001
2056
|
|
|
@@ -2103,14 +2158,23 @@ function handleComment(str, node) {
|
|
|
2103
2158
|
|
|
2104
2159
|
const shadowedPropsSymbol = Symbol('shadowedProps');
|
|
2105
2160
|
/**
|
|
2106
|
-
* Transforms the usage of a slot (
|
|
2161
|
+
* Transforms the usage of a slot (slot="xxx")
|
|
2162
|
+
* - transforms let:xx, {@const xx}
|
|
2107
2163
|
*/
|
|
2108
2164
|
function handleSlot(htmlx, str, slotEl, component, slotName, ifScope, templateScope) {
|
|
2109
2165
|
var _a;
|
|
2110
2166
|
//collect "let" definitions
|
|
2111
2167
|
const slotElIsComponent = slotEl === component;
|
|
2112
|
-
let
|
|
2168
|
+
let hasMovedLet = false;
|
|
2113
2169
|
let slotDefInsertionPoint;
|
|
2170
|
+
// lazily calculate insertion point only when needed
|
|
2171
|
+
const calculateSlotDefInsertionPoint = () => {
|
|
2172
|
+
slotDefInsertionPoint =
|
|
2173
|
+
slotDefInsertionPoint ||
|
|
2174
|
+
(slotElIsComponent
|
|
2175
|
+
? htmlx.lastIndexOf('>', slotEl.children[0].start) + 1
|
|
2176
|
+
: slotEl.start);
|
|
2177
|
+
};
|
|
2114
2178
|
for (const attr of slotEl.attributes) {
|
|
2115
2179
|
if (attr.type != 'Let') {
|
|
2116
2180
|
continue;
|
|
@@ -2120,19 +2184,15 @@ function handleSlot(htmlx, str, slotEl, component, slotName, ifScope, templateSc
|
|
|
2120
2184
|
str.remove(attr.start, attr.end);
|
|
2121
2185
|
continue;
|
|
2122
2186
|
}
|
|
2123
|
-
|
|
2124
|
-
slotDefInsertionPoint ||
|
|
2125
|
-
(slotElIsComponent
|
|
2126
|
-
? htmlx.lastIndexOf('>', slotEl.children[0].start) + 1
|
|
2127
|
-
: slotEl.start);
|
|
2187
|
+
calculateSlotDefInsertionPoint();
|
|
2128
2188
|
str.move(attr.start, attr.end, slotDefInsertionPoint);
|
|
2129
2189
|
//remove let:
|
|
2130
2190
|
str.remove(attr.start, attr.start + 'let:'.length);
|
|
2131
|
-
if (
|
|
2191
|
+
if (hasMovedLet) {
|
|
2132
2192
|
str.appendRight(attr.start + 'let:'.length, ', ');
|
|
2133
2193
|
}
|
|
2134
2194
|
templateScope.inits.add(((_a = attr.expression) === null || _a === void 0 ? void 0 : _a.name) || attr.name);
|
|
2135
|
-
|
|
2195
|
+
hasMovedLet = true;
|
|
2136
2196
|
if (attr.expression) {
|
|
2137
2197
|
//overwrite the = as a :
|
|
2138
2198
|
const equalSign = htmlx.lastIndexOf('=', attr.expression.start);
|
|
@@ -2141,13 +2201,34 @@ function handleSlot(htmlx, str, slotEl, component, slotName, ifScope, templateSc
|
|
|
2141
2201
|
str.remove(attr.expression.end, attr.end);
|
|
2142
2202
|
}
|
|
2143
2203
|
}
|
|
2144
|
-
|
|
2204
|
+
const hasConstTag = slotEl.children.some((child) => child.type === 'ConstTag');
|
|
2205
|
+
if (!hasMovedLet && !hasConstTag) {
|
|
2145
2206
|
return;
|
|
2146
2207
|
}
|
|
2208
|
+
calculateSlotDefInsertionPoint();
|
|
2147
2209
|
const { singleSlotDef, constRedeclares } = getSingleSlotDefAndConstsRedeclaration(component, slotName, str.original, ifScope, slotElIsComponent);
|
|
2148
2210
|
const prefix = constRedeclares ? `() => {${constRedeclares}` : '';
|
|
2149
|
-
str.appendLeft(slotDefInsertionPoint, `{${prefix}() => {
|
|
2150
|
-
|
|
2211
|
+
str.appendLeft(slotDefInsertionPoint, `{${prefix}() => { `);
|
|
2212
|
+
if (hasMovedLet) {
|
|
2213
|
+
str.appendLeft(slotDefInsertionPoint, 'let {');
|
|
2214
|
+
str.appendRight(slotDefInsertionPoint, `} = ${singleSlotDef};`);
|
|
2215
|
+
}
|
|
2216
|
+
if (hasConstTag) {
|
|
2217
|
+
// unable to move multiple codes to the same place while insert code in between
|
|
2218
|
+
// NOTE: cheat by move to `slotDefInsertionPoint + 1` position
|
|
2219
|
+
// then copy the character in str[slotDefInsertionPoint...slotDefInsertionPoint + 1] to the back
|
|
2220
|
+
// and comment out the original str[slotDefInsertionPoint...slotDefInsertionPoint + 1]
|
|
2221
|
+
str.appendRight(slotDefInsertionPoint, '/*');
|
|
2222
|
+
extractConstTags(slotEl.children).forEach((insertion) => {
|
|
2223
|
+
insertion(slotDefInsertionPoint + 1, str);
|
|
2224
|
+
});
|
|
2225
|
+
str.appendRight(slotDefInsertionPoint + 1, `${ifScope.addPossibleIfCondition()}<>`);
|
|
2226
|
+
str.appendRight(slotDefInsertionPoint + 1, str.original.slice(slotDefInsertionPoint, slotDefInsertionPoint + 1));
|
|
2227
|
+
str.appendLeft(slotDefInsertionPoint + 1, '*/');
|
|
2228
|
+
}
|
|
2229
|
+
else {
|
|
2230
|
+
str.appendRight(slotDefInsertionPoint, `${ifScope.addPossibleIfCondition()}<>`);
|
|
2231
|
+
}
|
|
2151
2232
|
const closeSlotDefInsertionPoint = slotElIsComponent
|
|
2152
2233
|
? htmlx.lastIndexOf('<', slotEl.end - 1)
|
|
2153
2234
|
: slotEl.end;
|
|
@@ -2233,7 +2314,11 @@ function handleEach(htmlx, str, eachBlock, ifScope) {
|
|
|
2233
2314
|
const idxLoc = htmlx.indexOf(eachBlock.index, contextEnd);
|
|
2234
2315
|
contextEnd = idxLoc + eachBlock.index.length;
|
|
2235
2316
|
}
|
|
2236
|
-
|
|
2317
|
+
const constTags = extractConstTags(eachBlock.children);
|
|
2318
|
+
str.prependLeft(contextEnd, ') =>' + (constTags.length ? ' {' : ''));
|
|
2319
|
+
constTags.forEach((insertion) => {
|
|
2320
|
+
insertion(contextEnd, str);
|
|
2321
|
+
});
|
|
2237
2322
|
if (eachBlock.key) {
|
|
2238
2323
|
const endEachStart = htmlx.indexOf('}', eachBlock.key.end);
|
|
2239
2324
|
str.overwrite(endEachStart, endEachStart + 1, ` && ${ifScope.addPossibleIfCondition()}<>`);
|
|
@@ -2243,8 +2328,8 @@ function handleEach(htmlx, str, eachBlock, ifScope) {
|
|
|
2243
2328
|
str.overwrite(endEachStart, endEachStart + 1, ` ${ifScope.addPossibleIfCondition()}<>`);
|
|
2244
2329
|
}
|
|
2245
2330
|
const endEach = htmlx.lastIndexOf('{', eachBlock.end - 1);
|
|
2246
|
-
const suffix = constRedeclares ? '
|
|
2247
|
-
// {/each} -> </>)} or {:else} -> </>)}
|
|
2331
|
+
const suffix = '</>' + (constTags.length ? '}' : '') + (constRedeclares ? ')}}}' : ')}');
|
|
2332
|
+
// {/each} -> </>})} or {:else} -> </>})}
|
|
2248
2333
|
if (eachBlock.else) {
|
|
2249
2334
|
const elseEnd = htmlx.lastIndexOf('}', eachBlock.else.start);
|
|
2250
2335
|
const elseStart = htmlx.lastIndexOf('{', elseEnd);
|
|
@@ -2662,6 +2747,40 @@ function handleRawHtml(htmlx, str, rawBlock) {
|
|
|
2662
2747
|
str.remove(tokenStart, tokenStart + '@html'.length);
|
|
2663
2748
|
}
|
|
2664
2749
|
|
|
2750
|
+
/**
|
|
2751
|
+
* style:xx ---> __sveltets_1_ensureType(String, xx);
|
|
2752
|
+
* style:xx={yy} ---> __sveltets_1_ensureType(String, yy);
|
|
2753
|
+
* style:xx="yy" ---> __sveltets_1_ensureType(String, "yy");
|
|
2754
|
+
* style:xx="a{b}" ---> __sveltets_1_ensureType(String, `a${b}`);
|
|
2755
|
+
*/
|
|
2756
|
+
function handleStyleDirective(str, style) {
|
|
2757
|
+
const htmlx = str.original;
|
|
2758
|
+
if (style.value === true || style.value.length === 0) {
|
|
2759
|
+
str.overwrite(style.start, htmlx.indexOf(':', style.start) + 1, '{...__sveltets_1_ensureType(String, ');
|
|
2760
|
+
str.appendLeft(style.end, ')}');
|
|
2761
|
+
return;
|
|
2762
|
+
}
|
|
2763
|
+
if (style.value.length > 1) {
|
|
2764
|
+
buildTemplateString(style, str, htmlx, '{...__sveltets_1_ensureType(String, `', '`)}', style.start);
|
|
2765
|
+
return;
|
|
2766
|
+
}
|
|
2767
|
+
const styleVal = style.value[0];
|
|
2768
|
+
if (styleVal.type === 'Text') {
|
|
2769
|
+
str.overwrite(style.start, styleVal.start, '{...__sveltets_1_ensureType(String, "');
|
|
2770
|
+
if (styleVal.end === style.end) {
|
|
2771
|
+
str.appendLeft(style.end, '")}');
|
|
2772
|
+
}
|
|
2773
|
+
else {
|
|
2774
|
+
str.overwrite(styleVal.end, style.end, '")}');
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
else {
|
|
2778
|
+
// MustacheTag
|
|
2779
|
+
str.overwrite(style.start, styleVal.start + 1, '{...__sveltets_1_ensureType(String, ');
|
|
2780
|
+
str.overwrite(styleVal.end - 1, style.end, ')}');
|
|
2781
|
+
}
|
|
2782
|
+
}
|
|
2783
|
+
|
|
2665
2784
|
/**
|
|
2666
2785
|
* `<svelte:window>...</svelte:window>` ----> `<sveltewindow>...</sveltewindow>`
|
|
2667
2786
|
* (same for :head, :body, :options, :fragment)
|
|
@@ -2753,7 +2872,7 @@ class TemplateScopeManager {
|
|
|
2753
2872
|
eachEnter(node) {
|
|
2754
2873
|
this.value = this.value.child();
|
|
2755
2874
|
if (node.context) {
|
|
2756
|
-
this.handleScope(node.context);
|
|
2875
|
+
this.handleScope(node.context, node.children);
|
|
2757
2876
|
}
|
|
2758
2877
|
if (node.index) {
|
|
2759
2878
|
this.value.inits.add(node.index);
|
|
@@ -2765,12 +2884,13 @@ class TemplateScopeManager {
|
|
|
2765
2884
|
}
|
|
2766
2885
|
}
|
|
2767
2886
|
awaitEnter(node) {
|
|
2887
|
+
var _a;
|
|
2768
2888
|
this.value = this.value.child();
|
|
2769
2889
|
if (node.value) {
|
|
2770
|
-
this.handleScope(node.value);
|
|
2890
|
+
this.handleScope(node.value, (_a = node.then) === null || _a === void 0 ? void 0 : _a.children);
|
|
2771
2891
|
}
|
|
2772
2892
|
if (node.error) {
|
|
2773
|
-
this.handleScope(node.error);
|
|
2893
|
+
this.handleScope(node.error, []);
|
|
2774
2894
|
}
|
|
2775
2895
|
}
|
|
2776
2896
|
awaitPendingEnter(node, parent) {
|
|
@@ -2788,7 +2908,7 @@ class TemplateScopeManager {
|
|
|
2788
2908
|
// scope into account.
|
|
2789
2909
|
this.value.inits.clear();
|
|
2790
2910
|
if (parent.value) {
|
|
2791
|
-
this.handleScope(parent.value);
|
|
2911
|
+
this.handleScope(parent.value, node.children);
|
|
2792
2912
|
}
|
|
2793
2913
|
}
|
|
2794
2914
|
awaitCatchEnter(node, parent) {
|
|
@@ -2799,7 +2919,7 @@ class TemplateScopeManager {
|
|
|
2799
2919
|
// scope into account.
|
|
2800
2920
|
this.value.inits.clear();
|
|
2801
2921
|
if (parent.error) {
|
|
2802
|
-
this.handleScope(parent.error);
|
|
2922
|
+
this.handleScope(parent.error, node.children);
|
|
2803
2923
|
}
|
|
2804
2924
|
}
|
|
2805
2925
|
awaitLeave() {
|
|
@@ -2811,8 +2931,11 @@ class TemplateScopeManager {
|
|
|
2811
2931
|
}
|
|
2812
2932
|
}
|
|
2813
2933
|
componentOrSlotTemplateOrElementEnter(node) {
|
|
2814
|
-
|
|
2934
|
+
var _a;
|
|
2935
|
+
const hasConstTags = (_a = node.children) === null || _a === void 0 ? void 0 : _a.some((child) => child.type === 'ConstTag');
|
|
2936
|
+
if (usesLet(node) || hasConstTags) {
|
|
2815
2937
|
this.value = this.value.child();
|
|
2938
|
+
this.handleScope({}, node.children);
|
|
2816
2939
|
}
|
|
2817
2940
|
}
|
|
2818
2941
|
componentOrSlotTemplateOrElementLeave(node) {
|
|
@@ -2820,7 +2943,7 @@ class TemplateScopeManager {
|
|
|
2820
2943
|
this.value = this.value.parent;
|
|
2821
2944
|
}
|
|
2822
2945
|
}
|
|
2823
|
-
handleScope(identifierDef) {
|
|
2946
|
+
handleScope(identifierDef, children) {
|
|
2824
2947
|
if (isIdentifier(identifierDef)) {
|
|
2825
2948
|
this.value.inits.add(identifierDef.name);
|
|
2826
2949
|
}
|
|
@@ -2829,6 +2952,14 @@ class TemplateScopeManager {
|
|
|
2829
2952
|
const identifiers = extract_identifiers(identifierDef);
|
|
2830
2953
|
identifiers.forEach((id) => this.value.inits.add(id.name));
|
|
2831
2954
|
}
|
|
2955
|
+
if (children === null || children === void 0 ? void 0 : children.length) {
|
|
2956
|
+
children.forEach((child) => {
|
|
2957
|
+
if (child.type === 'ConstTag') {
|
|
2958
|
+
const identifiers = extract_identifiers(child.expression.left);
|
|
2959
|
+
identifiers.forEach((id) => this.value.inits.add(id.name));
|
|
2960
|
+
}
|
|
2961
|
+
});
|
|
2962
|
+
}
|
|
2832
2963
|
}
|
|
2833
2964
|
}
|
|
2834
2965
|
|
|
@@ -2944,6 +3075,9 @@ function convertHtmlxToJsx(str, ast, onWalk = null, onLeave = null, options = {}
|
|
|
2944
3075
|
case 'Class':
|
|
2945
3076
|
handleClassDirective(str, node);
|
|
2946
3077
|
break;
|
|
3078
|
+
case 'StyleDirective':
|
|
3079
|
+
handleStyleDirective(str, node);
|
|
3080
|
+
break;
|
|
2947
3081
|
case 'Action':
|
|
2948
3082
|
handleActionDirective(htmlx, str, node, parent);
|
|
2949
3083
|
break;
|
|
@@ -2974,9 +3108,7 @@ function convertHtmlxToJsx(str, ast, onWalk = null, onLeave = null, options = {}
|
|
|
2974
3108
|
case 'SlotTemplate':
|
|
2975
3109
|
handleSvelteTag(htmlx, str, node);
|
|
2976
3110
|
templateScopeManager.componentOrSlotTemplateOrElementEnter(node);
|
|
2977
|
-
|
|
2978
|
-
handleSlot(htmlx, str, node, parent, getSlotName(node) || 'default', ifScope, templateScopeManager.value);
|
|
2979
|
-
}
|
|
3111
|
+
handleSlot(htmlx, str, node, parent, getSlotName(node) || 'default', ifScope, templateScopeManager.value);
|
|
2980
3112
|
break;
|
|
2981
3113
|
case 'Text':
|
|
2982
3114
|
handleText(str, node);
|
|
@@ -3767,9 +3899,6 @@ class ExportedNames {
|
|
|
3767
3899
|
`\n /**accessor*/\n set ${name}(_) {}`)
|
|
3768
3900
|
.join('');
|
|
3769
3901
|
}
|
|
3770
|
-
setUses$$Props() {
|
|
3771
|
-
this.uses$$Props = true;
|
|
3772
|
-
}
|
|
3773
3902
|
/**
|
|
3774
3903
|
* Marks a top level declaration as a possible export
|
|
3775
3904
|
* which could be exported through `export { .. }` later.
|
|
@@ -3866,6 +3995,9 @@ class ExportedNames {
|
|
|
3866
3995
|
'...__sveltets_1_ensureRightProps<Partial<$$Props>>({' +
|
|
3867
3996
|
this.createReturnElements(lets, false).join(',') +
|
|
3868
3997
|
'}), ...{} as unknown as $$Props, ...{' +
|
|
3998
|
+
// We add other exports of classes and functions here because
|
|
3999
|
+
// they need to appear in the props object in order to properly
|
|
4000
|
+
// type bind:xx but they are not needed to be part of $$Props
|
|
3869
4001
|
this.createReturnElements(others, false).join(', ') +
|
|
3870
4002
|
'} as {' +
|
|
3871
4003
|
this.createReturnElementsType(others).join(',') +
|
|
@@ -4099,8 +4231,9 @@ class Scripts {
|
|
|
4099
4231
|
}
|
|
4100
4232
|
|
|
4101
4233
|
function attributeStrValueAsJsExpression(attr) {
|
|
4102
|
-
if (attr.value.length == 0)
|
|
4234
|
+
if (attr.value.length == 0) {
|
|
4103
4235
|
return "''"; //wut?
|
|
4236
|
+
}
|
|
4104
4237
|
//handle single value
|
|
4105
4238
|
if (attr.value.length == 1) {
|
|
4106
4239
|
const attrVal = attr.value[0];
|
|
@@ -4133,6 +4266,12 @@ class SlotHandler {
|
|
|
4133
4266
|
}
|
|
4134
4267
|
return resolved;
|
|
4135
4268
|
}
|
|
4269
|
+
/**
|
|
4270
|
+
* Returns a string which expresses the given identifier unpacked to
|
|
4271
|
+
* the top level in order to express the slot types correctly later on.
|
|
4272
|
+
*
|
|
4273
|
+
* Example: {#each items as item} ---> __sveltets_1_unwrapArr(items)
|
|
4274
|
+
*/
|
|
4136
4275
|
getResolveExpressionStr(identifierDef, scope, initExpression) {
|
|
4137
4276
|
const { name } = identifierDef;
|
|
4138
4277
|
const owner = scope.getOwner(name);
|
|
@@ -4201,6 +4340,10 @@ class SlotHandler {
|
|
|
4201
4340
|
slotName
|
|
4202
4341
|
}));
|
|
4203
4342
|
}
|
|
4343
|
+
/**
|
|
4344
|
+
* Resolves the slot expression to a string that can be used
|
|
4345
|
+
* in the props-object in the return type of the render function
|
|
4346
|
+
*/
|
|
4204
4347
|
resolveExpression(expression, scope) {
|
|
4205
4348
|
let resolved = this.resolvedExpression.get(expression);
|
|
4206
4349
|
if (resolved) {
|
|
@@ -4213,8 +4356,9 @@ class SlotHandler {
|
|
|
4213
4356
|
enter(node, parent, prop) {
|
|
4214
4357
|
if (node.type === 'Identifier') {
|
|
4215
4358
|
if (parent) {
|
|
4216
|
-
if (isMember$1(parent, prop))
|
|
4359
|
+
if (isMember$1(parent, prop)) {
|
|
4217
4360
|
return;
|
|
4361
|
+
}
|
|
4218
4362
|
if (isObjectKey(parent, prop)) {
|
|
4219
4363
|
return;
|
|
4220
4364
|
}
|
|
@@ -4257,10 +4401,18 @@ class SlotHandler {
|
|
|
4257
4401
|
//collect attributes
|
|
4258
4402
|
const attributes = new Map();
|
|
4259
4403
|
for (const attr of node.attributes) {
|
|
4260
|
-
if (attr.name == 'name')
|
|
4404
|
+
if (attr.name == 'name') {
|
|
4261
4405
|
continue;
|
|
4262
|
-
|
|
4406
|
+
}
|
|
4407
|
+
if (attr.type === 'Spread') {
|
|
4408
|
+
const rawName = attr.expression.name;
|
|
4409
|
+
const init = scope.getInit(rawName);
|
|
4410
|
+
const name = init ? this.resolved.get(init) : rawName;
|
|
4411
|
+
attributes.set(`__spread__${name}`, name);
|
|
4412
|
+
}
|
|
4413
|
+
if (!((_a = attr.value) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
4263
4414
|
continue;
|
|
4415
|
+
}
|
|
4264
4416
|
if (attributeValueIsString(attr)) {
|
|
4265
4417
|
attributes.set(attr.name, attributeStrValueAsJsExpression(attr));
|
|
4266
4418
|
continue;
|
|
@@ -4325,10 +4477,12 @@ function handleStore(node, parent, str) {
|
|
|
4325
4477
|
// handle $store++, $store--, ++$store, --$store
|
|
4326
4478
|
if (parent.type == 'UpdateExpression') {
|
|
4327
4479
|
let simpleOperator;
|
|
4328
|
-
if (parent.operator === '++')
|
|
4480
|
+
if (parent.operator === '++') {
|
|
4329
4481
|
simpleOperator = '+';
|
|
4330
|
-
|
|
4482
|
+
}
|
|
4483
|
+
if (parent.operator === '--') {
|
|
4331
4484
|
simpleOperator = '-';
|
|
4485
|
+
}
|
|
4332
4486
|
if (simpleOperator) {
|
|
4333
4487
|
const storename = node.name.slice(1); // drop the $
|
|
4334
4488
|
str.overwrite(parent.start, parent.end, `${storename}.set( $${storename} ${simpleOperator} 1)`);
|
|
@@ -4691,6 +4845,25 @@ class Generics {
|
|
|
4691
4845
|
}
|
|
4692
4846
|
}
|
|
4693
4847
|
|
|
4848
|
+
/**
|
|
4849
|
+
* move imports to top of script so they appear outside our render function
|
|
4850
|
+
*/
|
|
4851
|
+
function handleImportDeclaration(node, str, astOffset, scriptStart) {
|
|
4852
|
+
var _a;
|
|
4853
|
+
const comments = (_a = ts__default['default'].getLeadingCommentRanges(node.getFullText(), 0)) !== null && _a !== void 0 ? _a : [];
|
|
4854
|
+
for (const comment of comments) {
|
|
4855
|
+
const commentEnd = node.pos + comment.end + astOffset;
|
|
4856
|
+
str.move(node.pos + comment.pos + astOffset, commentEnd, scriptStart + 1);
|
|
4857
|
+
if (comment.hasTrailingNewLine) {
|
|
4858
|
+
str.overwrite(commentEnd - 1, commentEnd, str.original[commentEnd - 1] + '\n');
|
|
4859
|
+
}
|
|
4860
|
+
}
|
|
4861
|
+
str.move(node.getStart() + astOffset, node.end + astOffset, scriptStart + 1);
|
|
4862
|
+
//add in a \n
|
|
4863
|
+
const originalEndChar = str.original[node.end + astOffset - 1];
|
|
4864
|
+
str.overwrite(node.end + astOffset - 1, node.end + astOffset, originalEndChar + '\n');
|
|
4865
|
+
}
|
|
4866
|
+
|
|
4694
4867
|
function processInstanceScriptContent(str, script, events, implicitStoreValues, mode) {
|
|
4695
4868
|
const htmlx = str.original;
|
|
4696
4869
|
const scriptContent = htmlx.substring(script.content.start, script.content.end);
|
|
@@ -4790,7 +4963,12 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
|
|
|
4790
4963
|
// - in order to get ts errors if store is not assignable to SvelteStore
|
|
4791
4964
|
// - use $store variable defined above to get ts flow control
|
|
4792
4965
|
const dollar = str.original.indexOf('$', ident.getStart() + astOffset);
|
|
4793
|
-
const getPrefix = isSafeToPrefixWithSemicolon(ident)
|
|
4966
|
+
const getPrefix = isSafeToPrefixWithSemicolon(ident)
|
|
4967
|
+
? ';'
|
|
4968
|
+
: ts__namespace.isShorthandPropertyAssignment(parent)
|
|
4969
|
+
? // { $store } --> { $store: __sveltets_1_store_get(..)}
|
|
4970
|
+
ident.text + ': '
|
|
4971
|
+
: '';
|
|
4794
4972
|
str.overwrite(dollar, dollar + 1, getPrefix + '(__sveltets_1_store_get(');
|
|
4795
4973
|
str.prependLeft(ident.end + astOffset, `), $${storename})`);
|
|
4796
4974
|
};
|
|
@@ -4861,7 +5039,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
|
|
|
4861
5039
|
uses$$SlotsInterface = true;
|
|
4862
5040
|
}
|
|
4863
5041
|
if (is$$PropsDeclaration(node)) {
|
|
4864
|
-
exportedNames.
|
|
5042
|
+
exportedNames.uses$$Props = true;
|
|
4865
5043
|
}
|
|
4866
5044
|
if (ts__namespace.isVariableStatement(node)) {
|
|
4867
5045
|
exportedNames.handleVariableStatement(node, parent);
|
|
@@ -4886,11 +5064,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
|
|
|
4886
5064
|
exportedNames.handleExportDeclaration(node);
|
|
4887
5065
|
}
|
|
4888
5066
|
if (ts__namespace.isImportDeclaration(node)) {
|
|
4889
|
-
|
|
4890
|
-
str.move(node.getStart() + astOffset, node.end + astOffset, script.start + 1);
|
|
4891
|
-
//add in a \n
|
|
4892
|
-
const originalEndChar = str.original[node.end + astOffset - 1];
|
|
4893
|
-
str.overwrite(node.end + astOffset - 1, node.end + astOffset, originalEndChar + '\n');
|
|
5067
|
+
handleImportDeclaration(node, str, astOffset, script.start);
|
|
4894
5068
|
// Check if import is the event dispatcher
|
|
4895
5069
|
events.checkIfImportIsEventDispatcher(node);
|
|
4896
5070
|
}
|
|
@@ -5072,7 +5246,7 @@ function addComponentExport(params) {
|
|
|
5072
5246
|
addSimpleComponentExport(params);
|
|
5073
5247
|
}
|
|
5074
5248
|
}
|
|
5075
|
-
function addGenericsComponentExport({ strictEvents,
|
|
5249
|
+
function addGenericsComponentExport({ strictEvents, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str, generics }) {
|
|
5076
5250
|
const genericsDef = generics.toDefinitionString();
|
|
5077
5251
|
const genericsRef = generics.toReferencesString();
|
|
5078
5252
|
const doc = componentDocumentation.getFormatted();
|
|
@@ -5083,7 +5257,7 @@ function addGenericsComponentExport({ strictEvents, uses$$propsOr$$restProps, ex
|
|
|
5083
5257
|
let statement = `
|
|
5084
5258
|
class __sveltets_Render${genericsDef} {
|
|
5085
5259
|
props() {
|
|
5086
|
-
return ${props(true,
|
|
5260
|
+
return ${props(true, canHaveAnyProp, exportedNames, `render${genericsRef}()`)}.props;
|
|
5087
5261
|
}
|
|
5088
5262
|
events() {
|
|
5089
5263
|
return ${events(strictEvents, `render${genericsRef}()`)}.events;
|
|
@@ -5112,8 +5286,8 @@ class __sveltets_Render${genericsDef} {
|
|
|
5112
5286
|
}
|
|
5113
5287
|
str.append(statement);
|
|
5114
5288
|
}
|
|
5115
|
-
function addSimpleComponentExport({ strictEvents, isTsFile,
|
|
5116
|
-
const propDef = props(isTsFile,
|
|
5289
|
+
function addSimpleComponentExport({ strictEvents, isTsFile, canHaveAnyProp, exportedNames, componentDocumentation, fileName, mode, usesAccessors, str }) {
|
|
5290
|
+
const propDef = props(isTsFile, canHaveAnyProp, exportedNames, events(strictEvents, 'render()'));
|
|
5117
5291
|
const doc = componentDocumentation.getFormatted();
|
|
5118
5292
|
const className = fileName && classNameFromFilename(fileName, mode !== 'dts');
|
|
5119
5293
|
let statement;
|
|
@@ -5151,13 +5325,13 @@ function addSimpleComponentExport({ strictEvents, isTsFile, uses$$propsOr$$restP
|
|
|
5151
5325
|
function events(strictEvents, renderStr) {
|
|
5152
5326
|
return strictEvents ? renderStr : `__sveltets_1_with_any_event(${renderStr})`;
|
|
5153
5327
|
}
|
|
5154
|
-
function props(isTsFile,
|
|
5328
|
+
function props(isTsFile, canHaveAnyProp, exportedNames, renderStr) {
|
|
5155
5329
|
if (isTsFile) {
|
|
5156
|
-
return
|
|
5330
|
+
return canHaveAnyProp ? `__sveltets_1_with_any(${renderStr})` : renderStr;
|
|
5157
5331
|
}
|
|
5158
5332
|
else {
|
|
5159
5333
|
const optionalProps = exportedNames.createOptionalPropsArray();
|
|
5160
|
-
const partial = `__sveltets_1_partial${
|
|
5334
|
+
const partial = `__sveltets_1_partial${canHaveAnyProp ? '_with_any' : ''}`;
|
|
5161
5335
|
return optionalProps.length > 0
|
|
5162
5336
|
? `${partial}([${optionalProps.join(',')}], ${renderStr})`
|
|
5163
5337
|
: `${partial}(${renderStr})`;
|
|
@@ -5238,7 +5412,9 @@ function createRenderFunction({ str, scriptTag, scriptDestination, slots, events
|
|
|
5238
5412
|
Array.from(slots.entries())
|
|
5239
5413
|
.map(([name, attrs]) => {
|
|
5240
5414
|
const attrsAsString = Array.from(attrs.entries())
|
|
5241
|
-
.map(([exportName, expr]) =>
|
|
5415
|
+
.map(([exportName, expr]) => exportName.startsWith('__spread__')
|
|
5416
|
+
? `...${expr}`
|
|
5417
|
+
: `${exportName}:${expr}`)
|
|
5242
5418
|
.join(', ');
|
|
5243
5419
|
return `'${name}': {${attrsAsString}}`;
|
|
5244
5420
|
})
|
|
@@ -5518,7 +5694,7 @@ function svelte2tsx(svelte, options = {}) {
|
|
|
5518
5694
|
}
|
|
5519
5695
|
addComponentExport({
|
|
5520
5696
|
str,
|
|
5521
|
-
uses$$
|
|
5697
|
+
canHaveAnyProp: !exportedNames.uses$$Props && (uses$$props || uses$$restProps),
|
|
5522
5698
|
strictEvents: events.hasStrictEvents(),
|
|
5523
5699
|
isTsFile: options === null || options === void 0 ? void 0 : options.isTsFile,
|
|
5524
5700
|
exportedNames,
|