@@ -127,7 +127,7 @@ function declareNamedTypes(
127
127
return type
128
128
}
129
129
130
- function generateType ( ast : AST , options : Options , indentDepth : number ) : string {
130
+ function generateType ( ast : AST , options : Options ) : string {
131
131
log ( whiteBright . bgMagenta ( 'generator' ) , ast )
132
132
133
133
if ( hasStandaloneName ( ast ) ) {
@@ -137,94 +137,84 @@ function generateType(ast: AST, options: Options, indentDepth: number): string {
137
137
switch ( ast . type ) {
138
138
case 'ANY' : return 'any'
139
139
case 'ARRAY' : return ( ( ) => {
140
- let type = generateType ( ast . params , options , indentDepth + 1 )
140
+ let type = generateType ( ast . params , options )
141
141
return type . endsWith ( '"' ) ? '(' + type + ')[]' : type + '[]'
142
142
} ) ( )
143
143
case 'BOOLEAN' : return 'boolean'
144
- case 'INTERFACE' : return generateInterface ( ast , options , indentDepth + 1 )
145
- case 'INTERSECTION' : return generateSetOperation ( ast , options , indentDepth )
144
+ case 'INTERFACE' : return generateInterface ( ast , options )
145
+ case 'INTERSECTION' : return generateSetOperation ( ast , options )
146
146
case 'LITERAL' : return JSON . stringify ( ast . params )
147
147
case 'NUMBER' : return 'number'
148
148
case 'NULL' : return 'null'
149
149
case 'OBJECT' : return 'object'
150
150
case 'REFERENCE' : return ast . params
151
151
case 'STRING' : return 'string'
152
152
case 'TUPLE' : return '['
153
- + ast . params . map ( _ => generateType ( _ , options , indentDepth + 1 ) ) . join ( ', ' )
153
+ + ast . params . map ( _ => generateType ( _ , options ) ) . join ( ', ' )
154
154
+ ']'
155
- case 'UNION' : return generateSetOperation ( ast , options , indentDepth )
155
+ case 'UNION' : return generateSetOperation ( ast , options )
156
156
}
157
157
}
158
158
159
159
/**
160
160
* Generate a Union or Intersection
161
161
*/
162
- function generateSetOperation ( ast : TIntersection | TUnion , options : Options , indentDepth : number ) : string {
163
- const members = ( ast as TUnion ) . params . map ( _ => generateType ( _ , options , indentDepth ) )
162
+ function generateSetOperation ( ast : TIntersection | TUnion , options : Options ) : string {
163
+ const members = ( ast as TUnion ) . params . map ( _ => generateType ( _ , options ) )
164
164
const separator = ast . type === 'UNION' ? '|' : '&'
165
165
return members . length === 1 ? members [ 0 ] : '(' + members . join ( ' ' + separator + ' ' ) + ')'
166
166
}
167
167
168
168
function generateInterface (
169
169
ast : TInterface ,
170
- options : Options ,
171
- indentDepth : number
170
+ options : Options
172
171
) : string {
173
172
return `{`
174
173
+ '\n'
175
- + options . indentWith . repeat ( indentDepth )
176
174
+ ast . params
177
- . map ( ( { isRequired, keyName, ast } ) => [ isRequired , keyName , ast , generateType ( ast , options , indentDepth ) ] as [ boolean , string , AST , string ] )
175
+ . map ( ( { isRequired, keyName, ast } ) => [ isRequired , keyName , ast , generateType ( ast , options ) ] as [ boolean , string , AST , string ] )
178
176
. map ( ( [ isRequired , keyName , ast , type ] ) =>
179
- ( hasComment ( ast ) && ! ast . standaloneName ? generateComment ( ast . comment , options , indentDepth + 1 ) + '\n' : '' )
180
- + options . indentWith
177
+ ( hasComment ( ast ) && ! ast . standaloneName ? generateComment ( ast . comment ) + '\n' : '' )
181
178
+ escapeKeyName ( keyName )
182
179
+ ( isRequired ? '' : '?' )
183
180
+ ': '
184
181
+ ( hasStandaloneName ( ast ) ? toSafeString ( type ) : type )
185
- + ( options . enableTrailingSemicolonForInterfaceProperties ? ';' : '' )
186
182
)
187
- . join ( '\n' + options . indentWith . repeat ( indentDepth ) )
183
+ . join ( '\n' )
188
184
+ '\n'
189
- + options . indentWith . repeat ( indentDepth ) + '}'
185
+ + '}'
190
186
}
191
187
192
- function generateComment ( comment : string , options : Options , indentDepth : number ) : string {
193
- return options . indentWith . repeat ( indentDepth )
194
- + [
195
- '/**' ,
196
- ...comment . split ( '\n' ) . map ( _ => ' * ' + _ ) ,
197
- ' */'
198
- ] . join ( '\n' + options . indentWith . repeat ( indentDepth ) )
188
+ function generateComment ( comment : string ) : string {
189
+ return [
190
+ '/**' ,
191
+ ...comment . split ( '\n' ) . map ( _ => ' * ' + _ ) ,
192
+ ' */'
193
+ ] . join ( '\n' )
199
194
}
200
195
201
196
function generateStandaloneEnum ( ast : TEnum , options : Options ) : string {
202
- return ( hasComment ( ast ) ? generateComment ( ast . comment , options , 0 ) + '\n' : '' )
197
+ return ( hasComment ( ast ) ? generateComment ( ast . comment ) + '\n' : '' )
203
198
+ 'export ' + ( options . enableConstEnums ? 'const ' : '' ) + `enum ${ toSafeString ( ast . standaloneName ) } {`
204
199
+ '\n'
205
200
+ ast . params . map ( ( { ast, keyName } ) =>
206
- options . indentWith
207
- + keyName
208
- + ' = '
209
- + generateType ( ast , options , 0 )
201
+ keyName + ' = ' + generateType ( ast , options )
210
202
)
211
203
. join ( ',\n' )
212
204
+ '\n'
213
205
+ '}'
214
206
}
215
207
216
208
function generateStandaloneInterface ( ast : TNamedInterface , options : Options ) : string {
217
- return ( hasComment ( ast ) ? generateComment ( ast . comment , options , 0 ) + '\n' : '' )
209
+ return ( hasComment ( ast ) ? generateComment ( ast . comment ) + '\n' : '' )
218
210
+ `export interface ${ toSafeString ( ast . standaloneName ) } `
219
211
+ ( ast . superTypes . length > 0 ? `extends ${ ast . superTypes . map ( superType => toSafeString ( superType . standaloneName ) ) . join ( ', ' ) } ` : '' )
220
- + generateInterface ( ast , options , 0 )
221
- + ( options . enableTrailingSemicolonForInterfaces ? ';' : '' )
212
+ + generateInterface ( ast , options )
222
213
}
223
214
224
215
function generateStandaloneType ( ast : ASTWithStandaloneName , options : Options ) : string {
225
- return ( hasComment ( ast ) ? generateComment ( ast . comment , options , 0 ) + '\n' : '' )
226
- + `export type ${ toSafeString ( ast . standaloneName ) } = ${ generateType ( omit < ASTWithStandaloneName , AST > ( ast , 'standaloneName' ) , options , 0 ) } `
227
- + ( options . enableTrailingSemicolonForTypes ? ';' : '' )
216
+ return ( hasComment ( ast ) ? generateComment ( ast . comment ) + '\n' : '' )
217
+ + `export type ${ toSafeString ( ast . standaloneName ) } = ${ generateType ( omit < ASTWithStandaloneName , AST > ( ast , 'standaloneName' ) , options ) } `
228
218
}
229
219
230
220
function escapeKeyName ( keyName : string ) : string {
0 commit comments