Skip to content

Commit e737daf

Browse files
authored
fix svelte:component on:event (#419)
#418
1 parent e7d86d1 commit e737daf

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

packages/svelte2tsx/src/htmlxtojsx.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import svelte from 'svelte/compiler';
33
import { Node } from 'estree-walker';
44
import { parseHtmlx } from './htmlxparser';
55
import svgAttributes from './svgattributes';
6+
import { getTypeForComponent } from './nodes/component-type';
67

78
type ElementType = string;
89
const oneWayBindingAttributes: Map<string, ElementType> = new Map(
@@ -73,7 +74,7 @@ export function convertHtmlxToJsx(
7374
const on = 'on';
7475
//for handler assignment, we changeIt to call to our __sveltets_ensureFunction
7576
str.appendRight(
76-
attr.start, `{__sveltets_instanceOf(${parent.name}).$`
77+
attr.start, `{__sveltets_instanceOf(${getTypeForComponent(parent)}).$`
7778
);
7879
const eventNameIndex = htmlx.indexOf(':', attr.start) + 1;
7980
str.overwrite(
@@ -172,17 +173,11 @@ export function convertHtmlxToJsx(
172173
};
173174

174175
const handleBinding = (attr: Node, el: Node) => {
175-
const getThisTypeForComponent = (node: Node) => {
176-
if (node.name === 'svelte:component' || node.name === 'svelte:self') {
177-
return '__sveltets_componentType()';
178-
} else {
179-
return node.name;
180-
}
181-
};
176+
182177
const getThisType = (node: Node) => {
183178
switch (node.type) {
184179
case 'InlineComponent':
185-
return getThisTypeForComponent(node);
180+
return getTypeForComponent(node);
186181
case 'Element':
187182
return 'HTMLElement';
188183
case 'Body':
@@ -229,7 +224,7 @@ export function convertHtmlxToJsx(
229224
str.appendLeft(
230225
attr.end,
231226
`=__sveltets_instanceOf(${oneWayBindingAttributes.get(attr.name)}).${
232-
attr.name
227+
attr.name
233228
})}`,
234229
);
235230
} else {
@@ -238,7 +233,7 @@ export function convertHtmlxToJsx(
238233
attr.expression.end,
239234
attr.end,
240235
`=__sveltets_instanceOf(${oneWayBindingAttributes.get(attr.name)}).${
241-
attr.name
236+
attr.name
242237
})}`,
243238
);
244239
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Node } from 'estree-walker';
2+
3+
export function getTypeForComponent(node: Node) {
4+
if (node.name === 'svelte:component' || node.name === 'svelte:self') {
5+
return '__sveltets_componentType()';
6+
} else {
7+
return node.name;
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<><sveltecomponent this={Whatever} />{__sveltets_instanceOf(__sveltets_componentType()).$on('submit', handleSubmit)}</>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svelte:component this={Whatever} on:submit={handleSubmit} />

0 commit comments

Comments
 (0)