@@ -775,3 +775,96 @@ func TestGetObjectKeys(t *testing.T) {
775775 })
776776 }
777777}
778+
779+ func TestGetPropsType (t * testing.T ) {
780+ tests := []struct {
781+ name string
782+ source string
783+ want Props
784+ }{
785+ {
786+ name : "no props" ,
787+ source : `const foo = "bar"` ,
788+ want : Props {
789+ Ident : "Record<string, any>" ,
790+ Statement : "" ,
791+ Generics : "" ,
792+ },
793+ },
794+ {
795+ name : "interface Props" ,
796+ source : `interface Props {
797+ foo: string;
798+ }` ,
799+ want : Props {
800+ Ident : "Props" ,
801+ Statement : "" ,
802+ Generics : "" ,
803+ },
804+ },
805+ {
806+ name : "type Props" ,
807+ source : `type Props = {
808+ foo: string;
809+ }` ,
810+ want : Props {
811+ Ident : "Props" ,
812+ Statement : "" ,
813+ Generics : "" ,
814+ },
815+ },
816+ {
817+ name : "Props with generics" ,
818+ source : `interface Props<T> {
819+ foo: T;
820+ }` ,
821+ want : Props {
822+ Ident : "Props" ,
823+ Statement : "<T>" ,
824+ Generics : "<T>" ,
825+ },
826+ },
827+ {
828+ name : "destructuring with 'as' prop name without type assertion - issue #927" ,
829+ source : `interface Props {
830+ as?: string;
831+ href?: string;
832+ }
833+ const { as: Component, href } = Astro.props;` ,
834+ want : Props {
835+ Ident : "Props" ,
836+ Statement : "" ,
837+ Generics : "" ,
838+ },
839+ },
840+ {
841+ name : "destructuring with 'as' prop name with type assertion" ,
842+ source : `interface Props {
843+ as?: string;
844+ href?: string;
845+ }
846+ const { as: Component, href } = Astro.props as Props;` ,
847+ want : Props {
848+ Ident : "Props" ,
849+ Statement : "" ,
850+ Generics : "" ,
851+ },
852+ },
853+ }
854+
855+ for _ , tt := range tests {
856+ t .Run (tt .name , func (t * testing.T ) {
857+ got := GetPropsType ([]byte (tt .source ))
858+ if got .Ident != tt .want .Ident {
859+ t .Errorf ("Ident: got %q, want %q" , got .Ident , tt .want .Ident )
860+ t .Logf ("Source:\n %s" , tt .source )
861+ }
862+ if got .Statement != tt .want .Statement {
863+ t .Errorf ("Statement: got %q, want %q" , got .Statement , tt .want .Statement )
864+ }
865+ if got .Generics != tt .want .Generics {
866+ t .Errorf ("Generics: got %q, want %q" , got .Generics , tt .want .Generics )
867+ }
868+ })
869+ }
870+ }
0 commit comments