From f0c771303d4979959d0fdc476b40a91f59e98b55 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 5 Dec 2016 14:52:08 -0800 Subject: [PATCH] implement getters/setters as property --- src/services/codefixes/helpers.ts | 13 ++--- .../codeFixClassExtendsAbstractGetter.ts | 11 ++++ ...codeFixClassExtendsAbstractGetterSetter.ts | 17 ++++++ .../codeFixClassExtendsAbstractSetter.ts | 11 ++++ ...edClassMissingAbstractGettersAndSetters.ts | 52 +++++-------------- 5 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 tests/cases/fourslash/codeFixClassExtendsAbstractGetter.ts create mode 100644 tests/cases/fourslash/codeFixClassExtendsAbstractGetterSetter.ts create mode 100644 tests/cases/fourslash/codeFixClassExtendsAbstractSetter.ts diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 3c06ae6288f..f4d50b57dbf 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -28,8 +28,9 @@ namespace ts.codefix { } const node = declarations[0]; const visibility = getVisibilityPrefix(getModifierFlags(node)); - let getOrSetPrefix: string = undefined; switch (node.kind) { + case SyntaxKind.GetAccessor: + case SyntaxKind.SetAccessor: case SyntaxKind.PropertySignature: case SyntaxKind.PropertyDeclaration: const typeString = checker.typeToString(type, enclosingDeclaration, TypeFormatFlags.None); @@ -45,19 +46,13 @@ namespace ts.codefix { const sigString = checker.signatureToString(signatures[0], enclosingDeclaration, TypeFormatFlags.SuppressAnyReturnType, SignatureKind.Call); return `${visibility}${name}${sigString}${getMethodBodyStub(newlineChar)}`; - case SyntaxKind.GetAccessor: - getOrSetPrefix = "get"; - case SyntaxKind.SetAccessor: - getOrSetPrefix = getOrSetPrefix ? getOrSetPrefix : "set"; - - throw new Error('Not implemented, getter and setter.'); case SyntaxKind.ComputedPropertyName: if (hasDynamicName(node)) { return ""; } - throw new Error('Not implemented, computed property name.'); + throw new Error("Not implemented, computed property name."); case SyntaxKind.IndexSignature: - throw new Error('Not implemented.'); + throw new Error("Not implemented."); default: return ""; diff --git a/tests/cases/fourslash/codeFixClassExtendsAbstractGetter.ts b/tests/cases/fourslash/codeFixClassExtendsAbstractGetter.ts new file mode 100644 index 00000000000..8d79cce710e --- /dev/null +++ b/tests/cases/fourslash/codeFixClassExtendsAbstractGetter.ts @@ -0,0 +1,11 @@ +/// + +//// abstract class A { +//// abstract get b(): number; +//// } +//// +//// class C extends A {[| |]} + +verify.rangeAfterCodeFix(` + b: number; +`); \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixClassExtendsAbstractGetterSetter.ts b/tests/cases/fourslash/codeFixClassExtendsAbstractGetterSetter.ts new file mode 100644 index 00000000000..fc0ac400623 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassExtendsAbstractGetterSetter.ts @@ -0,0 +1,17 @@ +/// + +//// abstract class A { +//// private _a: string; +//// +//// abstract get a(): string; +//// abstract set a(newName: string); +//// } +//// +//// // Don't need to add anything in this case. +//// abstract class B extends A {} +//// +//// class C extends A {[| |]} + +verify.rangeAfterCodeFix(` + a: string; +`); \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixClassExtendsAbstractSetter.ts b/tests/cases/fourslash/codeFixClassExtendsAbstractSetter.ts new file mode 100644 index 00000000000..e8cb55fa660 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassExtendsAbstractSetter.ts @@ -0,0 +1,11 @@ +/// + +//// abstract class A { +//// abstract set c(arg: number | string); +//// } +//// +//// class C extends A {[| |]} + +verify.rangeAfterCodeFix(` + c: string | number; +`); \ No newline at end of file diff --git a/tests/cases/fourslash/codeFixUnImplementedClassMissingAbstractGettersAndSetters.ts b/tests/cases/fourslash/codeFixUnImplementedClassMissingAbstractGettersAndSetters.ts index 5540b20cfb9..df78803c588 100644 --- a/tests/cases/fourslash/codeFixUnImplementedClassMissingAbstractGettersAndSetters.ts +++ b/tests/cases/fourslash/codeFixUnImplementedClassMissingAbstractGettersAndSetters.ts @@ -1,44 +1,20 @@ /// -//// namespace N1 { -//// export interface I1 { -//// f1():string; -//// } -//// } -//// interface I1 { -//// f1(); +//// abstract class A { +//// private _a: string; +//// +//// abstract get a(): string; +//// abstract set a(newName: string); +//// +//// abstract get b(): number; +//// +//// abstract set c(arg: number | string); //// } //// -//// class C1 implements N1.I1 {[| -//// |]} +//// class C implements A {[| |]} -let passcode = "secret passcode"; - -abstract class A { - private _a: string; - - abstract get a(): string; - abstract set a(newName: string); -} - -class B extends A { +verify.rangeAfterCodeFix(` a: string; -} - - -abstract class AA { - private _a: string; - - abstract get a(): string { - return this._a; - } - - abstract set a(newName: string) { - this._a = newName; - } -} - -verify.rangeAfterCodeFix(`f1(): string{ - throw new Error('Method not implemented.'); -} -`); + b: number; + c: string | number; +`); \ No newline at end of file