Skip to content

Commit

Permalink
fix: deduplicate exports (#2083)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jul 6, 2023
1 parent d213200 commit 25a531f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/svelte2tsx/src/svelte2tsx/addComponentExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,25 @@ function addSimpleComponentExport({

let statement: string;
if (mode === 'dts' && isTsFile) {
const addTypeExport = (type: string) => {
const exportName = className + type;

if (!str.original.includes(exportName)) {
return `export type ${exportName} = typeof __propDef.${type.toLowerCase()};\n`;
}

let replacement = exportName + '_';
while (str.original.includes(replacement)) {
replacement += '_';
}
return `type ${replacement} = typeof __propDef.${type.toLowerCase()};\nexport { ${replacement} as ${exportName} };\n`;
};

statement =
`\nconst __propDef = ${propDef};\n` +
`export type ${className}Props = typeof __propDef.props;\n` +
`export type ${className}Events = typeof __propDef.events;\n` +
`export type ${className}Slots = typeof __propDef.slots;\n` +
addTypeExport('Props') +
addTypeExport('Events') +
addTypeExport('Slots') +
`\n${doc}export default class${
className ? ` ${className}` : ''
} extends SvelteComponentTyped<${className}Props, ${className}Events, ${className}Slots> {` +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SvelteComponentTyped } from "svelte";
import { type TestProps } from './foo';
declare const __propDef: {
props: {
p: TestProps;
x?: {
x: {
b: string;
};
};
};
events: {
[evt: string]: CustomEvent<any>;
};
slots: {};
};
type TestProps_ = typeof __propDef.props;
export { TestProps_ as TestProps };
export type TestEvents = typeof __propDef.events;
export type TestSlots = typeof __propDef.slots;
export default class Test extends SvelteComponentTyped<TestProps, TestEvents, TestSlots> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type TestProps = {
a: true;
};
type EventProps = {
b: string;
};
export declare function eventProps(): {
x: EventProps;
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import { eventProps, type TestProps } from './foo';
export let p: TestProps;
export let x = eventProps();
</script>

{p}
{x}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type TestProps = { a: true };

type EventProps = { b: string };

export function eventProps(): { x: EventProps } {
return { x: { b: 'b' } };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020", "DOM"],
"target": "es2019",
"isolatedModules": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowJs": true,
"checkJs": true
},
"include": ["./src/**/*.d.ts", "./src/**/*.js", "./src/**/*.ts", "./src/**/*.svelte"]
}

0 comments on commit 25a531f

Please sign in to comment.