fixClassDoesnotImplementInheritedAbstractMember: Don't perform fix for same class twice (#22073)

This commit is contained in:
Andy
2018-02-21 10:03:02 -08:00
committed by GitHub
parent dd47f2492b
commit dda4bd0d0b
4 changed files with 20 additions and 11 deletions

View File

@@ -14,18 +14,22 @@ namespace ts.codefix {
return changes.length === 0 ? undefined : [{ description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class), changes, fixId }];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
addMissingMembers(getClass(diag.file!, diag.start!), context.sourceFile, context.program.getTypeChecker(), changes);
}),
getAllCodeActions: context => {
const seenClassDeclarations = createMap<true>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const classDeclaration = getClass(diag.file!, diag.start!);
if (addToSeen(seenClassDeclarations, getNodeId(classDeclaration))) {
addMissingMembers(classDeclaration, context.sourceFile, context.program.getTypeChecker(), changes);
}
});
},
});
function getClass(sourceFile: SourceFile, pos: number): ClassLikeDeclaration {
// This is the identifier in the case of a class declaration
// Token is the identifier in the case of a class declaration
// or the class keyword token in the case of a class expression.
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const classDeclaration = token.parent;
Debug.assert(isClassLike(classDeclaration));
return classDeclaration as ClassLikeDeclaration;
return cast(token.parent, isClassLike);
}
function addMissingMembers(classDeclaration: ClassLikeDeclaration, sourceFile: SourceFile, checker: TypeChecker, changeTracker: textChanges.ChangeTracker): void {

View File

@@ -31,9 +31,7 @@ namespace ts.codefix {
});
function getClass(sourceFile: SourceFile, pos: number): ClassLikeDeclaration {
const classDeclaration = getContainingClass(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false));
Debug.assert(!!classDeclaration);
return classDeclaration!;
return Debug.assertDefined(getContainingClass(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false)));
}
function addMissingDeclarations(

View File

@@ -2,6 +2,7 @@
////abstract class A {
//// abstract m(): void;
//// abstract n(): void;
////}
////class B extends A {}
////class C extends A {}
@@ -11,15 +12,22 @@ verify.codeFixAll({
newFileContent:
`abstract class A {
abstract m(): void;
abstract n(): void;
}
class B extends A {
m(): void {
throw new Error("Method not implemented.");
}
n(): void {
throw new Error("Method not implemented.");
}
}
class C extends A {
m(): void {
throw new Error("Method not implemented.");
}
n(): void {
throw new Error("Method not implemented.");
}
}`,
});

View File

@@ -7,7 +7,6 @@
verify.codeFixAll({
fixId: "fixClassIncorrectlyImplementsInterface",
// TODO: GH#20073
newFileContent:
`interface I { i(): void; }
interface J { j(): void; }