88} from "ts-morph" ;
99import type {
1010 BooleanType ,
11- LiteralType ,
11+ LiteralType ,
1212 NullType ,
1313 ObjectType ,
1414 PropDescriptor ,
@@ -55,7 +55,6 @@ export function parseProps(
5555}
5656
5757function parseType ( type : Type < ts . Type > ) : TypeDescriptor {
58-
5958 if ( type . isUnion ( ) ) {
6059 if ( type . getText ( ) === "boolean" ) {
6160 return {
@@ -73,56 +72,64 @@ function parseType(type: Type<ts.Type>): TypeDescriptor {
7372 if ( type . isBooleanLiteral ( ) ) {
7473 return {
7574 name : "literal" ,
76- value : type . getText ( ) === "true" ,
75+ value : type . getText ( ) === "true" ,
7776 } as LiteralType ;
7877 }
7978
8079 if ( type . isLiteral ( ) ) {
8180 return {
8281 name : "literal" ,
83- value : type . getLiteralValue ( ) ,
82+ value : type . getLiteralValue ( ) ,
8483 } as LiteralType ;
8584 }
8685
87- if ( type . isUndefined ( ) ) {
88- return {
89- name : "undefined" ,
90- } as UndefinedType ;
91- }
86+ if ( type . isUndefined ( ) ) {
87+ return {
88+ name : "undefined" ,
89+ } as UndefinedType ;
90+ }
9291
93- if ( type . isNull ( ) ) {
94- return {
95- name : "null" ,
96- } as NullType ;
97- }
92+ if ( type . isNull ( ) ) {
93+ return {
94+ name : "null" ,
95+ } as NullType ;
96+ }
9897
9998 if ( type . isObject ( ) || type . isInterface ( ) || type . isAnonymous ( ) ) {
10099 return {
101100 name : "object" ,
102- properties : Object . fromEntries ( type . getProperties ( ) . map ( ( prop ) => {
103- let parsed = parseType ( prop . getValueDeclarationOrThrow ( ) . getType ( ) ) ;
104-
105- if ( parsed . name === "union" && prop . isOptional ( ) ) {
106- if ( parsed . values . filter ( ( v ) => v . name !== "undefined" ) . length === 1 ) {
107- parsed = parsed . values . filter ( ( v ) => v . name !== "undefined" ) [ 0 ] ;
108- } else if ( parsed . values . filter ( ( v ) => v . name !== "undefined" ) . length > 1 ) {
109- parsed . values = parsed . values . filter ( ( v ) => v . name !== "undefined" ) ;
101+ properties : Object . fromEntries (
102+ type . getProperties ( ) . map ( ( prop ) => {
103+ let parsed = parseType ( prop . getValueDeclarationOrThrow ( ) . getType ( ) ) ;
104+
105+ if ( parsed . name === "union" && prop . isOptional ( ) ) {
106+ if (
107+ parsed . values . filter ( ( v ) => v . name !== "undefined" ) . length === 1
108+ ) {
109+ parsed = parsed . values . filter ( ( v ) => v . name !== "undefined" ) [ 0 ] ;
110+ } else if (
111+ parsed . values . filter ( ( v ) => v . name !== "undefined" ) . length > 1
112+ ) {
113+ parsed . values = parsed . values . filter (
114+ ( v ) => v . name !== "undefined" ,
115+ ) ;
116+ }
110117 }
111- }
112-
113- return [
114- prop . getName ( ) ,
115- {
116- ... parsed ,
117- required : ! prop . isOptional ( ) ,
118- } ,
119- ]
120- } ) ) ,
118+
119+ return [
120+ prop . getName ( ) ,
121+ {
122+ ... parsed ,
123+ required : ! prop . isOptional ( ) ,
124+ } ,
125+ ] ;
126+ } ) ,
127+ ) ,
121128 } as ObjectType ;
122129 }
123-
130+
124131 return {
125132 name : "unknown" ,
126- raw : type . getText ( ) ,
133+ raw : type . getText ( ) ,
127134 } ;
128135}
0 commit comments