Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions packages/svelte2tsx/src/htmlxtojsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import svelte from 'svelte/compiler';
import { Node } from 'estree-walker';
import { parseHtmlx } from './htmlxparser';
import svgAttributes from './svgattributes';
import { getTypeForComponent } from './nodes/component-type';

type ElementType = string;
const oneWayBindingAttributes: Map<string, ElementType> = new Map(
Expand Down Expand Up @@ -73,7 +74,7 @@ export function convertHtmlxToJsx(
const on = 'on';
//for handler assignment, we changeIt to call to our __sveltets_ensureFunction
str.appendRight(
attr.start, `{__sveltets_instanceOf(${parent.name}).$`
attr.start, `{__sveltets_instanceOf(${getTypeForComponent(parent)}).$`
);
const eventNameIndex = htmlx.indexOf(':', attr.start) + 1;
str.overwrite(
Expand Down Expand Up @@ -172,17 +173,11 @@ export function convertHtmlxToJsx(
};

const handleBinding = (attr: Node, el: Node) => {
const getThisTypeForComponent = (node: Node) => {
if (node.name === 'svelte:component' || node.name === 'svelte:self') {
return '__sveltets_componentType()';
} else {
return node.name;
}
};

const getThisType = (node: Node) => {
switch (node.type) {
case 'InlineComponent':
return getThisTypeForComponent(node);
return getTypeForComponent(node);
case 'Element':
return 'HTMLElement';
case 'Body':
Expand Down Expand Up @@ -229,7 +224,7 @@ export function convertHtmlxToJsx(
str.appendLeft(
attr.end,
`=__sveltets_instanceOf(${oneWayBindingAttributes.get(attr.name)}).${
attr.name
attr.name
})}`,
);
} else {
Expand All @@ -238,7 +233,7 @@ export function convertHtmlxToJsx(
attr.expression.end,
attr.end,
`=__sveltets_instanceOf(${oneWayBindingAttributes.get(attr.name)}).${
attr.name
attr.name
})}`,
);
}
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte2tsx/src/nodes/component-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Node } from 'estree-walker';

export function getTypeForComponent(node: Node) {
if (node.name === 'svelte:component' || node.name === 'svelte:self') {
return '__sveltets_componentType()';
} else {
return node.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<><sveltecomponent this={Whatever} />{__sveltets_instanceOf(__sveltets_componentType()).$on('submit', handleSubmit)}</>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:component this={Whatever} on:submit={handleSubmit} />