@@ -6,6 +6,7 @@ import { parseHtmlx } from './htmlxparser';
6
6
import { convertHtmlxToJsx } from './htmlxtojsx' ;
7
7
import { Node } from 'estree-walker' ;
8
8
import * as ts from 'typescript' ;
9
+ import { createEventHandlerTransformer } from './nodes/event-handler' ;
9
10
10
11
function AttributeValueAsJsExpression ( htmlx : string , attr : Node ) : string {
11
12
if ( attr . value . length == 0 ) return "''" ; //wut?
@@ -41,6 +42,7 @@ type TemplateProcessResult = {
41
42
moduleScriptTag : Node ;
42
43
/** To be added later as a comment on the default class export */
43
44
componentDocumentation : string | null ;
45
+ events : Map < string , string > ;
44
46
} ;
45
47
46
48
class Scope {
@@ -278,6 +280,8 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
278
280
str . remove ( node . start , node . end ) ;
279
281
} ;
280
282
283
+ const { handleEventHandler, getEvents } = createEventHandlerTransformer ( ) ;
284
+
281
285
const onHtmlxWalk = ( node : Node , parent : Node , prop : string ) => {
282
286
if (
283
287
prop == 'params' &&
@@ -314,6 +318,9 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
314
318
case 'ArrowFunctionExpression' :
315
319
enterArrowFunctionExpression ( ) ;
316
320
break ;
321
+ case 'EventHandler' :
322
+ handleEventHandler ( node , parent ) ;
323
+ break ;
317
324
case 'VariableDeclarator' :
318
325
isDeclaration = true ;
319
326
break ;
@@ -358,6 +365,7 @@ function processSvelteTemplate(str: MagicString): TemplateProcessResult {
358
365
moduleScriptTag,
359
366
scriptTag,
360
367
slots,
368
+ events : getEvents ( ) ,
361
369
uses$$props,
362
370
uses$$restProps,
363
371
componentDocumentation,
@@ -897,6 +905,7 @@ function createRenderFunction(
897
905
scriptTag : Node ,
898
906
scriptDestination : number ,
899
907
slots : Map < string , Map < string , string > > ,
908
+ events : Map < string , string > ,
900
909
exportedNames : ExportedNames ,
901
910
uses$$props : boolean ,
902
911
uses$$restProps : boolean ,
@@ -936,10 +945,13 @@ function createRenderFunction(
936
945
} )
937
946
. join ( ', ' ) +
938
947
'}' ;
948
+ const eventsDef = '{' + Array . from ( events . entries ( ) )
949
+ . map ( ( [ evnetName , expression ] ) => `'${ evnetName } ':${ expression } ` )
950
+ . join ( ', ' ) + '}' ;
939
951
940
952
const returnString = `\nreturn { props: ${ createPropsStr (
941
953
exportedNames ,
942
- ) } , slots: ${ slotsAsDef } , events: { } }}`;
954
+ ) } , slots: ${ slotsAsDef } , events: ${ eventsDef } }}`;
943
955
str . append ( returnString ) ;
944
956
}
945
957
@@ -979,6 +991,7 @@ export function svelte2tsx(svelte: string, options?: { filename?: string; strict
979
991
slots,
980
992
uses$$props,
981
993
uses$$restProps,
994
+ events,
982
995
componentDocumentation,
983
996
} = processSvelteTemplate ( str ) ;
984
997
@@ -1018,6 +1031,7 @@ export function svelte2tsx(svelte: string, options?: { filename?: string; strict
1018
1031
scriptTag ,
1019
1032
instanceScriptTarget ,
1020
1033
slots ,
1034
+ events ,
1021
1035
exportedNames ,
1022
1036
uses$$props ,
1023
1037
uses$$restProps ,
0 commit comments