fixAddMissingMember: Add a new PropertyDeclaration at the end of the first set (#23837)

This commit is contained in:
Andy
2018-05-02 15:42:05 -07:00
committed by GitHub
parent 5aa0a79dac
commit 306418e171
3 changed files with 20 additions and 4 deletions

View File

@@ -132,7 +132,24 @@ namespace ts.codefix {
/*questionToken*/ undefined,
typeNode,
/*initializer*/ undefined);
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, property);
const lastProp = getNodeToInsertPropertyAfter(classDeclaration);
if (lastProp) {
changeTracker.insertNodeAfter(classDeclarationSourceFile, lastProp, property);
}
else {
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, property);
}
}
// Gets the last of the first run of PropertyDeclarations, or undefined if the class does not start with a PropertyDeclaration.
function getNodeToInsertPropertyAfter(cls: ClassLikeDeclaration): PropertyDeclaration | undefined {
let res: PropertyDeclaration | undefined;
for (const member of cls.members) {
if (!isPropertyDeclaration(member)) break;
res = member;
}
return res;
}
function createAddIndexSignatureAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode): CodeFixAction {