From 28363faa1cd6b6da76517242f9f6ce34e431e14c Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sun, 20 Jun 2021 10:07:04 +0200 Subject: [PATCH 1/2] (fix) correct transformation of as expression in reactive assignment #1046 --- .../src/svelte2tsx/nodes/ImplicitTopLevelNames.ts | 6 +++--- .../reactive-assignment-type-cast/expected.tsx | 11 +++++++++++ .../reactive-assignment-type-cast/input.svelte | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/expected.tsx create mode 100644 packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/input.svelte diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts index f8d9c7618..938d2540a 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts @@ -38,8 +38,8 @@ export class ImplicitTopLevelNames { const start = expression.getStart() + this.astOffset; const end = expression.getEnd() + this.astOffset; - // () => ({}) - if (ts.isObjectLiteralExpression(expression)) { + // $: a = { .. } / $: a = .. as .. => () => ( .. ) + if (ts.isObjectLiteralExpression(expression) || ts.isAsExpression(expression)) { this.str.appendLeft(start, '('); this.str.appendRight(end, ')'); } @@ -89,7 +89,7 @@ export class ImplicitTopLevelNames { const start = node.statement.expression.getStart() + this.astOffset; this.str.overwrite(start, start + 1, '', { contentOnly: true }); const end = node.statement.expression.getEnd() + this.astOffset - 1; - // We need to keep the `)` of the "wrap with invalidate" expression above. + // We need to keep the `))` of the "wrap with invalidate" expression above. // We overwrite the same range so it's needed. this.str.overwrite(end, end + 1, ')', { contentOnly: true }); } diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/expected.tsx new file mode 100644 index 000000000..759939a62 --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/expected.tsx @@ -0,0 +1,11 @@ +/// +<>;function render() { + + type Team = any; + let team = __sveltets_invalidate(() => ({ search: "Real", players: [] } as Team)); +; +() => (<>); +return { props: {}, slots: {}, getters: {}, events: {} }} + +export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(__sveltets_with_any_event(render()))) { +} \ No newline at end of file diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/input.svelte b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/input.svelte new file mode 100644 index 000000000..7a3b827ae --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-assignment-type-cast/input.svelte @@ -0,0 +1,4 @@ + From 53ea18ac6ea9a6ffa79394b76cd1f7cd59353ce5 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Sun, 20 Jun 2021 10:07:57 +0200 Subject: [PATCH 2/2] undo --- .../svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts index 938d2540a..71596cfaf 100644 --- a/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts +++ b/packages/svelte2tsx/src/svelte2tsx/nodes/ImplicitTopLevelNames.ts @@ -89,7 +89,7 @@ export class ImplicitTopLevelNames { const start = node.statement.expression.getStart() + this.astOffset; this.str.overwrite(start, start + 1, '', { contentOnly: true }); const end = node.statement.expression.getEnd() + this.astOffset - 1; - // We need to keep the `))` of the "wrap with invalidate" expression above. + // We need to keep the `)` of the "wrap with invalidate" expression above. // We overwrite the same range so it's needed. this.str.overwrite(end, end + 1, ')', { contentOnly: true }); }