@@ -41,25 +41,34 @@ function visitClassDeclaration(
41
41
return classDeclaration ;
42
42
}
43
43
44
- const firstHeritageClauses = classDeclaration . heritageClauses [ 0 ] ;
45
- const expressionWithTypeArguments = firstHeritageClauses . types [ 0 ] ;
46
-
47
- firstHeritageClauses . types [ 0 ] = ts . updateExpressionWithTypeArguments (
48
- expressionWithTypeArguments ,
49
- [
50
- getPropsTypeOfReactComponentClass ( classDeclaration , sourceFile ) ,
51
- getStateTypeOfReactComponentClass ( classDeclaration , typeChecker ) ,
52
- ] ,
53
- expressionWithTypeArguments . expression ,
54
- )
44
+ const firstHeritageClause = classDeclaration . heritageClauses [ 0 ] ;
45
+
46
+ const newFirstHeritageClauseTypes = helpers . replaceItem (
47
+ firstHeritageClause . types ,
48
+ firstHeritageClause . types [ 0 ] ,
49
+ ts . updateExpressionWithTypeArguments (
50
+ firstHeritageClause . types [ 0 ] ,
51
+ [
52
+ getPropsTypeOfReactComponentClass ( classDeclaration , sourceFile ) ,
53
+ getStateTypeOfReactComponentClass ( classDeclaration , typeChecker ) ,
54
+ ] ,
55
+ firstHeritageClause . types [ 0 ] . expression ,
56
+ ) ,
57
+ ) ;
58
+
59
+ const newHeritageClauses = helpers . replaceItem (
60
+ classDeclaration . heritageClauses ,
61
+ firstHeritageClause ,
62
+ ts . updateHeritageClause ( firstHeritageClause , newFirstHeritageClauseTypes ) ,
63
+ ) ;
55
64
56
65
return ts . updateClassDeclaration (
57
66
classDeclaration ,
58
67
classDeclaration . decorators ,
59
68
classDeclaration . modifiers ,
60
69
classDeclaration . name ,
61
70
classDeclaration . typeParameters ,
62
- classDeclaration . heritageClauses ,
71
+ newHeritageClauses ,
63
72
classDeclaration . members ,
64
73
) ;
65
74
}
@@ -228,39 +237,44 @@ function getStateLookingForSetStateCalls(
228
237
* @param objectLiteral
229
238
*/
230
239
function buildInterfaceFromPropTypeObjectLiteral ( objectLiteral : ts . ObjectLiteralExpression ) {
231
- const resultObjectLiteral = objectLiteral . properties . reduce (
232
- ( result , propertyAssignment : ts . PropertyAssignment ) => {
240
+ const members = objectLiteral . properties
241
+ // We only need to process PropertyAssignment:
242
+ // {
243
+ // a: 123 // PropertyAssignment
244
+ // }
245
+ //
246
+ // filter out:
247
+ // {
248
+ // a() {}, // MethodDeclaration
249
+ // b, // ShorthandPropertyAssignment
250
+ // ...c, // SpreadAssignment
251
+ // get d() {}, // AccessorDeclaration
252
+ // }
253
+ . filter ( ts . isPropertyAssignment )
254
+ . filter ( property => {
255
+ return (
256
+ // Ignore children, React types have it
257
+ property . name . getText ( ) !== 'children' &&
258
+ ts . isPropertyAccessExpression ( property . initializer )
259
+ )
260
+ } )
261
+ . map ( propertyAssignment => {
233
262
const name = propertyAssignment . name . getText ( ) ;
234
- if ( ! helpers . isPropertyAccessExpression ( propertyAssignment . initializer ) ) {
235
- console . warn ( 'Bad value for propType' , name , 'at' , propertyAssignment . getStart ( ) ) ;
236
- return result ;
237
- }
238
-
239
- // Ignore children, React types have it
240
- if ( propertyAssignment . name . getText ( ) === 'children' ) {
241
- return result ;
242
- }
263
+ // We have guarantee this in the previous `filter`
264
+ const initializer = propertyAssignment . initializer as ts . PropertyAccessExpression
265
+ const typeValue = getTypeFromReactPropTypeExpression ( initializer ) ;
266
+ const isOptional = isPropTypeOptional ( initializer ) ;
243
267
244
- // Ignore children, React types have it
245
- if ( propertyAssignment . name . getText ( ) === 'children' ) {
246
- return result ;
247
- }
248
-
249
- const typeValue = getTypeFromReactPropTypeExpression ( propertyAssignment . initializer ) ;
250
- const isOptional = isPropTypeOptional ( propertyAssignment . initializer ) ;
251
- const propertySignature = ts . createPropertySignature (
268
+ return ts . createPropertySignature (
252
269
[ ] ,
253
270
name ,
254
- isOptional ? ts . createToken ( ts . SyntaxKind . QuestionToken ) : undefined ,
271
+ isOptional ? ts . createToken ( ts . SyntaxKind . QuestionToken ) : undefined ,
255
272
typeValue ,
256
273
undefined ,
257
274
) ;
258
- result . members . push ( propertySignature )
259
- return result ;
260
- } , ts . createTypeLiteralNode ( [ ] ) ) ;
261
-
275
+ } ) ;
262
276
263
- return resultObjectLiteral ;
277
+ return ts . createTypeLiteralNode ( members )
264
278
}
265
279
266
280
/**
0 commit comments