From fcc148d20e274470c079d33805ff6287784a5772 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 25 Apr 2024 10:15:30 +0200 Subject: [PATCH] fix: better `bind:group` transformation (#2349) reuse the one way binding transformation to get autocompletion, error diagnostics etc #2337 --- 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 +- .../htmlx2jsx/samples/editing-binding/expected-svelte5.js | 2 +- .../test/htmlx2jsx/samples/editing-binding/expectedv2.js | 2 +- .../svelte2tsx/samples/binding-group-store/expectedv2.ts | 2 +- 6 files changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts index 724a169ab..16bffb8b8 100644 --- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts +++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts @@ -61,13 +61,7 @@ export function handleBinding( // 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([ - surroundWithIgnoreComments(`() => ${expressionStr} = __sveltets_2_any(null);`) - ]); + appendOneWayBinding(attr, ' = __sveltets_2_any(null)', element); 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 ada26a755..afb869c04 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`,});/*Ω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 + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});group = __sveltets_2_any(null);} + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});group = __sveltets_2_any(null);} \ 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 e4b496cfb..cf24f323b 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`,});/*Ωignore_startΩ*/() => tortilla = __sveltets_2_any(null);/*Ωignore_endΩ*/} \ No newline at end of file + { svelteHTML.createElement("input", { "type":`radio`,"value":`Plain`,});tortilla = __sveltets_2_any(null);} \ No newline at end of file diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expected-svelte5.js b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expected-svelte5.js index e5a62b562..7bcb65399 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expected-svelte5.js +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-binding/expected-svelte5.js @@ -1,3 +1,3 @@ - { svelteHTML.createElement("input", { });/*Ωignore_startΩ*/() => obj = __sveltets_2_any(null);/*Ωignore_endΩ*/} + { svelteHTML.createElement("input", { });obj = __sveltets_2_any(null);} { 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:__sveltets_2_binding(obj.),}});/*Ωignore_startΩ*/() => obj = __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 196a5d4ba..b880269e3 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", { });/*Ωignore_startΩ*/() => obj = __sveltets_2_any(null);/*Ωignore_endΩ*/} + { svelteHTML.createElement("input", { });obj = __sveltets_2_any(null);} { 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 efbe52b6a..c4c1df6fe 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`,});/*Ωignore_startΩ*/() => $compile_options.generate = __sveltets_2_any(null);/*Ωignore_endΩ*/}}; +async () => { { svelteHTML.createElement("input", { "id":`dom-input`,"type":`radio`,"value":`dom`,});$compile_options.generate = __sveltets_2_any(null);}}; return { props: /** @type {Record} */ ({}), slots: {}, events: {} }} export default class Input__SvelteComponent_ extends __sveltets_2_createSvelte2TsxComponent(__sveltets_2_partial(__sveltets_2_with_any_event(render()))) {