1- import { stripStateAndPropsRefs } from '../../helpers/strip-state-and-props-refs' ;
21import { selfClosingTags } from '../../parsers/jsx' ;
32import { MitosisComponent } from '../../types/mitosis-component' ;
43import { BaseNode , Binding , ForNode , MitosisNode } from '../../types/mitosis-node' ;
@@ -9,8 +8,8 @@ import { isSlotProperty, stripSlotPrefix } from '../../helpers/slots';
98import { VALID_HTML_TAGS } from '../../constants/html_tags' ;
109import { isUpperCase } from '../../helpers/is-upper-case' ;
1110import { getForArguments } from '../../helpers/nodes/for' ;
12- import { stripStateAndProps } from './helpers' ;
1311import { ToSvelteOptions } from './types' ;
12+ import { stripStateAndProps } from './helpers' ;
1413
1514const mappers : {
1615 For : BlockToSvelte < ForNode > ;
@@ -42,16 +41,14 @@ const mappers: {
4241 const args = getForArguments ( json , { excludeCollectionName : true } ) . join ( ', ' ) ;
4342
4443 return `
45- {#each ${ stripStateAndProps ( json . bindings . each ?. code , options ) } as ${ args } ${
46- keyValue ? `(${ keyValue } )` : ''
47- } }
44+ {#each ${ json . bindings . each ?. code } as ${ args } ${ keyValue ? `(${ keyValue } )` : '' } }
4845${ json . children . map ( ( item ) => blockToSvelte ( { json : item , options, parentComponent } ) ) . join ( '\n' ) }
4946{/each}
5047` ;
5148 } ,
5249 Show : ( { json, options, parentComponent } ) => {
5350 return `
54- {#if ${ stripStateAndProps ( json . bindings . when ?. code , options ) } }
51+ {#if ${ json . bindings . when ?. code } }
5552${ json . children . map ( ( item ) => blockToSvelte ( { json : item , options, parentComponent } ) ) . join ( '\n' ) }
5653
5754 ${
@@ -75,21 +72,20 @@ ${json.children.map((item) => blockToSvelte({ json: item, options, parentCompone
7572
7673 return `
7774 <span #${ key } >
78- ${ stripStateAndPropsRefs ( json . bindings [ key ] ?. code ) }
75+ ${ json . bindings [ key ] ?. code }
7976 </span>
8077 ` ;
8178 }
82- const strippedTextCode = stripStateAndPropsRefs ( json . bindings . name . code ) ;
8379
84- return `<slot name="${ stripSlotPrefix ( strippedTextCode ) . toLowerCase ( ) } ">${ json . children
80+ return `<slot name="${ stripSlotPrefix ( json . bindings . name . code ) . toLowerCase ( ) } ">${ json . children
8581 ?. map ( ( item ) => blockToSvelte ( { json : item , options, parentComponent } ) )
8682 . join ( '\n' ) } </slot>`;
8783 } ,
8884} ;
8985
9086const BINDINGS_MAPPER = {
9187 innerHTML : ( json : MitosisNode , options : ToSvelteOptions ) =>
92- `{@html ${ stripStateAndPropsRefs ( json . bindings . innerHTML ?. code ) } }` ,
88+ `{@html ${ json . bindings . innerHTML ?. code } }` ,
9389} ;
9490
9591const SVELTE_SPECIAL_TAGS = {
@@ -101,9 +97,11 @@ const SVELTE_SPECIAL_TAGS = {
10197const getTagName = ( {
10298 json,
10399 parentComponent,
100+ options,
104101} : {
105102 json : MitosisNode ;
106103 parentComponent : MitosisComponent ;
104+ options : ToSvelteOptions ;
107105} ) => {
108106 if ( parentComponent && json . name === parentComponent . name ) {
109107 return SVELTE_SPECIAL_TAGS . SELF ;
@@ -115,10 +113,13 @@ const getTagName = ({
115113 const hasMatchingImport = parentComponent . imports . some ( ( { imports } ) =>
116114 Object . keys ( imports ) . some ( ( name ) => name === json . name ) ,
117115 ) ;
116+
118117 // TO-DO: no way to decide between <svelte:component> and <svelte:element>...need to do that through metadata
119118 // overrides for now
120119 if ( ! isValidHtmlTag && ! isSpecialSvelteTag && ! hasMatchingImport ) {
121- json . bindings . this = { code : json . name } ;
120+ json . bindings . this = {
121+ code : stripStateAndProps ( { json : parentComponent , options } ) ( json . name ) ,
122+ } ;
122123 return SVELTE_SPECIAL_TAGS . COMPONENT ;
123124 }
124125
@@ -139,26 +140,25 @@ const stringifyBinding =
139140 }
140141
141142 const { code, arguments : cusArgs = [ 'event' ] , type } = binding ;
142- const useValue = stripStateAndProps ( code , options ) ;
143143
144144 if ( type === 'spread' ) {
145- const spreadValue = key === 'props' ? '$$props' : useValue ;
145+ const spreadValue = key === 'props' ? '$$props' : code ;
146146 return ` {...${ spreadValue } } ` ;
147147 } else if ( key . startsWith ( 'on' ) ) {
148148 const event = key . replace ( 'on' , '' ) . toLowerCase ( ) ;
149149 // TODO: handle quotes in event handler values
150150
151- const valueWithoutBlock = removeSurroundingBlock ( useValue ) ;
151+ const valueWithoutBlock = removeSurroundingBlock ( code ) ;
152152
153153 if ( valueWithoutBlock === key ) {
154154 return ` on:${ event } ={${ valueWithoutBlock } } ` ;
155155 } else {
156156 return ` on:${ event } ="{${ cusArgs . join ( ',' ) } => {${ valueWithoutBlock } }}" ` ;
157157 }
158158 } else if ( key === 'ref' ) {
159- return ` bind:this={${ useValue } } ` ;
159+ return ` bind:this={${ code } } ` ;
160160 } else {
161- return ` ${ key } ={${ useValue } } ` ;
161+ return ` ${ key } ={${ code } } ` ;
162162 }
163163 } ;
164164
@@ -171,9 +171,9 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
171171 } ) ;
172172 }
173173
174- const tagName = getTagName ( { json, parentComponent } ) ;
174+ const tagName = getTagName ( { json, parentComponent, options } ) ;
175175
176- if ( isChildren ( json ) ) {
176+ if ( isChildren ( { node : json , extraMatches : [ '$$slots.default' ] } ) ) {
177177 return `<slot></slot>` ;
178178 }
179179
@@ -184,11 +184,10 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
184184 const textCode = json . bindings . _text ?. code ;
185185
186186 if ( textCode ) {
187- const strippedTextCode = stripStateAndProps ( textCode , options ) ;
188- if ( isSlotProperty ( strippedTextCode ) ) {
189- return `<slot name="${ stripSlotPrefix ( strippedTextCode ) . toLowerCase ( ) } "/>` ;
187+ if ( isSlotProperty ( textCode ) ) {
188+ return `<slot name="${ stripSlotPrefix ( textCode ) . toLowerCase ( ) } "/>` ;
190189 }
191- return `{${ strippedTextCode } }` ;
190+ return `{${ textCode } }` ;
192191 }
193192
194193 let str = '' ;
@@ -197,10 +196,7 @@ export const blockToSvelte: BlockToSvelte = ({ json, options, parentComponent })
197196
198197 const isComponent = Boolean ( tagName [ 0 ] && isUpperCase ( tagName [ 0 ] ) ) ;
199198 if ( ( json . bindings . style ?. code || json . properties . style ) && ! isComponent ) {
200- const useValue = stripStateAndProps (
201- json . bindings . style ?. code || json . properties . style ,
202- options ,
203- ) ;
199+ const useValue = json . bindings . style ?. code || json . properties . style ;
204200
205201 str += `use:mitosis_styling={${ useValue } }` ;
206202 delete json . bindings . style ;
0 commit comments