From 306418e17133baac3b66754eb700581a8ba24312 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 2 May 2018 15:42:05 -0700 Subject: [PATCH] fixAddMissingMember: Add a new PropertyDeclaration at the end of the first set (#23837) --- src/services/codefixes/fixAddMissingMember.ts | 19 ++++++++++++++++++- .../codeFixUndeclaredInStaticMethod.ts | 3 +-- ...UndeclaredPropertyFunctionNonEmptyClass.ts | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/services/codefixes/fixAddMissingMember.ts b/src/services/codefixes/fixAddMissingMember.ts index 940201d7008..e53ce41ad8a 100644 --- a/src/services/codefixes/fixAddMissingMember.ts +++ b/src/services/codefixes/fixAddMissingMember.ts @@ -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 { diff --git a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts index f1bac17b2fa..84b0ebd4414 100644 --- a/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts +++ b/tests/cases/fourslash/codeFixUndeclaredInStaticMethod.ts @@ -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."); diff --git a/tests/cases/fourslash/codeFixUndeclaredPropertyFunctionNonEmptyClass.ts b/tests/cases/fourslash/codeFixUndeclaredPropertyFunctionNonEmptyClass.ts index b05fb0d42b5..30d7e13cb56 100644 --- a/tests/cases/fourslash/codeFixUndeclaredPropertyFunctionNonEmptyClass.ts +++ b/tests/cases/fourslash/codeFixUndeclaredPropertyFunctionNonEmptyClass.ts @@ -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;