@@ -8,27 +8,32 @@ const fs = require('./utils/fs-extra.js');
88const { COLOR_PROPS , ICON_PROPS , ROUTER_PROPS , ACTIONS_PROPS } = require ( './ts-extend-props.js' ) ;
99
1010const TEMPLATE = `
11- import { SvelteComponent } from 'svelte';
11+ import { SvelteComponent, Snippet } from 'svelte';
1212import { HTMLAttributes } from 'svelte/elements';
1313{{IMPORTS}}
1414
15- interface {{componentName}} Props {
15+ interface Props {
1616 {{PROPS}}
1717}
1818
19+ interface {{componentName}}Props {}
20+ interface {{componentName}}Props extends Props {}
1921{{EXTENDS}}
22+ interface {{componentName}}Events extends Record<'',{}>{}
23+
24+
2025
2126declare class {{componentName}} extends SvelteComponent<
22- {{componentName}}Props & Omit<HTMLAttributes<HTMLElementTagNameMap['div']>, keyof {{componentName}}Props> ,
23- { {{EVENTS}} } ,
24- { {{SLOTS}} }
27+ {{componentName}}Props,
28+ {{componentName}}Events ,
29+ { }
2530> {}
2631
2732export { {{componentName}}Props };
2833export default {{componentName}};
2934` ;
3035
31- function generateComponentProps ( propsContent ) {
36+ function generateComponentProps ( propsContent , slots , events ) {
3237 // eslint-disable-next-line
3338 const props = { } ;
3439 propsContent
@@ -42,7 +47,7 @@ function generateComponentProps(propsContent) {
4247 . map ( ( line ) => line . trim ( ) )
4348 . filter ( ( line ) => ! ! line )
4449 . forEach ( ( line ) => {
45- const propName = line . split ( ':' ) [ 0 ] . replace ( '?' , '' ) ;
50+ const propName = line . split ( ':' ) [ 0 ] . replace ( '?' , '' ) . trim ( ) ;
4651 let propValue = line . split ( ':' ) . slice ( 1 ) . join ( ':' ) ;
4752 if ( propValue . charAt ( propValue . length - 1 ) === ';' ) {
4853 propValue = propValue . substr ( 0 , propValue . length - 1 ) ;
@@ -53,6 +58,21 @@ function generateComponentProps(propsContent) {
5358 [ 'id' , 'style' , 'className' , 'ref' , 'slot' , 'children' ] . forEach ( ( key ) => {
5459 delete props [ key ] ;
5560 } ) ;
61+ Object . keys ( props ) . forEach ( ( propName ) => {
62+ if ( slots . includes ( propName ) ) {
63+ props [ propName ] = `${ props [ propName ] } | Snippet` ;
64+ }
65+ } ) ;
66+ slots . forEach ( ( slotName ) => {
67+ if ( ! Object . keys ( props ) . includes ( slotName ) ) {
68+ props [ slotName ] = `Snippet` ;
69+ }
70+ } ) ;
71+ events . forEach ( ( eventName ) => {
72+ if ( ! Object . keys ( props ) . includes ( eventName ) ) {
73+ props [ eventName ] = `() => void` ;
74+ }
75+ } ) ;
5676 const content = Object . keys ( props )
5777 . map ( ( propName ) => `${ propName } ?: ${ props [ propName ] } ;` )
5878 . join ( '\n ' ) ;
@@ -74,29 +94,28 @@ function generateComponentTypings(componentName, fileContent, reactFileContent)
7494 propsExtends = reactFileContent . split ( '/* dts-extends' ) [ 1 ] . split ( '*/' ) [ 0 ] || '' ;
7595 }
7696 const slots = [ ] ;
77- fileContent . replace ( / < s l o t ( [ ^ > ] * ) \/ > / g, ( str , name ) => {
78- if ( ! name . trim ( ) ) name = 'default' ;
79- name = name . match ( / n a m e = " ( [ a - z - ] * ) " / ) ;
80- if ( name ) name = name [ 1 ] ;
81- else name = 'default' ;
97+ fileContent . replace ( / { @ r e n d e r ( [ a - z A - Z ] * ) / g, ( str , name ) => {
98+ if ( ! name . trim ( ) ) name = 'children' ;
8299 if ( ! slots . includes ( name ) ) slots . push ( name ) ;
83100 } ) ;
84- if ( fileContent . includes ( '<slot' ) && ! slots . includes ( 'default' ) ) slots . push ( 'default' ) ;
85- const slotsContent = slots . map ( ( slot ) => ` '${ slot } ' : {};` ) . join ( ' ' ) ;
101+ fileContent . replace ( / < S n i p p e t R e n d e r c o n t e n t = { ( [ a - z A - Z ] * ) / g, ( str , name ) => {
102+ if ( ! name . trim ( ) ) name = 'children' ;
103+ if ( ! slots . includes ( name ) ) slots . push ( name ) ;
104+ } ) ;
105+ if ( componentName === 'Navbar' ) {
106+ slots . push ( ...[ 'title' , 'subtitle' ] ) ;
107+ }
86108
87109 const events = [ ] ;
88- fileContent . replace ( / e m i t \( ' ( [ a - z A - Z ] * ) ' / g, ( str , name ) => {
89- if ( ! events . includes ( name ) ) events . push ( name ) ;
110+ fileContent . replace ( / r e s t P r o p s . o n ( [ a - z A - Z ] * ) / g, ( str , name ) => {
111+ if ( ! events . includes ( `on ${ name } ` ) ) events . push ( `on ${ name } ` ) ;
90112 } ) ;
91- const eventsContent = events . map ( ( event ) => `${ event } : CustomEvent<void>;` ) . join ( ' ' ) ;
92113
93114 // prettier-ignore
94115 return TEMPLATE
95116 . replace ( '{{IMPORTS}}' , imports )
96117 . replace ( '{{EXTENDS}}' , propsExtends ? ` interface ${ componentName } Props extends ${ propsExtends . trim ( ) } {}` : '' )
97- . replace ( '{{PROPS}}' , generateComponentProps ( props ) )
98- . replace ( '{{EVENTS}}' , eventsContent )
99- . replace ( '{{SLOTS}}' , slotsContent )
118+ . replace ( '{{PROPS}}' , generateComponentProps ( props , slots , events ) )
100119 . replace ( / { { componentName} } / g, componentName )
101120}
102121
0 commit comments