Skip to content

Commit 43b0411

Browse files
authored
(fix) emitDts for components with no script (#1114)
* (fix) emitDts for components with no script * format * format * (fix) robust tsx replace strategy
1 parent 25c296b commit 43b0411

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

packages/svelte2tsx/src/svelte2tsx/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,14 @@ declare function __sveltets_1_createSvelteComponentTyped<Props, Events, Slots>(
401401
);
402402
let code = str.toString();
403403
// Remove all tsx occurences and the template part from the output
404-
code =
405-
code
406-
.substr(0, code.indexOf('\n() => (<>'))
407-
// prepended before each script block
408-
.replace('<></>;', '')
409-
.replace('<></>;', '') + code.substr(code.lastIndexOf('</>);') + '</>);'.length);
404+
code = code
405+
// prepended before each script block
406+
.replace('<></>;', '')
407+
.replace('<></>;', '')
408+
// tsx in render function
409+
.replace(/<>.*<\/>/s, '')
410+
.replace('\n() => ();', '');
411+
410412
return {
411413
code
412414
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @typedef {typeof __propDef.props} TestNoScriptProps */
2+
/** @typedef {typeof __propDef.events} TestNoScriptEvents */
3+
/** @typedef {typeof __propDef.slots} TestNoScriptSlots */
4+
export default class TestNoScript extends SvelteComponentTyped<{}, {
5+
click: MouseEvent;
6+
} & {
7+
[evt: string]: CustomEvent<any>;
8+
}, {
9+
default: {};
10+
}> {
11+
}
12+
export type TestNoScriptProps = typeof __propDef.props;
13+
export type TestNoScriptEvents = typeof __propDef.events;
14+
export type TestNoScriptSlots = typeof __propDef.slots;
15+
import { SvelteComponentTyped } from "svelte";
16+
declare const __propDef: {
17+
props: {};
18+
events: {
19+
click: MouseEvent;
20+
} & {
21+
[evt: string]: CustomEvent<any>;
22+
};
23+
slots: {
24+
default: {};
25+
};
26+
};
27+
export {};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div on:click>
2+
<slot />
3+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SvelteComponentTyped } from "svelte"
2+
3+
declare function __sveltets_1_createSvelteComponentTyped<Props, Events, Slots>(
4+
render: {props: Props, events: Events, slots: Slots }
5+
): SvelteComponentConstructor<SvelteComponentTyped<Props, Events, Slots>,Svelte2TsxComponentConstructorParameters<Props>>;
6+
7+
function render() {
8+
9+
return { props: {}, slots: {'default': {}}, getters: {}, events: {'click':__sveltets_1_mapElementEvent('click')} }}
10+
const __propDef = __sveltets_1_partial(__sveltets_1_with_any_event(render()));
11+
/** @typedef {typeof __propDef.props} InputProps */
12+
/** @typedef {typeof __propDef.events} InputEvents */
13+
/** @typedef {typeof __propDef.slots} InputSlots */
14+
15+
export default class Input extends __sveltets_1_createSvelteComponentTyped(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<button on:click><slot /></button>
2+
{#await Promise.resolve(0) then n}
3+
{n}
4+
{/await}

0 commit comments

Comments
 (0)