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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 {

View file

@ -78,9 +78,8 @@ verify.codeFix({
index: 1, // fix at index 0 is to change the spelling to 'prop1'
newFileContent:
`class A {
static prop2: string;
static prop1: number;
static prop2: string;
static m2(arg0: any, arg1: any): any {
throw new Error("Method not implemented.");

View file

@ -11,8 +11,8 @@
verify.rangeAfterCodeFix(`
class A {
x: (x: number, y?: A) => number | A;
y: number;
x: (x: number, y?: A) => number | A;
constructor(public a: number) {
this.x = function(x: number, y?: A){
return x > 0 ? x : y;