Skip to content

Added override keyword to codefix implemented abstract methods #51033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,8 @@ export function append<T>(to: T[], value: T | undefined): T[] | undefined {
*
* @internal
*/
export function combine<T>(xs: T[] | undefined, ys: T[] | undefined): T[] | undefined;
/** @internal */
export function combine<T>(xs: T | readonly T[] | undefined, ys: T | readonly T[] | undefined): T | readonly T[] | undefined;
/** @internal */
export function combine<T>(xs: T | T[] | undefined, ys: T | T[] | undefined): T | T[] | undefined;
Expand Down
23 changes: 22 additions & 1 deletion src/services/codefixes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CharacterCodes,
ClassLikeDeclaration,
CodeFixContextBase,
combine,
Debug,
Diagnostics,
emptyArray,
Expand All @@ -31,6 +32,7 @@ import {
getSynthesizedDeepClone,
getTokenAtPosition,
getTsConfigObjectLiteralExpression,
hasAbstractModifier,
Identifier,
idText,
IntersectionType,
Expand Down Expand Up @@ -197,7 +199,7 @@ export function addNewNodeForMemberSymbol(
if (declaration && isAutoAccessorPropertyDeclaration(declaration)) {
modifierFlags |= ModifierFlags.Accessor;
}
const modifiers = modifierFlags ? factory.createNodeArray(factory.createModifiersFromModifierFlags(modifierFlags)) : undefined;
const modifiers = createModifiers();
const type = checker.getWidenedType(checker.getTypeOfSymbolAtLocation(symbol, enclosingDeclaration));
const optional = !!(symbol.flags & SymbolFlags.Optional);
const ambient = !!(enclosingDeclaration.flags & NodeFlags.Ambient) || isAmbient;
Expand Down Expand Up @@ -304,6 +306,25 @@ export function addNewNodeForMemberSymbol(
if (method) addClassElement(method);
}


function createModifiers(): NodeArray<Modifier> | undefined {
let modifiers: Modifier[] | undefined;

if (modifierFlags) {
modifiers = combine(modifiers, factory.createModifiersFromModifierFlags(modifierFlags));
}

if (shouldAddOverrideKeyword()) {
modifiers = append(modifiers, factory.createToken(SyntaxKind.OverrideKeyword));
}

return modifiers && factory.createNodeArray(modifiers);
}

function shouldAddOverrideKeyword(): boolean {
return !!(context.program.getCompilerOptions().noImplicitOverride && declaration && hasAbstractModifier(declaration));
}

function createName(node: PropertyName) {
return getSynthesizedDeepClone(node, /*includeTrivia*/ false);
}
Expand Down
15 changes: 8 additions & 7 deletions tests/cases/fourslash/codeFixAmbientClassExtendAbstractMethod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract f(a: number, b: string): boolean;
//// abstract f(a: number, b: string): this;
Expand Down Expand Up @@ -30,12 +31,12 @@ verify.codeFix({
}

declare class C extends A {
f(a: number, b: string): boolean;
f(a: number, b: string): this;
f(a: string, b: number): Function;
f(a: string): Function;
f1(this: A): number;
f2(this: A, a: number, b: string): number;
foo(): number;
override f(a: number, b: string): boolean;
override f(a: number, b: string): this;
override f(a: string, b: number): Function;
override f(a: string): Function;
override f1(this: A): number;
override f2(this: A, a: number, b: string): number;
override foo(): number;
}`
});
17 changes: 9 additions & 8 deletions tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract get a(): number | string;
//// abstract get b(): this;
Expand Down Expand Up @@ -38,28 +39,28 @@ verify.codeFix({
abstract class B extends A {}

class C extends A {
get a(): string | number {
override get a(): string | number {
throw new Error("Method not implemented.");
}
get b(): this {
override get b(): this {
throw new Error("Method not implemented.");
}
get c(): A {
override get c(): A {
throw new Error("Method not implemented.");
}
set d(arg: string | number) {
override set d(arg: string | number) {
throw new Error("Method not implemented.");
}
set e(arg: this) {
override set e(arg: this) {
throw new Error("Method not implemented.");
}
set f(arg: A) {
override set f(arg: A) {
throw new Error("Method not implemented.");
}
get g(): string {
override get g(): string {
throw new Error("Method not implemented.");
}
set g(newName: string) {
override set g(newName: string) {
throw new Error("Method not implemented.");
}
}`
Expand Down
13 changes: 7 additions & 6 deletions tests/cases/fourslash/codeFixClassExtendAbstractMethod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract f(a: number, b: string): boolean;
//// abstract f(a: number, b: string): this;
Expand All @@ -22,14 +23,14 @@ verify.codeFix({
}

class C extends A {
f(a: number, b: string): boolean;
f(a: number, b: string): this;
f(a: string, b: number): Function;
f(a: string): Function;
f(a: unknown, b?: unknown): boolean | Function | this {
override f(a: number, b: string): boolean;
override f(a: number, b: string): this;
override f(a: string, b: number): Function;
override f(a: string): Function;
override f(a: unknown, b?: unknown): boolean | Function | this {
throw new Error("Method not implemented.");
}
foo(): number {
override foo(): number {
throw new Error("Method not implemented.");
}
}`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
//// abstract class A {
//// abstract foo(n: number | string): number | string;
//// }
////
//// abstract class B extends A {
//// abstract override foo(n: number): number;
//// }
////
//// class C extends B { }

verify.codeFix({
description: "Implement inherited abstract class",
newFileContent: `abstract class A {
abstract foo(n: number | string): number | string;
}

abstract class B extends A {
abstract override foo(n: number): number;
}

class C extends B {
override foo(n: number): number {
throw new Error("Method not implemented.");
}
}`,
});
9 changes: 5 additions & 4 deletions tests/cases/fourslash/codeFixClassExtendAbstractMethod_all.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract m(): void;
//// abstract n(): void;
Expand All @@ -16,18 +17,18 @@ verify.codeFixAll({
abstract n(): void;
}
class B extends A {
m(): void {
override m(): void {
throw new Error("Method not implemented.");
}
n(): void {
override n(): void {
throw new Error("Method not implemented.");
}
}
class C extends A {
m(): void {
override m(): void {
throw new Error("Method not implemented.");
}
n(): void {
override n(): void {
throw new Error("Method not implemented.");
}
}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract m() : void;
////}
Expand All @@ -16,7 +17,7 @@ verify.codeFix({
}

class B extends A {
m(): void {
override m(): void {
throw new Error("Method not implemented.");
}
// comment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
//// abstract class A {
//// private abstract x: number;
//// m() { this.x; } // Avoid unused private
Expand Down
7 changes: 4 additions & 3 deletions tests/cases/fourslash/codeFixClassExtendAbstractProperty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract x: number;
//// abstract y: this;
Expand All @@ -18,8 +19,8 @@ verify.codeFix({
}

class C extends A {
x: number;
y: this;
z: A;
override x: number;
override y: this;
override z: A;
}`
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// abstract x: this;
////}
Expand All @@ -14,6 +15,6 @@ verify.codeFix({
}

class C extends A {
x: this;
override x: this;
}`,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// protected abstract x: number;
////}
Expand All @@ -14,6 +15,6 @@ verify.codeFix({
}

class C extends A {
protected x: number;
protected override x: number;
}`,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
////abstract class A {
//// public abstract x: number;
////}
Expand All @@ -14,6 +15,6 @@ verify.codeFix({
}

class C extends A {
public x: number;
public override x: number;
}`,
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path='fourslash.ts' />

// @noImplicitOverride: true
//// abstract class A {
//// abstract x: number;
//// abstract y: number;
Expand All @@ -12,5 +13,5 @@
//// }

verify.rangeAfterCodeFix(`
z: number;
override z: number;
`);