@@ -72,13 +72,63 @@ function visitDateType(type: ts.ObjectType, visitorContext: VisitorContext) {
72
72
} ) ;
73
73
}
74
74
75
+ function createRecursiveCall ( functionName : string , functionArgument : ts . Expression , pathExpression : ts . Expression , visitorContext : VisitorContext ) : ts . Statement [ ] {
76
+ const errorIdentifier = ts . createIdentifier ( 'error' ) ;
77
+ const emitDetailedErrors = ! ! visitorContext . options . emitDetailedErrors ;
78
+
79
+ const statements : ts . Statement [ ] = [ ] ;
80
+ if ( emitDetailedErrors ) {
81
+ statements . push ( ts . createExpressionStatement (
82
+ ts . createCall (
83
+ ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'push' ) ,
84
+ undefined ,
85
+ [
86
+ VisitorUtils . createBinaries (
87
+ [
88
+ pathExpression
89
+ ] ,
90
+ ts . SyntaxKind . PlusToken
91
+ )
92
+ ]
93
+ )
94
+ ) ) ;
95
+ }
96
+ statements . push ( ts . createVariableStatement (
97
+ [ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
98
+ [
99
+ ts . createVariableDeclaration (
100
+ errorIdentifier ,
101
+ undefined ,
102
+ ts . createCall (
103
+ ts . createIdentifier ( functionName ) ,
104
+ undefined ,
105
+ [ functionArgument ]
106
+ )
107
+ )
108
+ ]
109
+ ) ) ;
110
+ if ( emitDetailedErrors ) {
111
+ statements . push ( ts . createExpressionStatement (
112
+ ts . createCall (
113
+ ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'pop' ) ,
114
+ undefined ,
115
+ undefined
116
+ )
117
+ ) ) ;
118
+ }
119
+ statements . push ( ts . createIf (
120
+ errorIdentifier ,
121
+ ts . createReturn ( errorIdentifier )
122
+ ) ) ;
123
+ return statements ;
124
+ }
125
+
75
126
function visitTupleObjectType ( type : ts . TupleType , visitorContext : VisitorContext ) {
76
127
const name = VisitorTypeName . visitType ( type , visitorContext , { type : 'type-check' } ) ;
77
128
return VisitorUtils . setFunctionIfNotExists ( name , visitorContext , ( ) => {
78
129
const functionNames = type . typeArguments ?
79
130
type . typeArguments . map ( ( type ) => visitType ( type , visitorContext ) )
80
131
: [ ] ;
81
- const errorIdentifier = ts . createIdentifier ( 'error' ) ;
82
132
83
133
const maxLength = functionNames . length ;
84
134
let minLength = functionNames . length ;
@@ -134,40 +184,12 @@ function visitTupleObjectType(type: ts.TupleType, visitorContext: VisitorContext
134
184
ts . createReturn ( VisitorUtils . createErrorObject ( { type : 'tuple' , minLength, maxLength } , visitorContext ) )
135
185
) ,
136
186
...functionNames . map ( ( functionName , index ) =>
137
- ts . createBlock ( [
138
- ts . createExpressionStatement (
139
- ts . createCall (
140
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'push' ) ,
141
- undefined ,
142
- [ ts . createStringLiteral ( `[${ index } ]` ) ]
143
- )
144
- ) ,
145
- ts . createVariableStatement (
146
- [ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
147
- [
148
- ts . createVariableDeclaration (
149
- errorIdentifier ,
150
- undefined ,
151
- ts . createCall (
152
- ts . createIdentifier ( functionName ) ,
153
- undefined ,
154
- [ ts . createElementAccess ( VisitorUtils . objectIdentifier , index ) ]
155
- )
156
- )
157
- ]
158
- ) ,
159
- ts . createExpressionStatement (
160
- ts . createCall (
161
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'pop' ) ,
162
- undefined ,
163
- undefined
164
- )
165
- ) ,
166
- ts . createIf (
167
- errorIdentifier ,
168
- ts . createReturn ( errorIdentifier )
169
- )
170
- ] )
187
+ ts . createBlock ( createRecursiveCall (
188
+ functionName ,
189
+ ts . createElementAccess ( VisitorUtils . objectIdentifier , index ) ,
190
+ ts . createStringLiteral ( `[${ index } ]` ) ,
191
+ visitorContext
192
+ ) )
171
193
) ,
172
194
ts . createReturn ( ts . createNull ( ) )
173
195
] )
@@ -184,7 +206,7 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
184
206
}
185
207
const functionName = visitType ( numberIndexType , visitorContext ) ;
186
208
const indexIdentifier = ts . createIdentifier ( 'i' ) ;
187
- const errorIdentifier = ts . createIdentifier ( 'error' ) ;
209
+
188
210
return ts . createFunctionDeclaration (
189
211
undefined ,
190
212
undefined ,
@@ -218,49 +240,21 @@ function visitArrayObjectType(type: ts.ObjectType, visitorContext: VisitorContex
218
240
ts . createPropertyAccess ( VisitorUtils . objectIdentifier , 'length' )
219
241
) ,
220
242
ts . createPostfixIncrement ( indexIdentifier ) ,
221
- ts . createBlock ( [
222
- ts . createExpressionStatement (
223
- ts . createCall (
224
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'push' ) ,
225
- undefined ,
243
+ ts . createBlock (
244
+ createRecursiveCall (
245
+ functionName ,
246
+ ts . createElementAccess ( VisitorUtils . objectIdentifier , indexIdentifier ) ,
247
+ VisitorUtils . createBinaries (
226
248
[
227
- VisitorUtils . createBinaries (
228
- [
229
- ts . createStringLiteral ( '[' ) ,
230
- indexIdentifier ,
231
- ts . createStringLiteral ( ']' )
232
- ] ,
233
- ts . SyntaxKind . PlusToken
234
- )
235
- ]
236
- )
237
- ) ,
238
- ts . createVariableStatement (
239
- [ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
240
- [
241
- ts . createVariableDeclaration (
242
- errorIdentifier ,
243
- undefined ,
244
- ts . createCall (
245
- ts . createIdentifier ( functionName ) ,
246
- undefined ,
247
- [ ts . createElementAccess ( VisitorUtils . objectIdentifier , indexIdentifier ) ]
248
- )
249
- )
250
- ]
251
- ) ,
252
- ts . createExpressionStatement (
253
- ts . createCall (
254
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'pop' ) ,
255
- undefined ,
256
- undefined
257
- )
258
- ) ,
259
- ts . createIf (
260
- errorIdentifier ,
261
- ts . createReturn ( errorIdentifier )
249
+ ts . createStringLiteral ( '[' ) ,
250
+ indexIdentifier ,
251
+ ts . createStringLiteral ( ']' )
252
+ ] ,
253
+ ts . SyntaxKind . PlusToken
254
+ ) ,
255
+ visitorContext
262
256
)
263
- ] )
257
+ )
264
258
) ,
265
259
ts . createReturn ( ts . createNull ( ) )
266
260
] )
@@ -275,7 +269,6 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
275
269
const stringIndexType = visitorContext . checker . getIndexTypeOfType ( type , ts . IndexKind . String ) ;
276
270
const stringIndexFunctionName = stringIndexType ? visitType ( stringIndexType , visitorContext ) : undefined ;
277
271
const keyIdentifier = ts . createIdentifier ( 'key' ) ;
278
- const errorIdentifier = ts . createIdentifier ( 'error' ) ;
279
272
return ts . createFunctionDeclaration (
280
273
undefined ,
281
274
undefined ,
@@ -329,40 +322,14 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
329
322
ts . SyntaxKind . InKeyword ,
330
323
VisitorUtils . objectIdentifier
331
324
) ,
332
- ts . createBlock ( [
333
- ts . createExpressionStatement (
334
- ts . createCall (
335
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'push' ) ,
336
- undefined ,
337
- [ ts . createStringLiteral ( propertyInfo . name ) ]
338
- )
339
- ) ,
340
- ts . createVariableStatement (
341
- [ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
342
- [
343
- ts . createVariableDeclaration (
344
- errorIdentifier ,
345
- undefined ,
346
- ts . createCall (
347
- ts . createIdentifier ( functionName ) ,
348
- undefined ,
349
- [ ts . createElementAccess ( VisitorUtils . objectIdentifier , ts . createStringLiteral ( propertyInfo . name ) ) ]
350
- )
351
- )
352
- ]
353
- ) ,
354
- ts . createExpressionStatement (
355
- ts . createCall (
356
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'pop' ) ,
357
- undefined ,
358
- undefined
359
- )
360
- ) ,
361
- ts . createIf (
362
- errorIdentifier ,
363
- ts . createReturn ( errorIdentifier )
325
+ ts . createBlock (
326
+ createRecursiveCall (
327
+ functionName ,
328
+ ts . createElementAccess ( VisitorUtils . objectIdentifier , ts . createStringLiteral ( propertyInfo . name ) ) ,
329
+ ts . createStringLiteral ( propertyInfo . name ) ,
330
+ visitorContext
364
331
)
365
- ] ) ,
332
+ ) ,
366
333
propertyInfo . optional
367
334
? undefined
368
335
: ts . createReturn ( VisitorUtils . createErrorObject ( { type : 'missing-property' , property : propertyInfo . name } , visitorContext ) )
@@ -384,40 +351,14 @@ function visitRegularObjectType(type: ts.ObjectType, visitorContext: VisitorCont
384
351
ts . NodeFlags . Const
385
352
) ,
386
353
ts . createCall ( ts . createPropertyAccess ( ts . createIdentifier ( 'Object' ) , 'keys' ) , undefined , [ VisitorUtils . objectIdentifier ] ) ,
387
- ts . createBlock ( [
388
- ts . createExpressionStatement (
389
- ts . createCall (
390
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'push' ) ,
391
- undefined ,
392
- [ keyIdentifier ]
393
- )
394
- ) ,
395
- ts . createVariableStatement (
396
- [ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
397
- [
398
- ts . createVariableDeclaration (
399
- errorIdentifier ,
400
- undefined ,
401
- ts . createCall (
402
- ts . createIdentifier ( stringIndexFunctionName ) ,
403
- undefined ,
404
- [ ts . createElementAccess ( VisitorUtils . objectIdentifier , keyIdentifier ) ]
405
- )
406
- )
407
- ]
408
- ) ,
409
- ts . createExpressionStatement (
410
- ts . createCall (
411
- ts . createPropertyAccess ( VisitorUtils . pathIdentifier , 'pop' ) ,
412
- undefined ,
413
- undefined
414
- )
415
- ) ,
416
- ts . createIf (
417
- errorIdentifier ,
418
- ts . createReturn ( errorIdentifier )
354
+ ts . createBlock (
355
+ createRecursiveCall (
356
+ stringIndexFunctionName ,
357
+ ts . createElementAccess ( VisitorUtils . objectIdentifier , keyIdentifier ) ,
358
+ keyIdentifier ,
359
+ visitorContext
419
360
)
420
- ] )
361
+ )
421
362
)
422
363
]
423
364
: [ ]
0 commit comments