From 66e81f37d0dacead96a1b3e4346edc3a6d9cabe2 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 1 Apr 2024 22:40:40 +0200 Subject: [PATCH] fix: ensure type widening for bind:group #2316 --- packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts | 8 ++++++-- .../htmlx2jsx/samples/binding-group-bare/expectedv2.js | 4 ++-- .../test/htmlx2jsx/samples/binding-group/expectedv2.js | 2 +- .../test/htmlx2jsx/samples/editing-binding/expectedv2.js | 2 +- .../svelte2tsx/samples/binding-group-store/expectedv2.ts | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts index b4c6add56..7f7f9dd3e 100644 --- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts +++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts @@ -59,9 +59,13 @@ export function handleBinding( ): void { // bind group on input if (element instanceof Element && attr.name == 'group' && parent.name == 'input') { + // add reassignment to force TS to widen the type of the declaration (in case it's never reassigned anywhere else) + const expressionStr = str.original.substring( + attr.expression.start, + getEnd(attr.expression) + ); element.appendToStartEnd([ - rangeWithTrailingPropertyAccess(str.original, attr.expression), - ';' + surroundWithIgnoreComments(`() => ${expressionStr} = __sveltets_2_any(null);`) ]); return; } diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group-bare/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group-bare/expectedv2.js index 6661e2705..ada26a755 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group-bare/expectedv2.js +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group-bare/expectedv2.js @@ -1,2 +1,2 @@ - { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});group;} - { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});group;} \ No newline at end of file + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});/*Ωignore_startΩ*/() => group = __sveltets_2_any(null);/*Ωignore_endΩ*/} + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});/*Ωignore_startΩ*/() => group = __sveltets_2_any(null);/*Ωignore_endΩ*/} \ No newline at end of file diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group/expectedv2.js index 3f0525443..e4b496cfb 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group/expectedv2.js +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/binding-group/expectedv2.js @@ -1 +1 @@ - { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});tortilla;} \ No newline at end of file + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});/*Ωignore_startΩ*/() => tortilla = __sveltets_2_any(null);/*Ωignore_endΩ*/} \ No newline at end of file diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expectedv2.js index 84c329d67..196a5d4ba 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expectedv2.js +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expectedv2.js @@ -1,3 +1,3 @@ - { svelteHTML.createElement("input", { });obj.;} + { svelteHTML.createElement("input", { });/*Ωignore_startΩ*/() => obj = __sveltets_2_any(null);/*Ωignore_endΩ*/} { svelteHTML.createElement("input", { "bind:value":obj.,});/*Ωignore_startΩ*/() => obj = __sveltets_2_any(null);/*Ωignore_endΩ*/} { const $$_tupnI0C = __sveltets_2_ensureComponent(Input); new $$_tupnI0C({ target: __sveltets_2_any(), props: { value:obj.,}});/*Ωignore_startΩ*/() => obj = __sveltets_2_any(null);/*Ωignore_endΩ*/} \ No newline at end of file diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/binding-group-store/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/binding-group-store/expectedv2.ts index d8e1d4ea5..efbe52b6a 100644 --- a/packages/svelte2tsx/test/svelte2tsx/samples/binding-group-store/expectedv2.ts +++ b/packages/svelte2tsx/test/svelte2tsx/samples/binding-group-store/expectedv2.ts @@ -1,6 +1,6 @@ /// ;function render() { -async () => { { svelteHTML.createElement("input", { "id":`dom-input`,"type":`radio`,"value":`dom`,});$compile_options.generate;}}; +async () => { { svelteHTML.createElement("input", { "id":`dom-input`,"type":`radio`,"value":`dom`,});/*Ωignore_startΩ*/() => $compile_options.generate = __sveltets_2_any(null);/*Ωignore_endΩ*/}}; return { props: /** @type {Record} */ ({}), slots: {}, events: {} }} export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {