@@ -60,6 +60,22 @@ export function handleBinding(
6060 isSvelte5Plus : boolean
6161) : void {
6262 const isGetSetBinding = attr . expression . type === 'SequenceExpression' ;
63+ const [ get , set ] = isGetSetBinding ? ( attr . expression as SequenceExpression ) . expressions : [ ] ;
64+
65+ // bind this
66+ if ( attr . name === 'this' && supportsBindThis . includes ( parent . type ) ) {
67+ // bind:this is effectively only works bottom up - the variable is updated by the element, not
68+ // the other way round. So we check if the instance is assignable to the variable.
69+ // Note: If the component unmounts (it's inside an if block, or svelte:component this={null},
70+ // the value becomes null, but we don't add it to the clause because it would introduce
71+ // worse DX for the 99% use case, and because null !== undefined which others might use to type the declaration.
72+ if ( isGetSetBinding ) {
73+ element . appendToStartEnd ( [ '(' , [ set . start , getEnd ( set ) ] , `)(${ element . name } );` ] ) ;
74+ } else {
75+ appendOneWayBinding ( attr , ` = ${ element . name } ` , element ) ;
76+ }
77+ return ;
78+ }
6379
6480 if ( ! isGetSetBinding ) {
6581 // bind group on input
@@ -69,17 +85,6 @@ export function handleBinding(
6985 return ;
7086 }
7187
72- // bind this
73- if ( attr . name === 'this' && supportsBindThis . includes ( parent . type ) ) {
74- // bind:this is effectively only works bottom up - the variable is updated by the element, not
75- // the other way round. So we check if the instance is assignable to the variable.
76- // Note: If the component unmounts (it's inside an if block, or svelte:component this={null},
77- // the value becomes null, but we don't add it to the clause because it would introduce
78- // worse DX for the 99% use case, and because null !== undefined which others might use to type the declaration.
79- appendOneWayBinding ( attr , ` = ${ element . name } ` , element ) ;
80- return ;
81- }
82-
8388 // one way binding
8489 if ( oneWayBindingAttributes . has ( attr . name ) && element instanceof Element ) {
8590 appendOneWayBinding ( attr , `= ${ element . name } .${ attr . name } ` , element ) ;
@@ -125,7 +130,6 @@ export function handleBinding(
125130 ]
126131 ] ;
127132
128- const [ get , set ] = isGetSetBinding ? ( attr . expression as SequenceExpression ) . expressions : [ ] ;
129133 const value : TransformationArray | undefined = isShorthand
130134 ? preserveBind && element instanceof Element
131135 ? [ rangeWithTrailingPropertyAccess ( str . original , attr . expression ) ]
0 commit comments