@@ -8,26 +8,57 @@ namespace ts.codefix {
8
8
const token = getTokenAtPosition ( sourceFile , start ) ;
9
9
const checker = context . program . getTypeChecker ( ) ;
10
10
11
- if ( token . kind === SyntaxKind . Identifier && isClassLike ( token . parent ) ) {
12
- const classDeclaration = < ClassDeclaration > token . parent ;
13
- const startPos : number = classDeclaration . members . pos ;
11
+ if ( ! ( token . kind === SyntaxKind . Identifier && isClassLike ( token . parent ) ) ) {
12
+ return undefined ;
13
+ }
14
+ const classDecl = < ClassDeclaration > token . parent ;
15
+ const startPos : number = classDecl . members . pos ;
16
+
17
+ const implementedTypeNodes = getClassImplementsHeritageClauseElements ( classDecl ) ;
18
+ const implementedTypes = implementedTypeNodes . map ( checker . getTypeFromTypeReference ) ;
19
+ const resolvedImplementedTypes = implementedTypes . map ( checker . resolveStructuredTypeMembers ) ;
20
+
21
+ let result : CodeAction [ ] | undefined = undefined ;
22
+
23
+ for ( const resolvedType of resolvedImplementedTypes ) {
24
+ const insertion = getMissingMembersInsertion ( classDecl , resolvedType , checker , context . newLineCharacter ) ;
25
+ result = pushAction ( insertion , getLocaleSpecificMessage ( Diagnostics . Implement_interface_on_class ) , result ) ;
26
+ }
27
+
28
+ // TODO: (arozga) Get this working and figure out how to test it reliably.
29
+ /*
30
+ // If there are multiple objects, we additionally try to generate a combined fix that simultaneously implements all types.
31
+ const intersectionType = checker.getIntersectionType(implementedTypes);
32
+ if(intersectionType.flags & TypeFlags.Intersection) {
33
+ const resolvedIntersectionType = checker.resolveStructuredTypeMembers(<IntersectionType>intersectionType)
34
+ const insertion = getMissingMembersInsertion(classDecl, resolvedIntersectionType, checker, context.newLineCharacter);
35
+ result = pushAction(insertion, "stubbed locale message", result)
36
+ }
37
+ */
14
38
15
- const insertion = getMissingInterfaceMembersInsertion ( classDeclaration , checker , context . newLineCharacter ) ;
39
+ return result ;
16
40
41
+ function pushAction ( insertion : string , description : string , result ?: CodeAction [ ] ) : CodeAction [ ] {
17
42
if ( insertion && insertion . length ) {
18
- return [ {
19
- description : getLocaleSpecificMessage ( Diagnostics . Implement_interface_on_class ) ,
43
+ const newAction : CodeAction = {
44
+ description : description ,
20
45
changes : [ {
21
46
fileName : sourceFile . fileName ,
22
47
textChanges : [ {
23
48
span : { start : startPos , length : 0 } ,
24
49
newText : insertion
25
50
} ]
26
51
} ]
27
- } ] ;
52
+ } ;
53
+ if ( result ) {
54
+ result . push ( newAction ) ;
55
+ }
56
+ else {
57
+ result = [ newAction ] ;
58
+ }
28
59
}
60
+ return result ;
29
61
}
30
- return undefined ;
31
62
}
32
63
} ) ;
33
64
}
0 commit comments