Skip to content

Commit eebb895

Browse files
authored
fix(43495): insert Override keyword after accessibility modifier (microsoft#43504)
1 parent 3a22b3e commit eebb895

File tree

7 files changed

+118
-1
lines changed

7 files changed

+118
-1
lines changed

src/services/codefixes/fixOverrideModifier.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ namespace ts.codefix {
8181

8282
function doAddOverrideModifierChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
8383
const classElement = findContainerClassElement(sourceFile, pos);
84-
changeTracker.insertModifierBefore(sourceFile, SyntaxKind.OverrideKeyword, classElement);
84+
const accessibilityModifier = find(classElement.modifiers || emptyArray, m => isAccessibilityModifier(m.kind));
85+
const modifierPos = accessibilityModifier ? accessibilityModifier.end :
86+
classElement.decorators ? skipTrivia(sourceFile.text, classElement.decorators.end) : classElement.getStart(sourceFile);
87+
const options = accessibilityModifier ? { prefix: " " } : { suffix: " " };
88+
changeTracker.insertModifierAt(sourceFile, modifierPos, SyntaxKind.OverrideKeyword, options);
8589
}
8690

8791
function doRemoveOverrideModifierChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitOverride: true
4+
5+
////class A {
6+
//// foo() {}
7+
////}
8+
////
9+
////class B extends A {
10+
//// [|public foo() {}|]
11+
////}
12+
13+
verify.codeFix({
14+
description: "Add 'override' modifier",
15+
newRangeContent: "public override foo() {}",
16+
index: 0
17+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitOverride: true
4+
5+
////class A {
6+
//// protected async foo() {}
7+
////}
8+
////
9+
////class B extends A {
10+
//// [|protected async foo() {}|]
11+
////}
12+
13+
verify.codeFix({
14+
description: "Add 'override' modifier",
15+
newRangeContent: "protected override async foo() {}",
16+
index: 0
17+
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @experimentalDecorators: true
4+
// @noImplicitOverride: true
5+
6+
////function decorator() {
7+
//// return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
8+
////}
9+
////class A {
10+
//// foo() {}
11+
////}
12+
////class B extends A {
13+
//// @decorator()
14+
//// [|public foo() {}|]
15+
////}
16+
17+
verify.codeFix({
18+
description: "Add 'override' modifier",
19+
newRangeContent: "public override foo() {}",
20+
index: 0
21+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @experimentalDecorators: true
4+
// @noImplicitOverride: true
5+
6+
////function decorator() {
7+
//// return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
8+
////}
9+
////class A {
10+
//// foo() {}
11+
////}
12+
////class B extends A {
13+
//// @decorator()
14+
//// // comment
15+
//// [|foo() {}|]
16+
////}
17+
18+
verify.codeFix({
19+
description: "Add 'override' modifier",
20+
newRangeContent: "override foo() {}",
21+
index: 0
22+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @experimentalDecorators: true
4+
// @noImplicitOverride: true
5+
6+
////function decorator() {
7+
//// return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
8+
////}
9+
////class A {
10+
//// foo() {}
11+
////}
12+
////class B extends A {
13+
//// @decorator()
14+
//// /**
15+
//// * comment
16+
//// */
17+
//// [|foo() {}|]
18+
////}
19+
20+
verify.codeFix({
21+
description: "Add 'override' modifier",
22+
newRangeContent: "override foo() {}",
23+
index: 0
24+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitOverride: true
4+
5+
////class A {
6+
//// static foo() {}
7+
////}
8+
////class B extends A {
9+
//// [|static foo() {}|]
10+
////}
11+
12+
verify.not.codeFixAvailable("fixAddOverrideModifier");

0 commit comments

Comments
 (0)